fixed type hint for packets

This commit is contained in:
Faraphel 2023-03-07 10:14:16 +01:00
parent bd748ad349
commit cc5a90405c
6 changed files with 26 additions and 12 deletions

View file

@ -12,7 +12,6 @@ from source.utils.thread import in_pyglet_context
if TYPE_CHECKING:
from source.gui.window import Window
from source.gui.scene import RoomJoin
class Client(StoppableThread):
@ -97,7 +96,7 @@ class Client(StoppableThread):
# paramètres & jeu
settings: Any = PacketSettings.from_connection(connection)
settings = PacketSettings.from_connection(connection)
PacketUsername(username=self.username).send_data_connection(connection)
enemy_username = PacketUsername.from_connection(connection).username

View file

@ -34,7 +34,7 @@ def game_network(
try:
while True:
data_type: Type["Packet"] = Packet.type_from_connection(connection)
data_type = Packet.type_from_connection(connection)
if data_type is None:
if thread.stopped: return # vérifie si le thread n'est pas censé s'arrêter

View file

@ -1,7 +1,10 @@
import socket
from abc import abstractmethod, ABC
from inspect import isabstract
from typing import Type, Optional
from typing import Optional, TypeVar
T = TypeVar("T", bound="Packet")
class Packet(ABC):
@ -12,7 +15,7 @@ class Packet(ABC):
The to_bytes and from_connection method need to be defined.
"""
packet_types: set[Type["Packet"]] = set()
packet_types: set[T] = set()
packet_header: bytes
packet_id: int = 0
@ -48,7 +51,7 @@ class Packet(ABC):
connection.send(self.to_bytes())
@classmethod
def type_from_header(cls, packet_header: bytes) -> Type["Packet"]:
def type_from_header(cls, packet_header: bytes) -> T:
"""
Get a subclass from its packet header.
:param packet_header: the header to find the corresponding subclass
@ -61,7 +64,7 @@ class Packet(ABC):
))
@classmethod
def type_from_connection(cls, connection: socket.socket) -> Optional[Type["Packet"]]:
def type_from_connection(cls, connection: socket.socket) -> Optional[T]:
try:
packet_header = connection.recv(1)
except socket.timeout:
@ -72,7 +75,7 @@ class Packet(ABC):
@classmethod
@abstractmethod
def from_connection(cls, connection: socket.socket) -> "Packet":
def from_connection(cls, connection: socket.socket) -> T:
"""
Receive a packet from a socket.
:param connection: the socket where to get the data from

View file

@ -1,9 +1,13 @@
from abc import ABC
import socket
from typing import TypeVar
from source.network.packet.abc import Packet
T = TypeVar("T", bound="SignalPacket")
class SignalPacket(Packet, ABC):
"""
A packet that has for only usage to send a signal thanks to the type of the class.
@ -14,5 +18,5 @@ class SignalPacket(Packet, ABC):
return b""
@classmethod
def from_connection(cls, connection: socket.socket) -> "SignalPacket":
def from_connection(cls, connection: socket.socket) -> T:
return cls()

View file

@ -1,10 +1,14 @@
import socket
import struct
from abc import ABC, abstractmethod
from typing import TypeVar
from source.network.packet.abc import Packet
T = TypeVar("T", bound="SimplePacket")
class SimplePacket(Packet, ABC):
"""
A packet with a simple packet format.
@ -15,7 +19,7 @@ class SimplePacket(Packet, ABC):
@classmethod
@abstractmethod
def from_bytes(cls, data: bytes) -> "Packet":
def from_bytes(cls, data: bytes) -> T:
"""
Convert a bytes object into a packet.
:param data: the data to convert into a packet. Should be "packet_size" long.
@ -24,7 +28,7 @@ class SimplePacket(Packet, ABC):
pass
@classmethod
def from_connection(cls, connection: socket.socket) -> "Packet":
def from_connection(cls, connection: socket.socket) -> T:
# récupère la taille du packet en fonction du format et charge
# les données dans une nouvelle instance.
packet_size: int = struct.calcsize(cls.packet_format)

View file

@ -1,10 +1,14 @@
import socket
import struct
from abc import ABC, abstractmethod
from typing import TypeVar
from source.network.packet.abc import Packet
T = TypeVar("T", bound="VariableLengthPacket")
class VariableLengthPacket(Packet, ABC):
"""
A Packet that represent a single value that can be encoded with a variable length.
@ -31,7 +35,7 @@ class VariableLengthPacket(Packet, ABC):
pass
@classmethod
def from_connection(cls, connection: socket.socket) -> "Packet":
def from_connection(cls, connection: socket.socket) -> T:
data_len, *_ = struct.unpack(
cls.packet_format,
connection.recv(struct.calcsize(cls.packet_format))