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

View File

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

View File

@@ -1,6 +1,7 @@
import threading import threading
import socket import socket
import queue import queue
import time
from mswp import Datapack from mswp import Datapack
from forwarder import receive_queues, send_queue from forwarder import receive_queues, send_queue
from config import jsondata from config import jsondata
@@ -68,15 +69,34 @@ class Netrecv:
def process_connection(self, conn, addr): def process_connection(self, conn, addr):
print('Connection accpet %s' % str(addr)) print('Connection accpet %s' % str(addr))
data = b''
while True: while True:
data = conn.recv(RECV_BUFF) # here needs to check whether the package is continued new_data = conn.recv(RECV_BUFF) # here needs to check whether the package is continued
if not data: if not new_data:
conn.close() conn.close()
return return
data += new_data
while True:
dp = Datapack(check_head=False) dp = Datapack(check_head=False)
dp.encode_data = data dp.encode_data = data
dp.decode() 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 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]) print('Sending %s error, data will be DROP!!' % data[0:10])
thread = threading.Thread(target=main, args=()) thread = threading.Thread(target=main, args=())
thread.start() thread.start()

View File

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