Browse Source

Merge pull request #43 from ChillMouse/fix/issue-42

Fix infinity loop, when you receive ServerMessage. Add increment seq
master
Richard Neumann 11 months ago
committed by GitHub
parent
commit
0abe31533e
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 15
      rcon/battleye/client.py
  2. 8
      rcon/battleye/proto.py

15
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))

8
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):

Loading…
Cancel
Save