hxp
4 天以前 cb452abc7bcda94073cfe0d136e831252374853d
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/python
# -*- coding: GBK -*-
 
"""
ÍøÂçЭÒ鶨ÒåÄ£¿é
¶¨ÒåÊý¾Ý°ü½á¹¹ºÍЭÒéÍ·
"""
 
import struct
 
class ProtocolHead:
    """ЭÒéÍ·½á¹¹"""
    
    def __init__(self, data=None):
        self.cmd = 0
        self.sub_cmd = 0
        
        if data:
            self.from_bytes(data)
    
    def from_bytes(self, data):
        """´Ó×Ö½ÚÊý¾Ý½âÎö"""
        if len(data) >= 2:
            self.cmd = ord(data[0])
            self.sub_cmd = ord(data[1])
    
    def to_bytes(self):
        """ת»»Îª×Ö½ÚÊý¾Ý"""
        return struct.pack('BB', self.cmd, self.sub_cmd)
    
    def __str__(self):
        return 'Head: (Cmd:0x%02X, SubCmd:0x%02X)' % (self.cmd, self.sub_cmd)
    
    def __eq__(self, other):
        return self.cmd == other.cmd and self.sub_cmd == other.sub_cmd
    
    def __hash__(self):
        return hash((self.cmd, self.sub_cmd))
 
 
# Ð­Òé³£Á¿
CMD_INTERFACE = 0x01
SUB_CMD_LOGIN = 0x01        # µÇ¼
SUB_CMD_LOGOUT = 0x02       # µÇ³ö
SUB_CMD_EVENT_SEND = 0x03   # Ê¼þ·¢ËÍ
SUB_CMD_HEARTBEAT = 0x04    # ÐÄÌø
SUB_CMD_EVENT_STR = 0x05    # ×Ö·û´®Ê¼þ
 
CMD_MONITOR = 0x0A
SUB_CMD_OTHER_DAY_LOGIN = 0x01  # ·ÇͬÌìµÇ¼
SUB_CMD_PLAYER_LOGIN = 0x02     # Íæ¼ÒµÇ¼
SUB_CMD_LOGIN_VALID = 0x03      # ÓÐЧµÇ¼
SUB_CMD_FIRST_LOGIN = 0x04      # Ê״εǼ
 
LOGIN_MAGIC_CODE = 0x34128621
 
 
class PacketParser:
    """Êý¾Ý°ü½âÎöÆ÷"""
    
    @staticmethod
    def read_byte(data, pos):
        """¶ÁÈ¡×Ö½Ú"""
        if pos >= len(data):
            raise Exception('read_byte: pos out of range')
        return ord(data[pos]), pos + 1
    
    @staticmethod
    def read_uint32(data, pos):
        """¶ÁÈ¡32λÎÞ·ûºÅÕûÊý"""
        if pos + 4 > len(data):
            raise Exception('read_uint32: pos out of range')
        value = struct.unpack('<I', data[pos:pos+4])[0]
        return value, pos + 4
    
    @staticmethod
    def read_int64(data, pos):
        """¶ÁÈ¡64λÕûÊý"""
        if pos + 8 > len(data):
            raise Exception('read_int64: pos out of range')
        value = struct.unpack('<q', data[pos:pos+8])[0]
        return value, pos + 8
    
    @staticmethod
    def read_string(data, pos, length):
        """¶ÁÈ¡×Ö·û´®"""
        if pos + length > len(data):
            raise Exception('read_string: pos out of range')
        return data[pos:pos+length], pos + length
    
    @staticmethod
    def read_var_string(data, pos):
        """¶ÁÈ¡±ä³¤×Ö·û´® (³¤¶Èǰ׺)"""
        length, pos = PacketParser.read_byte(data, pos)
        return PacketParser.read_string(data, pos, length)
 
 
class LoginPacket:
    """µÇ¼Êý¾Ý°ü"""
 
    def __init__(self, data=None):
        self.head = ProtocolHead()
        self.group_id = 0
        self.server_id = 0
        self.magic_code = 0
 
        if data:
            self.from_bytes(data)
 
    def from_bytes(self, data):
        pos = 0
        self.head = ProtocolHead(data[pos:pos+2])
        pos += 2
 
        self.group_id, pos = PacketParser.read_uint32(data, pos)
        self.server_id, pos = PacketParser.read_uint32(data, pos)
        self.magic_code, pos = PacketParser.read_uint32(data, pos)
 
 
class LogoutPacket:
    """µÇ³öÊý¾Ý°ü"""
 
    def __init__(self, data=None):
        self.head = ProtocolHead()
        self.reserve = 0
 
        if data:
            self.from_bytes(data)
 
    def from_bytes(self, data):
        pos = 0
        self.head = ProtocolHead(data[pos:pos+2])
        pos += 2
 
        self.reserve, pos = PacketParser.read_byte(data, pos)
 
 
class EventSendPacket:
    """ʼþ·¢ËÍÊý¾Ý°ü"""
    
    def __init__(self, data=None):
        self.head = ProtocolHead()
        self.event_id = 0
        self.data_len = 0
        self.sz_data = ''
        self.time = 0
        self.data = ''
        
        if data:
            self.from_bytes(data)
    
    def from_bytes(self, data):
        pos = 0
        self.head = ProtocolHead(data[pos:pos+2])
        pos += 2
        
        self.event_id, pos = PacketParser.read_uint32(data, pos)
        self.data_len, pos = PacketParser.read_uint32(data, pos)
        self.sz_data, pos = PacketParser.read_string(data, pos, self.data_len)
        self.time, pos = PacketParser.read_int64(data, pos)
        self.data = data[pos:]
 
 
class EventStrSendPacket:
    """×Ö·û´®Ê¼þ·¢ËÍÊý¾Ý°ü"""
    
    def __init__(self, data=None):
        self.head = ProtocolHead()
        self.event_id_string_len = 0
        self.sz_event_id_string = ''
        self.data_len = 0
        self.sz_data = ''
        self.ext_len = 0
        self.ext = []
        self.data = ''
        
        if data:
            self.from_bytes(data)
    
    def from_bytes(self, data):
        pos = 0
        self.head = ProtocolHead(data[pos:pos+2])
        pos += 2
        
        self.event_id_string_len, pos = PacketParser.read_byte(data, pos)
        self.sz_event_id_string, pos = PacketParser.read_string(data, pos, self.event_id_string_len)
        self.data_len, pos = PacketParser.read_uint32(data, pos)
        self.sz_data, pos = PacketParser.read_string(data, pos, self.data_len)
        
        self.ext_len, pos = PacketParser.read_byte(data, pos)
        self.ext = []
        for i in range(self.ext_len):
            ext_value, pos = PacketParser.read_uint32(data, pos)
            self.ext.append(ext_value)
        
        self.data = data[pos:]
 
 
class HeartBeatPacket:
    """ÐÄÌøÊý¾Ý°ü"""
    
    def __init__(self, data=None):
        self.head = ProtocolHead()
        self.time = 0
        
        if data:
            self.from_bytes(data)
    
    def from_bytes(self, data):
        pos = 0
        self.head = ProtocolHead(data[pos:pos+2])
        pos += 2
        
        self.time, pos = PacketParser.read_uint32(data, pos)