Fix data read loop from connection
This commit is contained in:
parent
a87893199c
commit
9e1e7c3377
@ -22,7 +22,9 @@ class Connection:
|
|||||||
self._host = host
|
self._host = host
|
||||||
self._port = port
|
self._port = port
|
||||||
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self._recv_buffer = ""
|
self._recv_buffer = b""
|
||||||
|
|
||||||
|
self._socket.settimeout(3)
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
"""
|
"""
|
||||||
@ -49,21 +51,26 @@ class Connection:
|
|||||||
|
|
||||||
data = b""
|
data = b""
|
||||||
while True:
|
while True:
|
||||||
new_data = self._socket.recv(65536)
|
try:
|
||||||
if new_data is not None:
|
new_data = self._socket.recv(1024)
|
||||||
data += new_data
|
except TimeoutError:
|
||||||
else:
|
|
||||||
break
|
break
|
||||||
self._recv_buffer += data.decode("utf-8")
|
|
||||||
|
|
||||||
if "\0" not in self._recv_buffer:
|
data += new_data
|
||||||
|
|
||||||
|
if b"\0" in new_data:
|
||||||
|
break
|
||||||
|
|
||||||
|
self._recv_buffer += data
|
||||||
|
|
||||||
|
if b"\0" not in self._recv_buffer:
|
||||||
# no new messages
|
# no new messages
|
||||||
return None
|
return None
|
||||||
|
|
||||||
message, buffer = self._recv_buffer.split("\0", 1)
|
message, buffer = self._recv_buffer.split(b"\0", 1)
|
||||||
self._recv_buffer = buffer
|
self._recv_buffer = buffer
|
||||||
|
|
||||||
return message
|
return message.decode("utf-8")
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user