sticked tpc package and uncontinued package

This commit is contained in:
2019-12-15 21:46:30 +08:00
parent 78271f6ff9
commit da8a56feaf
3 changed files with 39 additions and 25 deletions

10
mswp.py
View File

@@ -21,10 +21,13 @@ class Datapack:
heads += i.encode() + b': ' + self.head[i].encode() + b'\n'
self.encode_data = first_line + b'\n' + heads + b'\n' + self.body
def decode(self):
def decode(self, only_head=False):
index = self.encode_data.index(b'\n\n')
upper = self.encode_data[:index]
self.body = self.encode_data[index+2:]
if not only_head:
self.body = self.encode_data[index+2:]
else:
self.body = b''
upper = upper.decode()
heads = upper.split('\n')
first_line = heads.pop(0)
@@ -32,6 +35,9 @@ class Datapack:
for line in heads:
i, ii = line.split(': ')
self.head[i] = ii
if only_head:
return self.encode_data[index+2:]
def split_dp_data(data):
pass

View File

@@ -1,6 +1,7 @@
import threading
import socket
import queue
import time
from mswp import Datapack
from forwarder import receive_queues, send_queue
from config import jsondata
@@ -68,15 +69,34 @@ class Netrecv:
def process_connection(self, conn, addr):
print('Connection accpet %s' % str(addr))
data = b''
while True:
data = conn.recv(RECV_BUFF) # here needs to check whether the package is continued
if not data:
new_data = conn.recv(RECV_BUFF) # here needs to check whether the package is continued
if not new_data:
conn.close()
return
dp = Datapack(check_head=False)
dp.encode_data = data
dp.decode()
data += new_data
while True:
dp = Datapack(check_head=False)
dp.encode_data = data
try:
data = dp.decode(only_head=True)
except Exception as e:
print('Decode error %s: %s' % (type(e), str(e)))
print('Stop and start to receive more data')
break
length = int(dp.head['length'])
if length > len(data):
print('No enougth data, stop and start to receive')
break
dp.body = data[:length] # get the body
data = data[length:]
time.sleep(1)
dp.encode()
print(dp.body)
print('---------------\n'+dp.encode_data.decode()+'\n---------------')
if not data:
break
class Netlist: # contain net list and network controller
@@ -141,9 +161,5 @@ class Netlist: # contain net list and network controller
print('Sending %s error, data will be DROP!!' % data[0:10])
thread = threading.Thread(target=main, args=())
thread.start()

View File

@@ -2,21 +2,13 @@ import socket
data = '''post log msw/1.0
from: network
flag: aaaaa
length: 15
part_length: 7
num: 1
flag: abcdefgh
num: 1/2
lengt'''
hello, '''
data2 = '''h: 3
data2 = '''post log msw/1.0
from: network
flag: aaaaa
part_length: 8
length: 15
num: 2
network!'''
abc'''
data = data.encode()
data2 = data2.encode()