forked from StarryPy/StarryPy3k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pparser.py
187 lines (169 loc) · 5.03 KB
/
pparser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import asyncio
import traceback
from configuration_manager import ConfigurationManager
from data_parser import *
parse_map = {
0: ProtocolRequest,
1: ProtocolResponse,
2: ServerDisconnect,
3: ConnectSuccess,
4: ConnectFailure,
5: HandshakeChallenge,
6: ChatReceived,
7: None,
8: None,
9: PlayerWarpResult,
10: None,
11: None,
12: ClientConnect,
13: ClientDisconnectRequest,
14: None,
15: PlayerWarp,
16: FlyShip,
17: ChatSent,
18: None,
19: ClientContextUpdate,
20: WorldStart,
21: WorldStop,
22: None,
23: None,
24: None,
25: None,
26: None,
27: None,
28: None,
29: None,
30: GiveItem,
31: None,
32: None,
33: None,
34: None,
35: None,
36: None,
37: ModifyTileList,
38: None,
39: None,
40: None,
41: SpawnEntity,
42: None,
43: None,
44: None,
45: None,
46: None,
47: EntityCreate,
48: None,
49: None,
50: EntityInteract,
51: EntityInteractResult,
52: None,
53: DamageRequest,
54: DamageNotification,
55: EntityMessage,
56: EntityMessageResponse,
57: DictVariant,
58: StepUpdate,
59: None,
60: None,
61: None,
62: None,
63: None,
64: None,
65: None
}
class PacketParser:
"""
Object for handling the parsing and caching of packets.
"""
def __init__(self, config: ConfigurationManager):
self._cache = {}
self.config = config
self.loop = asyncio.get_event_loop()
self._reaper = self.loop.create_task(self._reap())
@asyncio.coroutine
def parse(self, packet):
"""
Given a packet preped packet from the stream, parse it down to its
parts. First check if the packet is one we've seen before; if it is,
pull its parsed form from the cache, and run with that. Otherwise,
pass it to the appropriate parser for parsing.
:param packet: Packet with header information parsed.
:return: Fully parsed packet.
"""
try:
if packet["size"] >= self.config.config["min_cache_size"]:
packet["hash"] = hash(packet["original_data"])
if packet["hash"] in self._cache:
self._cache[packet["hash"]].count += 1
packet["parsed"] = self._cache[packet["hash"]].packet[
"parsed"]
else:
packet = yield from self._parse_and_cache_packet(packet)
else:
packet = yield from self._parse_packet(packet)
except Exception as e:
print("Error during parsing.")
print(traceback.print_exc())
finally:
return packet
@asyncio.coroutine
def _reap(self):
"""
Prune packets from the cache that are not being used, and that are
older than the "packet_reap_time".
:return: None.
"""
while True:
yield from asyncio.sleep(self.config.config["packet_reap_time"])
for h, cached_packet in self._cache.copy().items():
cached_packet.count -= 1
if cached_packet.count <= 0:
del (self._cache[h])
@asyncio.coroutine
def _parse_and_cache_packet(self, packet):
"""
Take a new packet and pass it to the parser. Once we get it back,
make a copy of it to the cache.
:param packet: Packet with header information parsed.
:return: Fully parsed packet.
"""
packet = yield from self._parse_packet(packet)
self._cache[packet["hash"]] = CachedPacket(packet=packet)
return packet
@asyncio.coroutine
def _parse_packet(self, packet):
"""
Parse the packet by giving it to the appropriate parser.
:param packet: Packet with header information parsed.
:return: Fully parsed packet.
"""
res = parse_map[packet["type"]]
if res is None:
packet["parsed"] = {}
else:
#packet["parsed"] = yield from self.loop.run_in_executor(
# self.loop.executor, res.parse, packet["data"])
# Removed due to issues with testers. Need to evaluate what's going
# on.
packet["parsed"] = res.parse(packet["data"])
return packet
# def __del__(self):
# self._reaper.cancel()
class CachedPacket:
"""
Prototype for cached packets. Keep track of how often it is used,
as well as the full packet's contents.
"""
def __init__(self, packet):
self.count = 1
self.packet = packet
def build_packet(packet_id, data, compressed=False):
"""
Convenience method for building a packet.
:param packet_id: ID value of packet.
:param data: Contents of packet.
:param compressed: Whether or not to compress the packet.
:return: Built packet object.
"""
return BasePacket.build({"id": packet_id,
"data": data,
"compressed": compressed})