Skip to content

Commit 601f296

Browse files
committed
socket recv corrected
Python recv function returns up to the number of bytes passed. But by protocol the exact number of bytes must be read for the decoders to work. In c++ this was already fixed. Probability that this occurs is very low, especially on localhost.
1 parent 34a884d commit 601f296

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

thales_remote/connection.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,24 @@ def connectToTerm(
112112
# print("complete packet:" + str(registration_packet))
113113

114114
self._send_mutex.acquire()
115-
self._socket_handle.sendall(registration_packet)
115+
self.sendall(registration_packet)
116116
self._send_mutex.release()
117117

118118
time.sleep(0.8)
119119

120120
return True
121121

122+
def sendall(self, data: bytearray):
123+
return self._socket_handle.sendall(data)
124+
125+
def readall(self, numBytes: int) -> bytes:
126+
bytesToRead = numBytes
127+
readBytes = bytes()
128+
while bytesToRead > 0:
129+
readBytes += self._socket_handle.recv(bytesToRead)
130+
bytesToRead = numBytes - len(readBytes)
131+
return readBytes
132+
122133
def getConnectionName(self) -> str:
123134
"""
124135
get the connection name
@@ -198,7 +209,7 @@ def sendTelegram(
198209
# print(f"payload: {data}")
199210
# print("complete packet:" + str(packet))
200211

201-
self._socket_handle.sendall(packet)
212+
self.sendall(packet)
202213
finally:
203214
self._socket_handle.settimeout(None)
204215
self._send_mutex.release()
@@ -315,14 +326,12 @@ def _readTelegramFromSocket(self) -> tuple[Optional[str], bytearray]:
315326
exception in the threads waiting at the queue.
316327
"""
317328
try:
318-
header_len: bytes = self._socket_handle.recv(2)
319-
header_type_bytes: bytes = self._socket_handle.recv(1)
329+
header_len: bytes = self.readall(2)
330+
header_type_bytes: bytes = self.readall(1)
320331
header_type: str = struct.unpack("<B", header_type_bytes)[
321332
0
322333
] # actually a character, not a str
323-
incoming_packet: int = self._socket_handle.recv(
324-
struct.unpack("<H", header_len)[0]
325-
)
334+
incoming_packet: int = self.readall(struct.unpack("<H", header_len)[0])
326335

327336
# print("\n" + str(datetime.now().time()) + " read:")
328337
# print(f"payload_length: {struct.unpack('<H', header_len)[0]} message_type: {header_type}")

0 commit comments

Comments
 (0)