From 36a0f245e497e35d2c5a2c340a6e65b80c96210b Mon Sep 17 00:00:00 2001 From: Richard Neumann Date: Sun, 21 Aug 2022 16:01:18 +0200 Subject: [PATCH] Fix fragmented packet reading --- rcon/source/client.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/rcon/source/client.py b/rcon/source/client.py index 110ee64..47de7e0 100644 --- a/rcon/source/client.py +++ b/rcon/source/client.py @@ -32,16 +32,7 @@ class Client(BaseClient, socket_type=SOCK_STREAM): def communicate(self, packet: Packet) -> Packet: """Send and receive a packet.""" self.send(packet) - - if len((response := self.read()).payload) < self.frag_threshold: - return response - - self.send(Packet.make_command(self.frag_detect_cmd)) - - while (successor := self.read()).id == response.id: - response += successor - - return response + return self.read() def send(self, packet: Packet) -> None: """Send a packet to the server.""" @@ -51,7 +42,17 @@ class Client(BaseClient, socket_type=SOCK_STREAM): def read(self) -> Packet: """Read a packet from the server.""" with self._socket.makefile('rb') as file: - return Packet.read(file) + response = Packet.read(file) + + if len(response.payload) < self.frag_threshold: + return response + + self.send(Packet.make_command(self.frag_detect_cmd)) + + while (successor := Packet.read(file)).id == response.id: + response += successor + + return response def login(self, passwd: str, *, encoding: str = 'utf-8') -> bool: """Perform a login."""