diff --git a/mswp.py b/mswp.py index 9cb999a..fb7cf22 100644 --- a/mswp.py +++ b/mswp.py @@ -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 diff --git a/plugins/net.py b/plugins/net.py index 2026e71..7d113ee 100644 --- a/plugins/net.py +++ b/plugins/net.py @@ -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() diff --git a/test_file.py b/test_file.py index 6ff2750..564a3a3 100644 --- a/test_file.py +++ b/test_file.py @@ -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()