This commit is contained in:
2019-12-16 18:40:10 +08:00
parent bc7c19dc74
commit dac2e1bdb3
2 changed files with 54 additions and 10 deletions

View File

@@ -68,6 +68,49 @@ class Netrecv:
connection_thread.start() connection_thread.start()
def process_connection(self, conn, addr): def process_connection(self, conn, addr):
print('Connection accpet %s' % str(addr))
while True:
data = conn.recv(RECV_BUFF)
if not data:
conn.close()
print('return')
return
while data:
dp = Datapack(check_head=False)
dp.encode_data = data
try:
print(data)
data = dp.decode(only_head=True)
except Exception as e: # Not enough data
print('Decode error %s: %s' % (type(e), str(e)))
break
if dp.method == 'file':
pass
else:
length = int(dp.head['length'])
data_length = len(data)
if length == data_length:
print('length == data_length')
dp.body = data
data = b''
elif length > data_length:
print('length > data_length')
dp.body = data
while len(dp.body) < length:
data = conn.recv(RECV_BUFF)
if not data:
conn.close()
print('Connection close, dp drop')
return
need_length = len(data) - len(dp.body)
dp.body += data[:need_length]
data = data[need_length:]
dp.encode()
print('---------------\n' + dp.encode_data.decode() + '\n---------------')
def _process_connection(self, conn, addr):
print('Connection accpet %s' % str(addr)) print('Connection accpet %s' % str(addr))
data = b'' data = b''
need_data = False need_data = False
@@ -90,14 +133,15 @@ class Netrecv:
print('Stop and start to receive more data') print('Stop and start to receive more data')
break break
length = int(dp.head['length']) length = int(dp.head['length'])
if length > len(data): # check body data_length = len(data)
print('No enougth data, stop and start to receive') if length < data_length:
need_data = True dp.body = data[:length]
break data = data[length:]
elif length == len(data):
need_data = False need_data = False
dp.body = data[:length] # get the body continue
data = data[length:] elif length > data_length:
need_data = True
dp.encode() dp.encode()
print('---------------\n'+dp.encode_data.decode()+'\n---------------') print('---------------\n'+dp.encode_data.decode()+'\n---------------')

View File

@@ -1,7 +1,7 @@
import socket import socket
import time import time
data = '''file log msw/1.0 data = '''post log msw/1.0
from: network from: network
flag: abcdefgh flag: abcdefgh
num: 1/1 num: 1/1
@@ -11,9 +11,9 @@ data2 = '''h: 9
123''' 123'''
data3 ='''456789''' data3 ='''45678'''
data4 = '''post log msw/1.1 data4 = '''9post log msw/1.1
from: network from: network
flag: 12345678 flag: 12345678
num: 1/1 num: 1/1