added a StoppableThread class to simplify a bit the Host and Client thread
This commit is contained in:
parent
2f96ab14ad
commit
972327cdc7
4 changed files with 20 additions and 14 deletions
|
@ -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é")
|
||||
|
||||
|
|
|
@ -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é")
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
15
source/utils/thread.py
Normal file
15
source/utils/thread.py
Normal file
|
@ -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
|
Loading…
Reference in a new issue