-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.py
110 lines (84 loc) · 2.84 KB
/
packet.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
__author__ = 'jono'
import struct
class Packet:
# Little Endian uses prefix <B
def __init__(self, input=bytearray(), output=bytearray()):
self.input = input
self.output = output
# write null terminated utf-16 string
def write_string(self, data):
self.output += struct.pack('<B%iB' % (len(data)-1), *map(ord, data))
# Use to send opcodes too - write byte
def write_uint8(self, data):
self.output += struct.pack('<B', data)
# write short
def write_uint16(self, data):
self.output += struct.pack('<h', data)
# write int
def write_uint32(self, data):
self.output += struct.pack('<I', data)
# write float
def write_float32(self, data):
self.output += struct.pack('<f', data)
# write double
def write_float64(self, data):
self.output += struct.pack('<q', data)
# write boolean uint8
def write_bool(self, data):
if data:
self.output += struct.pack('<B', 1)
else:
self.output += struct.pack('<B', 0)
# return byte - unsigned 1 byte integer
def read_uint8(self):
value, = struct.unpack('<B', self.input[:1])
self.input = self.input[1:]
return value
# return short - unsigned 2 byte integer
def read_uint16(self):
value, = struct.unpack('<h', self.input[:2])
self.input = self.input[2:]
return value
# return int - unsigned 4 byte integer
def read_uint32(self):
value, = struct.unpack('<I', self.input[:4])
self.input = self.input[4:]
return value
# return float - signed 4 byte floating point value
def read_float32(self):
value, = struct.unpack('<f', self.input[:4])
self.input = self.input[4:]
return value
# return double - signed 8 byte floating point value
def read_float64(self):
value, = struct.unpack('<q', self.input[:8])
self.input = self.input[8:]
return value
# return boolean (uint8)
def read_bool(self):
value, = struct.unpack('<B', self.input[:1])
self.input = self.input[1:]
return value
def read_str8(self):
string_arr = []
while True:
bytes = self.read_uint8()
if len(bytes) == 0: break
string_arr.append(chr(bytes))
return ''.join(string_arr)
def read_str16(self):
string_arr = []
while True:
bytes = self.read_uint16()
if len(bytes) == 0: break
string_arr.append(chr(bytes))
return ''.join(string_arr)
def skip(self, size):
self.input = self.input[size:]
def read_session(self, session):
self.input = session.read()
def clear_input(self):
self.input = []
def flush_session(self, session):
session.send(self.output)
self.output = []