diff --git a/source/network/Client.py b/source/network/Client.py index d2bc71f..538b36f 100644 --- a/source/network/Client.py +++ b/source/network/Client.py @@ -1,30 +1,25 @@ import socket -from threading import Thread from typing import TYPE_CHECKING import pyglet.clock from source.gui import scene from source.network.SocketType import SocketType +from source.utils import StoppableThread if TYPE_CHECKING: from source.gui.window import Window -class Client(Thread): +class Client(StoppableThread): def __init__(self, window: "Window", username: str, ip_address: str, port: int = 52321, **kw): super().__init__(**kw) - self._stop = False - self.window = window self.username = username self.ip_address = ip_address self.port = port - def stop(self): - self._stop = True - def run(self) -> None: print("[Client] Thread démarré") diff --git a/source/network/Host.py b/source/network/Host.py index a7be490..8c0b329 100644 --- a/source/network/Host.py +++ b/source/network/Host.py @@ -1,29 +1,24 @@ import socket -from threading import Thread from typing import TYPE_CHECKING import pyglet from source.gui import scene from source.network.SocketType import SocketType +from source.utils import StoppableThread if TYPE_CHECKING: from source.gui.window import Window -class Host(Thread): +class Host(StoppableThread): def __init__(self, window: "Window", username: str, port: int = 52321, **kw): super().__init__(**kw) - self._stop = False - self.window = window self.username = username self.port = port - def stop(self) -> None: - self._stop = True - def run(self) -> None: print("[Serveur] Thread démarré") diff --git a/source/utils/__init__.py b/source/utils/__init__.py index ccc09a5..2e6154f 100644 --- a/source/utils/__init__.py +++ b/source/utils/__init__.py @@ -1,3 +1,4 @@ from .copy_array_offset import copy_array_offset from .in_bbox import in_bbox from .dict import dict_filter, dict_filter_prefix +from .thread import StoppableThread diff --git a/source/utils/thread.py b/source/utils/thread.py new file mode 100644 index 0000000..0c49ca7 --- /dev/null +++ b/source/utils/thread.py @@ -0,0 +1,15 @@ +from threading import Thread + + +class StoppableThread(Thread): + """ + A thread that can be stopped. + The run method need to check for the "self._stop" variable and return manually if it is true. + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self._stop = False + + def stop(self) -> None: + self._stop = True