simplified the code for handling error in the network module
This commit is contained in:
parent
8ef6d0d239
commit
fce1ed0abf
5 changed files with 185 additions and 190 deletions
|
@ -5,7 +5,7 @@ from typing import TYPE_CHECKING, Optional, Callable
|
||||||
|
|
||||||
from source.path import path_save
|
from source.path import path_save
|
||||||
from source.gui import scene
|
from source.gui import scene
|
||||||
from source.network import game_network
|
from source.network import game_network, handle_error
|
||||||
from source.network.packet import PacketUsername, PacketSettings, PacketHaveSaveBeenFound, PacketLoadOldSave
|
from source.network.packet import PacketUsername, PacketSettings, PacketHaveSaveBeenFound, PacketLoadOldSave
|
||||||
from source.utils import StoppableThread
|
from source.utils import StoppableThread
|
||||||
from source.utils.thread import in_pyglet_context
|
from source.utils.thread import in_pyglet_context
|
||||||
|
@ -36,8 +36,7 @@ class Client(StoppableThread):
|
||||||
self.port = port
|
self.port = port
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
print("[Client] Thread démarré")
|
try:
|
||||||
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as connection:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as connection:
|
||||||
try:
|
try:
|
||||||
connection.connect((self.ip_address, self.port))
|
connection.connect((self.ip_address, self.port))
|
||||||
|
@ -47,9 +46,7 @@ class Client(StoppableThread):
|
||||||
in_pyglet_context(self.on_connexion_refused)
|
in_pyglet_context(self.on_connexion_refused)
|
||||||
return
|
return
|
||||||
|
|
||||||
connection.settimeout(1) # défini le timeout à 1 secondes
|
connection.settimeout(1) # défini le timeout à 1 seconde
|
||||||
|
|
||||||
print(f"[Client] Connecté avec {connection}")
|
|
||||||
|
|
||||||
# sauvegarde
|
# sauvegarde
|
||||||
|
|
||||||
|
@ -87,14 +84,6 @@ class Client(StoppableThread):
|
||||||
break
|
break
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
if self.stopped: return
|
if self.stopped: return
|
||||||
except ConnectionResetError:
|
|
||||||
from source.gui.scene import GameError
|
|
||||||
in_pyglet_context(
|
|
||||||
self.window.set_scene,
|
|
||||||
GameError,
|
|
||||||
text="Perte de connexion avec l'adversaire"
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
if load_old_save:
|
if load_old_save:
|
||||||
|
|
||||||
|
@ -140,3 +129,6 @@ class Client(StoppableThread):
|
||||||
connection=connection,
|
connection=connection,
|
||||||
game_scene=game_scene,
|
game_scene=game_scene,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
except Exception as exception:
|
||||||
|
handle_error(self.window, exception)
|
||||||
|
|
|
@ -6,7 +6,7 @@ from threading import Condition
|
||||||
|
|
||||||
from source.path import path_save
|
from source.path import path_save
|
||||||
from source.gui import scene
|
from source.gui import scene
|
||||||
from source.network import game_network
|
from source.network import game_network, handle_error
|
||||||
from source.utils import StoppableThread
|
from source.utils import StoppableThread
|
||||||
from source.utils.thread import in_pyglet_context
|
from source.utils.thread import in_pyglet_context
|
||||||
from source.network.packet import PacketUsername, PacketLoadOldSave, PacketHaveSaveBeenFound
|
from source.network.packet import PacketUsername, PacketLoadOldSave, PacketHaveSaveBeenFound
|
||||||
|
@ -33,8 +33,7 @@ class Host(StoppableThread):
|
||||||
self.accept_load: bool = False
|
self.accept_load: bool = False
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
print("[Serveur] Thread démarré")
|
try:
|
||||||
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
|
||||||
server.bind(("", self.port)) # connecte le socket au port indiqué
|
server.bind(("", self.port)) # connecte le socket au port indiqué
|
||||||
|
|
||||||
|
@ -75,16 +74,7 @@ class Host(StoppableThread):
|
||||||
|
|
||||||
with self.condition_load: self.condition_load.wait() # attend que l'utilisateur choisisse l'option
|
with self.condition_load: self.condition_load.wait() # attend que l'utilisateur choisisse l'option
|
||||||
|
|
||||||
try:
|
|
||||||
PacketLoadOldSave(value=self.accept_load).send_data_connection(connection)
|
PacketLoadOldSave(value=self.accept_load).send_data_connection(connection)
|
||||||
except ConnectionResetError:
|
|
||||||
from source.gui.scene import GameError
|
|
||||||
in_pyglet_context(
|
|
||||||
self.window.set_scene,
|
|
||||||
GameError,
|
|
||||||
text="Perte de connexion avec l'adversaire"
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
if self.accept_load:
|
if self.accept_load:
|
||||||
|
|
||||||
|
@ -131,5 +121,5 @@ class Host(StoppableThread):
|
||||||
game_scene=game_scene
|
game_scene=game_scene
|
||||||
)
|
)
|
||||||
|
|
||||||
# TODO: englober les threads de try sur ConnectionResetError pour ramener au menu d'erreur directement
|
except Exception as exception:
|
||||||
# au lieu de le faire manuellement à chaque fois
|
handle_error(self.window, exception)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from .game_network import game_network
|
from .game_network import game_network
|
||||||
|
from .error import handle_error
|
||||||
|
|
||||||
from .Client import Client
|
from .Client import Client
|
||||||
from .Host import Host
|
from .Host import Host
|
||||||
|
|
25
source/network/error.py
Normal file
25
source/network/error.py
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import builtins
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from source.utils.thread import in_pyglet_context
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from source.gui.window import Window
|
||||||
|
|
||||||
|
|
||||||
|
def handle_error(window: "Window", exception: Exception):
|
||||||
|
message: str = "Erreur :\n"
|
||||||
|
|
||||||
|
match type(exception):
|
||||||
|
case builtins.ConnectionResetError:
|
||||||
|
message += "Perte de connexion avec l'adversaire."
|
||||||
|
case _:
|
||||||
|
message += str(exception)
|
||||||
|
|
||||||
|
from source.gui.scene import GameError
|
||||||
|
in_pyglet_context(
|
||||||
|
window.set_scene,
|
||||||
|
GameError,
|
||||||
|
text=message
|
||||||
|
)
|
|
@ -32,7 +32,6 @@ def game_network(
|
||||||
packet.PacketResponseSave: game_scene.network_on_response_save,
|
packet.PacketResponseSave: game_scene.network_on_response_save,
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
|
||||||
while True:
|
while True:
|
||||||
data_type = Packet.type_from_connection(connection)
|
data_type = Packet.type_from_connection(connection)
|
||||||
|
|
||||||
|
@ -47,15 +46,3 @@ def game_network(
|
||||||
) # Appelle la méthode.
|
) # Appelle la méthode.
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
except Exception as exception:
|
|
||||||
message: str = "Erreur :\n"
|
|
||||||
|
|
||||||
match type(exception):
|
|
||||||
case builtins.ConnectionResetError:
|
|
||||||
message += "Perte de connexion avec l'adversaire."
|
|
||||||
case _:
|
|
||||||
message += str(exception)
|
|
||||||
|
|
||||||
from source.gui.scene import GameError
|
|
||||||
in_pyglet_context(game_scene.window.set_scene, GameError, text=message)
|
|
||||||
|
|
Loading…
Reference in a new issue