bta-proxy/bta_proxy/debug.py

49 lines
1.4 KiB
Python
Raw Normal View History

2023-08-24 15:29:57 +03:00
from collections.abc import Iterable
from bta_proxy.packets.base import Packet
from bta_proxy.packets.packet50prechunk import Packet50PreChunk
from .datainputstream import DataInputStream
from typing import Generator, TextIO, TypeVar
T = TypeVar('T')
def chunks(gen: Iterable[T], size: int) -> Generator[list[T], None, None]:
bucket: list[T] = []
for item in gen:
bucket.append(item)
if len(bucket) >= size:
yield bucket
bucket.clear()
if bucket:
yield bucket
def debug_client(buffer: bytes, tmpfile: TextIO):
stream = DataInputStream(buffer)
while not stream.empty():
try:
packet = Packet.parse_packet(stream)
match type(packet)():
case Packet50PreChunk():
continue
case _:
print(packet)
except ValueError:
# print(f'[C:rest] {stream.end()}')
buf = stream.end()
print("[C]", len(buf), buf, file=tmpfile)
def debug_server(buffer: bytes, tmpfile: TextIO):
stream = DataInputStream(buffer)
while not stream.empty():
try:
packet = Packet.parse_packet(stream)
print(packet)
except ValueError:
# print(f'[S:rest] {stream.end()}')
buf = stream.end()
print("[S]", len(buf), buf, file=tmpfile)