-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.py
More file actions
212 lines (180 loc) · 6.11 KB
/
protocol.py
File metadata and controls
212 lines (180 loc) · 6.11 KB
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
#coding=utf-8
import struct
import hessian2
import network as nw
MAGIC_NUM = 'BT'
HEADER_LENGTH = 8
BT_VERSION = 1
FLAG_REQ = 0x01
'''
Header Format :
0 8 16 24 32
+--------+--------+--------+--------+
| Magic Number | Status | Flag |
+--------+--------+--------+--------+
| Data Length |
+--------+--------+--------+--------+
Status Format :
0 - 2 : Protocol Version (1 - 7)
3 - 7 : Status Code (0 - 31)
Flag Format :
0 :
1 :
'''
class BTRequest(object) :
def __init__(self) :
pass
class BTResponse(object) :
def __init__(self) :
pass
class BTCommand(object) :
def process(self, request, response) :
pass
class BTNetHandler(nw.IoHandler) :
def __init__(self, service) :
self.service = service
def connected(self, ioSession) :
if 'request' in ioSession.attr :
request = ioSession.attr['request']
del ioSession.attr['request']
addrKey = ioSession.attr['addrKey']
self.service.sessionMap[addrKey] = ioSession
ioSession.write(encodeRequest(request))
ioSession.read()
else :
ioSession.read()
def closed(self, ioSession) :
print 'session', ioSession.remoteAddr(), ioSession.localAddr(), 'closed'
if 'addrKey' in ioSession.attr and ioSession.attr['addrKey'] in self.service.sessionMap :
del self.service.sessionMap[ioSession.attr['addrKey']]
def exceptionCaught(self, ioSession, e) :
ioSession.close()
def __readData(self, ioSession, obj, data) :
if len(data) < obj.dataLength :
ioSession.attr['header'] = obj
ioSession.attr['dataBuf'] = data
return
obj.data = data
if isinstance(obj, BTRequest) :
self.service.processRequest(ioSession, obj)
elif 'callback' in ioSession.attr :
decodeResponse(obj)
ioSession.attr['callback'](obj)
def __readHeader(self, ioSession, msg) :
if len(msg) < HEADER_LENGTH :
ioSession.attr['headerBuf'] = msg
else :
obj = decodeHeader(msg[:HEADER_LENGTH])
data = msg[HEADER_LENGTH:]
self.__readData(ioSession, obj, data)
def msgRecv(self, ioSession, msg) :
if 'headerBuf' in ioSession.attr :
msg = ioSession.attr['headerBuf'] + msg
del ioSession.attr['headerBuf']
self.__readHeader(ioSession, msg)
elif 'dataBuf' in ioSession.attr :
data = ioSession.attr['dataBuf'] + msg
del ioSession.attr['dataBuf']
header = ioSession.attr['header']
del ioSession.attr['header']
self.__readData(ioSession, header, data)
else :
self.__readHeader(ioSession, msg)
def msgSent(self, ioSession, msg) :
pass
class BTNetService(object) :
def __init__(self) :
self.ioService = nw.IoService()
self.ioService.setHandler(BTNetHandler(self))
self.sessionMap = {}
self.commands = {}
def addCmd(self, name, command) :
self.commands[name] = command
def listen(self, port, host = '') :
self.ioService.listen(port, host)
def sendRequest(self, addr, cmd, callback, **param) :
request = self.createRequest(cmd, **param)
addrKey = str(addr[0]) + ':' + str(addr[1])
if addrKey in self.sessionMap :
session = self.sessionMap[addrKey]
session.attr['callback'] = callback
session.write(encodeRequest(request))
else :
self.ioService.connect(addr, request = request, callback = callback, addrKey = addrKey)
def processRequest(self, session, request) :
decodeRequest(request)
if request.cmd in self.commands :
cmd = self.commands[request.cmd]
response = self.createResponse()
cmd.process(request, response)
session.write(encodeResponse(response))
def createRequest(self, cmd, **param) :
request = BTRequest()
request.cmd = cmd
request.param = param
request.status = 0
request.flag = FLAG_REQ
return request
def createResponse(self) :
response = BTResponse()
response.status = 0
response.flag = 0
return response
def run(self) :
self.ioService.run()
def addTimerEvent(self, callback, data, expires) :
self.ioService.addTimerEvent(callback, data, expires)
##encode and decode
def encodeRequest(request) :
header = MAGIC_NUM
status = BT_VERSION << 5
header += chr(status)
header += chr(request.flag | FLAG_REQ)
out = hessian2.Hessian2Output()
out.writeObject(request.cmd)
out.writeObject(request.param)
data = out.getByteString()
dataLength = len(data)
header += struct.pack('>I', dataLength)
return header + data
def decodeRequest(request) :
hInput = hessian2.Hessian2Input(request.data)
request.cmd = hInput.readObject()
request.param = hInput.readObject()
def encodeResponse(response) :
header = MAGIC_NUM
status = (BT_VERSION << 5) + (response.status & 0x1F)
header += chr(status)
header += chr(response.flag)
out = hessian2.Hessian2Output()
if response.status == 0 :
out.writeObject(response.result)
else :
out.writeObject(response.error)
data = out.getByteString()
dataLength = len(data)
header += struct.pack('>I', dataLength)
return header + data
def decodeResponse(response) :
hInput = hessian2.Hessian2Input(response.data)
if response.status == 0 :
response.result = hInput.readObject()
else :
response.error = hInput.readObject()
def decodeHeader(header) :
if header[:2] != MAGIC_NUM :
return None
status = ord(header[2])
version = status >> 5
status = status & 0x1F
flag = ord(header[3])
dataLength = struct.unpack('>I', header[4:8])[0]
if flag & FLAG_REQ :
obj = BTRequest()
else :
obj = BTResponse()
obj.status = status
obj.version = version
obj.flag = flag
obj.dataLength = dataLength
return obj