Browse Source

make communicate functions thread safe

pull/14/head
Giles Knap 3 years ago
parent
commit
00c94b91a3
  1. 4
      rcon/battleye/client.py
  2. 5
      rcon/source/client.py

4
rcon/battleye/client.py

@ -2,6 +2,7 @@
from logging import getLogger from logging import getLogger
from socket import SOCK_DGRAM from socket import SOCK_DGRAM
from threading import Lock
from typing import Callable from typing import Callable
from rcon.battleye.proto import RESPONSE_TYPES from rcon.battleye.proto import RESPONSE_TYPES
@ -17,7 +18,7 @@ from rcon.exceptions import WrongPassword
__all__ = ['Client'] __all__ = ['Client']
lock = Lock()
MessageHandler = Callable[[ServerMessage], None] MessageHandler = Callable[[ServerMessage], None]
@ -55,6 +56,7 @@ class Client(BaseClient, socket_type=SOCK_DGRAM):
def communicate(self, request: Request) -> Response: def communicate(self, request: Request) -> Response:
"""Send a request and receive a response.""" """Send a request and receive a response."""
with lock:
with self._socket.makefile('wb') as file: with self._socket.makefile('wb') as file:
file.write(bytes(request)) file.write(bytes(request))

5
rcon/source/client.py

@ -1,14 +1,16 @@
"""Synchronous client.""" """Synchronous client."""
from socket import SOCK_STREAM from socket import SOCK_STREAM
from threading import Lock
from rcon.client import BaseClient from rcon.client import BaseClient
from rcon.exceptions import SessionTimeout, WrongPassword from rcon.exceptions import SessionTimeout, WrongPassword
from rcon.source.proto import Packet, Type from rcon.source.proto import Packet, Type
__all__ = ['Client'] __all__ = ['Client']
lock = Lock()
class Client(BaseClient, socket_type=SOCK_STREAM): class Client(BaseClient, socket_type=SOCK_STREAM):
"""An RCON client.""" """An RCON client."""
@ -31,6 +33,7 @@ class Client(BaseClient, socket_type=SOCK_STREAM):
def communicate(self, packet: Packet) -> Packet: def communicate(self, packet: Packet) -> Packet:
"""Send and receive a packet.""" """Send and receive a packet."""
with lock:
self.send(packet) self.send(packet)
return self.read() return self.read()

Loading…
Cancel
Save