fix: correctly receive all chunks in recorder
This commit is contained in:
		| @@ -28,6 +28,7 @@ class RecorderClient(QObject): | ||||
|             socket.AF_INET, socket.SOCK_STREAM) | ||||
|         self.timer: Optional[QTimer] = None | ||||
|         self.connected: bool = False | ||||
|         self.buffer: bytes = b"" | ||||
|  | ||||
|     @pyqtSlot() | ||||
|     def start(self): | ||||
| @@ -40,27 +41,27 @@ class RecorderClient(QObject): | ||||
|         print("Connected to server") | ||||
|  | ||||
|     def poll_socket(self): | ||||
|         buffer: bytes = b"" | ||||
|         if not self.connected: | ||||
|             return | ||||
|  | ||||
|         try: | ||||
|             chunk: bytes = self.socket.recv(self.DATA_CHUNK_SIZE) | ||||
|             if not chunk: | ||||
|                 return | ||||
|             buffer += chunk | ||||
|  | ||||
|             while True: | ||||
|                 if len(buffer) < 4: | ||||
|                     break | ||||
|                 msg_len: int = struct.unpack(">I", buffer[:4])[0] | ||||
|                 msg_end: int = 4 + msg_len | ||||
|                 if len(buffer) < msg_end: | ||||
|                     break | ||||
|                 chunk: bytes = self.socket.recv(self.DATA_CHUNK_SIZE) | ||||
|                 if not chunk: | ||||
|                     return | ||||
|                 self.buffer += chunk | ||||
|  | ||||
|                 message: bytes = buffer[4:msg_end] | ||||
|                 buffer = buffer[msg_end:] | ||||
|                 self.on_message(message) | ||||
|                 while True: | ||||
|                     if len(self.buffer) < 4: | ||||
|                         break | ||||
|                     msg_len: int = struct.unpack(">I", self.buffer[:4])[0] | ||||
|                     msg_end: int = 4 + msg_len | ||||
|                     if len(self.buffer) < msg_end: | ||||
|                         break | ||||
|  | ||||
|                     message: bytes = self.buffer[4:msg_end] | ||||
|                     self.buffer = self.buffer[msg_end:] | ||||
|                     self.on_message(message) | ||||
|         except BlockingIOError: | ||||
|             pass | ||||
|         except Exception as e: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user