diff --git a/a2s/__init__.py b/a2s/__init__.py index 332331e..0251452 100644 --- a/a2s/__init__.py +++ b/a2s/__init__.py @@ -1,5 +1,5 @@ from a2s.exceptions import BrokenMessageError, BufferExhaustedError -from a2s.info import info, SourceInfo, GoldSrcInfo -from a2s.players import players, Player -from a2s.rules import rules +from a2s.info import info, ainfo, SourceInfo, GoldSrcInfo +from a2s.players import players, aplayers, Player +from a2s.rules import rules, arules diff --git a/a2s/a2sasync.py b/a2s/a2sasync.py index 2356a7e..7c6c8bc 100644 --- a/a2s/a2sasync.py +++ b/a2s/a2sasync.py @@ -35,7 +35,7 @@ class A2SProtocol(asyncio.DatagramProtocol): reassembled = b"".join( fragment.payload for fragment in self.fragment_buf) logger.debug("Received %s part packet with content: %r", - len(fragments), reassembled) + len(self.fragment_buf), reassembled) self.recv_queue.put_nowait(reassembled) self.fragment_buf = [] else: @@ -64,7 +64,8 @@ class A2SStreamAsync: @classmethod async def create(cls, address, timeout): - transport, protocol = await asyncio.create_datagram_endpoint( + loop = asyncio.get_running_loop() + transport, protocol = await loop.create_datagram_endpoint( lambda: A2SProtocol(), remote_addr=address) return cls(transport, protocol, timeout) @@ -76,7 +77,7 @@ class A2SStreamAsync: queue_task = asyncio.create_task(self.protocol.recv_queue.get()) error_task = asyncio.create_task(self.protocol.error_event.wait()) done, pending = await asyncio.wait({queue_task, error_task}, - timeout=self.timeout, return_when=FIRST_COMPLETED) + timeout=self.timeout, return_when=asyncio.FIRST_COMPLETED) for task in pending: task.cancel() if error_task in done: @@ -86,7 +87,7 @@ class A2SStreamAsync: return queue_task.result() - async def request(payload): + async def request(self, payload): self.send(payload) return await self.recv() diff --git a/a2s/a2sstream.py b/a2s/a2sstream.py index 313d9a0..5d8c593 100644 --- a/a2s/a2sstream.py +++ b/a2s/a2sstream.py @@ -45,7 +45,7 @@ class A2SStream: raise BrokenMessageError( "Invalid packet header: " + repr(header)) - def request(payload): + def request(self, payload): self.send(payload) return self.recv() diff --git a/a2s/info.py b/a2s/info.py index a810068..bc94ef1 100644 --- a/a2s/info.py +++ b/a2s/info.py @@ -245,7 +245,7 @@ def parse_goldsrc(reader): return resp -def info_response(resp_data): +def info_response(resp_data, ping, encoding): reader = ByteReader( io.BytesIO(resp_data), endian="<", encoding=encoding) @@ -258,7 +258,7 @@ def info_response(resp_data): raise BrokenMessageError( "Invalid response type: " + str(response_type)) - resp.ping = recv_time - send_time + resp.ping = ping return resp def info(address, timeout=DEFAULT_TIMEOUT, encoding=DEFAULT_ENCODING): @@ -267,14 +267,16 @@ def info(address, timeout=DEFAULT_TIMEOUT, encoding=DEFAULT_ENCODING): resp_data = conn.request(b"\x54Source Engine Query\0") recv_time = time.monotonic() conn.close() + ping = recv_time - send_time - return info_response(resp_data) + return info_response(resp_data, ping, encoding) -async def info_async(address, timeout=DEFAULT_TIMEOUT, encoding=DEFAULT_ENCODING): +async def ainfo(address, timeout=DEFAULT_TIMEOUT, encoding=DEFAULT_ENCODING): conn = await A2SStreamAsync.create(address, timeout) send_time = time.monotonic() resp_data = await conn.request(b"\x54Source Engine Query\0") recv_time = time.monotonic() conn.close() + ping = recv_time - send_time - return info_response(resp_data) + return info_response(resp_data, ping, encoding) diff --git a/a2s/players.py b/a2s/players.py index 49d0634..06ba0d6 100644 --- a/a2s/players.py +++ b/a2s/players.py @@ -58,7 +58,7 @@ def players_request(conn, encoding, challenge=0, retries=0): raise BrokenMessageError( "Server keeps sending challenge responses") challenge = reader.read_uint32() - return players_impl( + return players_request( conn, encoding, challenge, retries + 1) if response_type != A2S_PLAYER_RESPONSE: @@ -67,8 +67,8 @@ def players_request(conn, encoding, challenge=0, retries=0): return reader -async def players_async(address, timeout=DEFAULT_TIMEOUT, encoding=DEFAULT_ENCODING): - conn = await A2SStream.create(address, timeout) +async def aplayers(address, timeout=DEFAULT_TIMEOUT, encoding=DEFAULT_ENCODING): + conn = await A2SStreamAsync.create(address, timeout) reader = await players_request_async(conn, encoding) conn.close() return players_response(reader) @@ -84,7 +84,7 @@ async def players_request_async(conn, encoding, challenge=0, retries=0): raise BrokenMessageError( "Server keeps sending challenge responses") challenge = reader.read_uint32() - return await players_impl( + return await players_request_async( conn, encoding, challenge, retries + 1) if response_type != A2S_PLAYER_RESPONSE: diff --git a/a2s/rules.py b/a2s/rules.py index fad10d5..bda2011 100644 --- a/a2s/rules.py +++ b/a2s/rules.py @@ -58,14 +58,14 @@ def rules_request(conn, encoding, challenge=0, retries=0): return reader -async def rules(address, timeout=DEFAULT_TIMEOUT, encoding=DEFAULT_ENCODING): +async def arules(address, timeout=DEFAULT_TIMEOUT, encoding=DEFAULT_ENCODING): conn = await A2SStreamAsync.create(address, timeout) reader = await rules_request_async(conn, encoding) conn.close() return rules_response(reader) async def rules_request_async(conn, encoding, challenge=0, retries=0): - resp_data = conn.request(b"\x56" + challenge.to_bytes(4, "little")) + resp_data = await conn.request(b"\x56" + challenge.to_bytes(4, "little")) reader = ByteReader( io.BytesIO(resp_data), endian="<", encoding=encoding) @@ -78,7 +78,7 @@ async def rules_request_async(conn, encoding, challenge=0, retries=0): raise BrokenMessageError( "Server keeps sending challenge responses") challenge = reader.read_uint32() - return await rules_request( + return await rules_request_async( conn, encoding, challenge, retries + 1) if response_type != A2S_RULES_RESPONSE: