From 4c8c9abf40e21e73cb93dab2073fc56d95a41bd4 Mon Sep 17 00:00:00 2001 From: denis Date: Sun, 27 Jul 2025 01:01:31 +0700 Subject: [PATCH] Fix infinity loop, when you receive ServerMessage. Add increment seq number, when you send CommandRequest --- rcon/battleye/client.py | 15 ++++++++------- rcon/battleye/proto.py | 8 ++++---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/rcon/battleye/client.py b/rcon/battleye/client.py index f06e695..93ad6d7 100644 --- a/rcon/battleye/client.py +++ b/rcon/battleye/client.py @@ -33,6 +33,7 @@ def log_message(server_message: ServerMessage) -> None: class Client(BaseClient, socket_type=SOCK_DGRAM): """BattlEye RCon client.""" + seq_num: int = 0x00 def __init__( self, @@ -65,7 +66,6 @@ class Client(BaseClient, socket_type=SOCK_DGRAM): def receive_transaction(self): command_responses = [] login_response = None - seq = 0 while True: response = self.receive() @@ -76,8 +76,7 @@ class Client(BaseClient, socket_type=SOCK_DGRAM): if isinstance(response, CommandResponse): command_responses.append(response) - seq = response.seq - if len(command_responses) >= seq: + if len(command_responses) >= response.seq: break continue @@ -86,8 +85,8 @@ class Client(BaseClient, socket_type=SOCK_DGRAM): if login_response is not None: return login_response - - + break + return "".join( command_response.message for command_response in sorted(command_responses, key=lambda cr: cr.seq) @@ -98,15 +97,17 @@ class Client(BaseClient, socket_type=SOCK_DGRAM): with self._socket.makefile("wb") as file: file.write(bytes(request)) + if isinstance(request, CommandRequest): + self.seq_num = self.seq_num + 1 & 0xff + return self.receive_transaction() def login(self, passwd: str) -> bool: """Log-in the user.""" if not self.communicate(LoginRequest(passwd)).success: raise WrongPassword() - return True def run(self, command: str, *args: str) -> str: """Execute a command and return the text message.""" - return self.communicate(CommandRequest.from_command(command, *args)) + return self.communicate(CommandRequest.from_command(self.seq_num, command, *args)) diff --git a/rcon/battleye/proto.py b/rcon/battleye/proto.py index 3f8fc83..e759da5 100644 --- a/rcon/battleye/proto.py +++ b/rcon/battleye/proto.py @@ -120,14 +120,14 @@ class CommandRequest(NamedTuple): return Header.create(0x01, self.payload) @classmethod - def from_string(cls, command: str) -> CommandRequest: + def from_string(cls, seq: int, command: str) -> CommandRequest: """Create a command packet from the given string.""" - return cls(0x00, command) + return cls(seq, command) @classmethod - def from_command(cls, command: str, *args: str) -> CommandRequest: + def from_command(cls, seq: int, command: str, *args: str) -> CommandRequest: """Create a command packet from the command and arguments.""" - return cls.from_string(" ".join([command, *args])) + return cls.from_string(seq, " ".join([command, *args])) class CommandResponse(NamedTuple):