fixed type hint for packets
This commit is contained in:
parent
bd748ad349
commit
cc5a90405c
6 changed files with 26 additions and 12 deletions
|
@ -12,7 +12,6 @@ from source.utils.thread import in_pyglet_context
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from source.gui.window import Window
|
from source.gui.window import Window
|
||||||
from source.gui.scene import RoomJoin
|
|
||||||
|
|
||||||
|
|
||||||
class Client(StoppableThread):
|
class Client(StoppableThread):
|
||||||
|
@ -97,7 +96,7 @@ class Client(StoppableThread):
|
||||||
|
|
||||||
# paramètres & jeu
|
# paramètres & jeu
|
||||||
|
|
||||||
settings: Any = PacketSettings.from_connection(connection)
|
settings = PacketSettings.from_connection(connection)
|
||||||
PacketUsername(username=self.username).send_data_connection(connection)
|
PacketUsername(username=self.username).send_data_connection(connection)
|
||||||
enemy_username = PacketUsername.from_connection(connection).username
|
enemy_username = PacketUsername.from_connection(connection).username
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ def game_network(
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
data_type: Type["Packet"] = Packet.type_from_connection(connection)
|
data_type = Packet.type_from_connection(connection)
|
||||||
|
|
||||||
if data_type is None:
|
if data_type is None:
|
||||||
if thread.stopped: return # vérifie si le thread n'est pas censé s'arrêter
|
if thread.stopped: return # vérifie si le thread n'est pas censé s'arrêter
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
import socket
|
import socket
|
||||||
from abc import abstractmethod, ABC
|
from abc import abstractmethod, ABC
|
||||||
from inspect import isabstract
|
from inspect import isabstract
|
||||||
from typing import Type, Optional
|
from typing import Optional, TypeVar
|
||||||
|
|
||||||
|
|
||||||
|
T = TypeVar("T", bound="Packet")
|
||||||
|
|
||||||
|
|
||||||
class Packet(ABC):
|
class Packet(ABC):
|
||||||
|
@ -12,7 +15,7 @@ class Packet(ABC):
|
||||||
The to_bytes and from_connection method need to be defined.
|
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_header: bytes
|
||||||
packet_id: int = 0
|
packet_id: int = 0
|
||||||
|
@ -48,7 +51,7 @@ class Packet(ABC):
|
||||||
connection.send(self.to_bytes())
|
connection.send(self.to_bytes())
|
||||||
|
|
||||||
@classmethod
|
@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.
|
Get a subclass from its packet header.
|
||||||
:param packet_header: the header to find the corresponding subclass
|
:param packet_header: the header to find the corresponding subclass
|
||||||
|
@ -61,7 +64,7 @@ class Packet(ABC):
|
||||||
))
|
))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def type_from_connection(cls, connection: socket.socket) -> Optional[Type["Packet"]]:
|
def type_from_connection(cls, connection: socket.socket) -> Optional[T]:
|
||||||
try:
|
try:
|
||||||
packet_header = connection.recv(1)
|
packet_header = connection.recv(1)
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
|
@ -72,7 +75,7 @@ class Packet(ABC):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def from_connection(cls, connection: socket.socket) -> "Packet":
|
def from_connection(cls, connection: socket.socket) -> T:
|
||||||
"""
|
"""
|
||||||
Receive a packet from a socket.
|
Receive a packet from a socket.
|
||||||
:param connection: the socket where to get the data from
|
:param connection: the socket where to get the data from
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
from abc import ABC
|
from abc import ABC
|
||||||
import socket
|
import socket
|
||||||
|
from typing import TypeVar
|
||||||
|
|
||||||
from source.network.packet.abc import Packet
|
from source.network.packet.abc import Packet
|
||||||
|
|
||||||
|
|
||||||
|
T = TypeVar("T", bound="SignalPacket")
|
||||||
|
|
||||||
|
|
||||||
class SignalPacket(Packet, ABC):
|
class SignalPacket(Packet, ABC):
|
||||||
"""
|
"""
|
||||||
A packet that has for only usage to send a signal thanks to the type of the class.
|
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""
|
return b""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_connection(cls, connection: socket.socket) -> "SignalPacket":
|
def from_connection(cls, connection: socket.socket) -> T:
|
||||||
return cls()
|
return cls()
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
from typing import TypeVar
|
||||||
|
|
||||||
from source.network.packet.abc import Packet
|
from source.network.packet.abc import Packet
|
||||||
|
|
||||||
|
|
||||||
|
T = TypeVar("T", bound="SimplePacket")
|
||||||
|
|
||||||
|
|
||||||
class SimplePacket(Packet, ABC):
|
class SimplePacket(Packet, ABC):
|
||||||
"""
|
"""
|
||||||
A packet with a simple packet format.
|
A packet with a simple packet format.
|
||||||
|
@ -15,7 +19,7 @@ class SimplePacket(Packet, ABC):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def from_bytes(cls, data: bytes) -> "Packet":
|
def from_bytes(cls, data: bytes) -> T:
|
||||||
"""
|
"""
|
||||||
Convert a bytes object into a packet.
|
Convert a bytes object into a packet.
|
||||||
:param data: the data to convert into a packet. Should be "packet_size" long.
|
:param data: the data to convert into a packet. Should be "packet_size" long.
|
||||||
|
@ -24,7 +28,7 @@ class SimplePacket(Packet, ABC):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@classmethod
|
@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
|
# récupère la taille du packet en fonction du format et charge
|
||||||
# les données dans une nouvelle instance.
|
# les données dans une nouvelle instance.
|
||||||
packet_size: int = struct.calcsize(cls.packet_format)
|
packet_size: int = struct.calcsize(cls.packet_format)
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
from typing import TypeVar
|
||||||
|
|
||||||
from source.network.packet.abc import Packet
|
from source.network.packet.abc import Packet
|
||||||
|
|
||||||
|
|
||||||
|
T = TypeVar("T", bound="VariableLengthPacket")
|
||||||
|
|
||||||
|
|
||||||
class VariableLengthPacket(Packet, ABC):
|
class VariableLengthPacket(Packet, ABC):
|
||||||
"""
|
"""
|
||||||
A Packet that represent a single value that can be encoded with a variable length.
|
A Packet that represent a single value that can be encoded with a variable length.
|
||||||
|
@ -31,7 +35,7 @@ class VariableLengthPacket(Packet, ABC):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_connection(cls, connection: socket.socket) -> "Packet":
|
def from_connection(cls, connection: socket.socket) -> T:
|
||||||
data_len, *_ = struct.unpack(
|
data_len, *_ = struct.unpack(
|
||||||
cls.packet_format,
|
cls.packet_format,
|
||||||
connection.recv(struct.calcsize(cls.packet_format))
|
connection.recv(struct.calcsize(cls.packet_format))
|
||||||
|
|
Loading…
Reference in a new issue