forked from watsy0007/ChatServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpstreampackage.py
More file actions
49 lines (35 loc) · 1.32 KB
/
Copy pathtcpstreampackage.py
File metadata and controls
49 lines (35 loc) · 1.32 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
# -*- coding:utf-8 -*-
__author__ = 'watsy'
import json
from protocol import Protocol
class TCPStreamPackage(object):
def __init__(self , callback):
super(TCPStreamPackage , self).__init__()
self.datas = ''
self._package_decode_callback = callback
def add(self, data):
#组包
if len(self.datas) == 0:
self.datas = data
else:
self.datas = "%s%s" % (self.datas , data)
#拆包
length = -1
length_index = self.datas.find('length\" :')
if length_index != -1:
length_end = self.datas.find(',' , length_index + 9 , length_index + 9 + 10)
if length_end == -1:
length_end = self.datas.find('}', length_index + 9 , length_index + 9 + 10)
if length_end != -1:
length = self.datas[length_index + 9 :length_end]
if length != -1:
length = int(length.strip())
if length != -1 and length <= len(self.datas):
package = self.datas[0:length]
package = Protocol.checkPackage(package)
if self._package_decode_callback:
self._package_decode_callback(package)
if len(self.datas) == length:
self.datas = ''
else:
self.datas = self.datas[length]