From bda05d3f8e05ec8f3d342739a4ad8681b64eeff9 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Thu, 23 Feb 2023 10:02:16 +0100 Subject: [PATCH] simplified packet with the struct module --- source/network/packet/PacketBombPlaced.py | 11 +++---- source/network/packet/PacketBombState.py | 19 ++++-------- source/network/packet/abc/Packet.py | 35 +++++++++++++++++------ 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/source/network/packet/PacketBombPlaced.py b/source/network/packet/PacketBombPlaced.py index 4d0e84b..cf23dc3 100644 --- a/source/network/packet/PacketBombPlaced.py +++ b/source/network/packet/PacketBombPlaced.py @@ -1,3 +1,5 @@ +import struct + from dataclasses import dataclass, field from source.network.packet.abc import Packet @@ -13,14 +15,13 @@ class PacketBombPlaced(Packet): position: Point2D = field() packet_size: int = 2 + packet_format: str = ">BB" def to_bytes(self): x, y = self.position - return x.to_bytes(1, "big") + y.to_bytes(1, "big") + return struct.pack(self.packet_format, x, y) @classmethod def from_bytes(cls, data: bytes): - return cls(position=( - int.from_bytes(data[0:1], "big"), - int.from_bytes(data[1:2], "big"), - )) + x, y = struct.unpack(cls.packet_format, data) + return cls(position=(x, y)) diff --git a/source/network/packet/PacketBombState.py b/source/network/packet/PacketBombState.py index a633087..59135aa 100644 --- a/source/network/packet/PacketBombState.py +++ b/source/network/packet/PacketBombState.py @@ -1,4 +1,4 @@ -import socket +import struct from dataclasses import dataclass, field from source.core.enums import BombState @@ -16,22 +16,13 @@ class PacketBombState(Packet): bomb_state: BombState = field() packet_size: int = 3 + packet_format: str = ">BBb" def to_bytes(self): x, y = self.position - - return ( - x.to_bytes(1, "big") + - y.to_bytes(1, "big") + - self.bomb_state.value.to_bytes(1, "big") - ) + return struct.pack(self.packet_format, x, y, self.bomb_state.value) @classmethod def from_bytes(cls, data: bytes): - return cls( - position=( - int.from_bytes(data[0:1], "big"), - int.from_bytes(data[1:2], "big"), - ), - bomb_state=BombState(int.from_bytes(data[2:3], "big")) - ) + x, y, bomb_state = struct.unpack(cls.packet_format, data) + return cls(position=(x, y), bomb_state=BombState(bomb_state)) diff --git a/source/network/packet/abc/Packet.py b/source/network/packet/abc/Packet.py index 68ad3f4..522013b 100644 --- a/source/network/packet/abc/Packet.py +++ b/source/network/packet/abc/Packet.py @@ -51,17 +51,36 @@ class Packet(ABC): connection.send(self.to_bytes()) @classmethod - def from_connection(cls, connection: socket.socket) -> Optional["Packet"]: + def cls_from_connection(cls, connection: socket.socket) -> Optional[Type["Packet"]]: + """ + Receive a packet type from a socket. + :param connection: the socket where to get the header from + :return: the packet class, or None if there was nothing in the socket to receive. + """ + packet_header: Optional[bytes] = None + try: + packet_header = connection.recv(1) + except socket.timeout: + pass + + return cls.cls_from_header(packet_header) if packet_header else None # ignore si le header est invalide + + @classmethod + def instance_from_connection(cls, connection: socket.socket) -> Optional["Packet"]: + """ + Receive a packet instance data from a socket. + :param connection: the socket where to get the data from + :return: the packet, or None if there was nothing in the socket to receive. + """ + return cls.from_bytes(connection.recv(cls.packet_size)) + + @classmethod + def from_connection(cls, connection: socket.socket) -> Optional[Type["Packet"]]: """ Receive a packet from a socket. :param connection: the socket where to get the data from :return: the packet, or None if there was nothing in the socket to receive. """ - packet_header: Optional[bytes] = None - try: packet_header = connection.recv(1) - except socket.timeout: pass + subcls = cls.cls_from_connection(connection) + return None if subcls is None else subcls.instance_from_connection(connection) - if not packet_header: return None # si le header du packet est invalide, ignore - subcls = cls.cls_from_header(packet_header) - - return subcls.from_bytes(connection.recv(subcls.packet_size))