Commit 75533bfb authored by Stanislav Bohm's avatar Stanislav Bohm

FIX fixed handling of large client downloads

parent d220b370
......@@ -14,30 +14,33 @@ class Connection(object):
self.socket.close()
def receive_message(self):
while True:
size = len(self.data)
if size >= 8:
msg_size = u64.unpack(self.data[:8])[0]
msg_size += 8
if size >= msg_size:
message = self.data[8:msg_size]
self.data = self.data[msg_size:]
return message
new_data = self.socket.recv(65536)
if not new_data:
raise Exception("Connection to server lost")
self.data += new_data
while len(self.data) < 8:
self.data += self.socket.recv(655360)
msg_size = u64.unpack(self.data[:8])[0]
if len(self.data) >= msg_size + 8:
msg_size = msg_size + 8
message = self.data[8:msg_size]
self.data = self.data[msg_size:]
return message
self.data = self.data[8:]
return self.read_data(msg_size)
def read_data(self, data_size):
result = bytes()
chunks = []
while True:
change = min(data_size, len(self.data))
result += self.data[:change]
self.data = self.data[change:]
data_size -= change
if data_size == 0:
return result
self.data = self.socket.recv(65536)
if data_size >= len(self.data):
chunks.append(self.data)
data_size -= len(self.data)
if data_size == 0:
self.data = b""
return b"".join(chunks)
else:
chunks.append(self.data[:data_size])
self.data = self.data[data_size:]
return b"".join(chunks)
self.data = self.socket.recv(655360)
if not self.data:
raise Exception("Connection to server lost")
def send_message(self, data):
data = u64.pack(len(data)) + data
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment