Browse Source

Fix all the bugs

async
Gabriel Huber 5 years ago
parent
commit
ffd705d3ab
  1. 6
      a2s/__init__.py
  2. 9
      a2s/a2sasync.py
  3. 2
      a2s/a2sstream.py
  4. 12
      a2s/info.py
  5. 8
      a2s/players.py
  6. 6
      a2s/rules.py

6
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

9
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()

2
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()

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

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

6
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:

Loading…
Cancel
Save