Browse Source

Add logging to a2sstream

async
Gabriel Huber 5 years ago
parent
commit
7887814324
  1. 9
      a2s/a2sstream.py

9
a2s/a2sstream.py

@ -1,6 +1,7 @@
import socket import socket
import bz2 import bz2
import io import io
import logging
from a2s.exceptions import BrokenMessageError from a2s.exceptions import BrokenMessageError
from a2s.byteio import ByteReader from a2s.byteio import ByteReader
@ -10,6 +11,8 @@ from a2s.byteio import ByteReader
HEADER_SIMPLE = b"\xFF\xFF\xFF\xFF" HEADER_SIMPLE = b"\xFF\xFF\xFF\xFF"
HEADER_MULTI = b"\xFE\xFF\xFF\xFF" HEADER_MULTI = b"\xFE\xFF\xFF\xFF"
logger = logging.getLogger("a2s")
class A2SFragment: class A2SFragment:
def __init__(self, message_id, fragment_count, fragment_id, mtu, def __init__(self, message_id, fragment_count, fragment_id, mtu,
decompressed_size=0, crc=0, payload=b""): decompressed_size=0, crc=0, payload=b""):
@ -61,6 +64,7 @@ class A2SStream:
header = packet[:4] header = packet[:4]
data = packet[4:] data = packet[4:]
if header == HEADER_SIMPLE: if header == HEADER_SIMPLE:
logger.debug("Received single packet: %r", data)
return data return data
elif header == HEADER_MULTI: elif header == HEADER_MULTI:
fragments = [decode_fragment(data)] fragments = [decode_fragment(data)]
@ -68,7 +72,10 @@ class A2SStream:
packet = self._socket.recv(4096) packet = self._socket.recv(4096)
fragments.append(decode_fragment(packet[4:])) fragments.append(decode_fragment(packet[4:]))
fragments.sort(key=lambda f: f.fragment_id) fragments.sort(key=lambda f: f.fragment_id)
return b"".join(fragment.payload for fragment in fragments) reassembled = b"".join(fragment.payload for fragment in fragments)
logger.debug("Received %s part packet with content: %r",
len(fragments), reassembled)
return reassembled
else: else:
raise BrokenMessageError( raise BrokenMessageError(
"Invalid packet header: " + repr(header)) "Invalid packet header: " + repr(header))

Loading…
Cancel
Save