From 4b00491e9b62319506e75fcd289a18ce2c5f655c Mon Sep 17 00:00:00 2001 From: Richard Neumann Date: Fri, 25 Dec 2020 14:18:40 +0100 Subject: [PATCH] Refactored to using socket files for communication. --- rcon/proto.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/rcon/proto.py b/rcon/proto.py index aae0c98..e116502 100644 --- a/rcon/proto.py +++ b/rcon/proto.py @@ -147,10 +147,14 @@ class Client: def communicate(self, packet: Packet) -> Packet: """Sends and receives a packet.""" - self._socket.send(bytes(packet)) - header = self._socket.recv(4) - length = int.from_bytes(header, 'little') - payload = self._socket.recv(length) + with self._socket.makefile('wb') as file: + file.write(bytes(packet)) + + with self._socket.makefile('rb') as file: + header = file.read(4) + length = int.from_bytes(header, 'little') + payload = file.read(length) + response = Packet.from_bytes(payload) if response.id == packet.id: