Cooperation/Connection.py

changeset 6483
1c0bfbf78353
parent 6048
82ad8ec9548c
child 6484
8bed668f2a85
equal deleted inserted replaced
6482:15b0a1583cdd 6483:1c0bfbf78353
24 MaxBufferSize = 1024 * 1024 24 MaxBufferSize = 1024 * 1024
25 TransferTimeout = 30 * 1000 25 TransferTimeout = 30 * 1000
26 PongTimeout = 60 * 1000 26 PongTimeout = 60 * 1000
27 PingInterval = 5 * 1000 27 PingInterval = 5 * 1000
28 SeparatorToken = '|||' 28 SeparatorToken = '|||'
29 SeparatorToken_b = b'|||'
29 30
30 31
31 class Connection(QTcpSocket): 32 class Connection(QTcpSocket):
32 """ 33 """
33 Class representing a peer connection. 34 Class representing a peer connection.
291 self.abort() 292 self.abort()
292 return 0 293 return 0
293 294
294 while self.bytesAvailable() and self.__buffer.size() < maxSize: 295 while self.bytesAvailable() and self.__buffer.size() < maxSize:
295 self.__buffer.append(self.read(1)) 296 self.__buffer.append(self.read(1))
296 if self.__buffer.endsWith(SeparatorToken): 297 if self.__buffer.endsWith(SeparatorToken_b):
297 break 298 break
298 299
299 return self.__buffer.size() - numBytesBeforeRead 300 return self.__buffer.size() - numBytesBeforeRead
300 301
301 def __dataLengthForCurrentDataType(self): 302 def __dataLengthForCurrentDataType(self):
304 305
305 @return data length (integer) 306 @return data length (integer)
306 """ 307 """
307 if self.bytesAvailable() <= 0 or \ 308 if self.bytesAvailable() <= 0 or \
308 self.__readDataIntoBuffer() <= 0 or \ 309 self.__readDataIntoBuffer() <= 0 or \
309 not self.__buffer.endsWith(SeparatorToken): 310 not self.__buffer.endsWith(SeparatorToken_b):
310 return 0 311 return 0
311 312
312 self.__buffer.chop(len(SeparatorToken)) 313 self.__buffer.chop(len(SeparatorToken_b))
313 number = self.__buffer.toInt()[0] 314 number = self.__buffer.toInt()[0]
314 self.__buffer.clear() 315 self.__buffer.clear()
315 return number 316 return number
316 317
317 def __readProtocolHeader(self): 318 def __readProtocolHeader(self):
327 if self.__readDataIntoBuffer() <= 0: 328 if self.__readDataIntoBuffer() <= 0:
328 self.__transferTimerId = self.startTimer(TransferTimeout) 329 self.__transferTimerId = self.startTimer(TransferTimeout)
329 return False 330 return False
330 331
331 self.__buffer.chop(len(SeparatorToken)) 332 self.__buffer.chop(len(SeparatorToken))
332 if self.__buffer == Connection.ProtocolPing: 333 protocolHeader = str(self.__buffer, encoding="utf-8")
334 if protocolHeader == Connection.ProtocolPing:
333 self.__currentDataType = Connection.Ping 335 self.__currentDataType = Connection.Ping
334 elif self.__buffer == Connection.ProtocolPong: 336 elif protocolHeader == Connection.ProtocolPong:
335 self.__currentDataType = Connection.Pong 337 self.__currentDataType = Connection.Pong
336 elif self.__buffer == Connection.ProtocolMessage: 338 elif protocolHeader == Connection.ProtocolMessage:
337 self.__currentDataType = Connection.PlainText 339 self.__currentDataType = Connection.PlainText
338 elif self.__buffer == Connection.ProtocolGreeting: 340 elif protocolHeader == Connection.ProtocolGreeting:
339 self.__currentDataType = Connection.Greeting 341 self.__currentDataType = Connection.Greeting
340 elif self.__buffer == Connection.ProtocolGetParticipants: 342 elif protocolHeader == Connection.ProtocolGetParticipants:
341 self.__currentDataType = Connection.GetParticipants 343 self.__currentDataType = Connection.GetParticipants
342 elif self.__buffer == Connection.ProtocolParticipants: 344 elif protocolHeader == Connection.ProtocolParticipants:
343 self.__currentDataType = Connection.Participants 345 self.__currentDataType = Connection.Participants
344 elif self.__buffer == Connection.ProtocolEditor: 346 elif protocolHeader == Connection.ProtocolEditor:
345 self.__currentDataType = Connection.Editor 347 self.__currentDataType = Connection.Editor
346 else: 348 else:
347 self.__currentDataType = Connection.Undefined 349 self.__currentDataType = Connection.Undefined
348 self.abort() 350 self.abort()
349 return False 351 return False

eric ide

mercurial