From 734aa6f3fd662cdc0245ce90f8611bf9660e2600 Mon Sep 17 00:00:00 2001 From: flifloo Date: Tue, 16 Jul 2019 13:37:41 +0200 Subject: [PATCH] improve exception system and disconnection error, add commands system --- client.py | 37 +++++++++++++++++++-------- server.py | 76 +++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 86 insertions(+), 27 deletions(-) diff --git a/client.py b/client.py index c4ed55a..77baa2f 100644 --- a/client.py +++ b/client.py @@ -1,6 +1,7 @@ from threading import Thread from SecureSocketService import Socket from sys import exit +from socket import error as socket_error class Client(Socket): @@ -9,28 +10,42 @@ class Client(Socket): self.service_id = service_id print("Connecting...") self.connect_server(host, port) - self.receive_th = Thread(target=self.receive_server).start() - self.send_th = Thread(target=self.send_server).start() + Thread(target=self.receive_server).start() + Thread(target=self.send_input).start() def receive_server(self): while True: - data = self.receive(self.socket) - if data.lower() == "quit": + try: + data = self.receive(self.socket) + except socket_error: self.quit() - break - print(data) + else: + if data.lower() == "quit": + self.quit() + break + print(data) - def send_server(self): + def send_server(self, data): + try: + self.send(self.socket, data) + except socket_error: + self.quit() + + def send_input(self): while True: data = input() - self.send(self.socket, data) + self.send_server(data) if data.lower() == "quit": break def quit(self): - self.socket.close() - print("Exit") - exit() + try: + self.socket.close() + except socket_error: + pass + finally: + print("Disconnected") + exit() if __name__ == "__main__": diff --git a/server.py b/server.py index 6725304..c7f14f9 100644 --- a/server.py +++ b/server.py @@ -1,5 +1,6 @@ from threading import Thread from SecureSocketService import Socket +from socket import error as socket_error class Server(Socket): @@ -8,17 +9,23 @@ class Server(Socket): self.socket.bind((host, port)) self.socket.listen(5) self.service_id = service_id + self.command_suffix = "!" + self.commands = {"help": self.command_help, "players list": self.command_players_list} self.clients = dict() Thread(target=self.connexion).start() def connexion(self): while True: - c, adress = self.connect_client(self.socket) - name = self.client_name(c) - if name: - self.clients[name] = c - self.broadcast(f"{name} is online !") - Thread(target=self.listen_client, args=(name,)).start() + try: + c, address = self.connect_client(self.socket) + except socket_error: + continue + else: + name = self.client_name(c) + if name: + self.clients[name] = c + self.broadcast(f"{name} is online !") + Thread(target=self.listen_client, args=(name,)).start() def client_name(self, sock): while True: @@ -32,7 +39,7 @@ class Server(Socket): name = None else: break - except: + except socket_error: name = None break return name @@ -42,24 +49,61 @@ class Server(Socket): try: data = self.receive(self.clients[name]) assert data.lower() != "quit" - except: - self.send(self.clients[name], "quit") - self.clients[name].close() - del self.clients[name] - self.broadcast(f"{name} is offline !") + except (socket_error, AssertionError): + self.client_quit(name) break else: - Thread(target=self.broadcast, args=(f"{name}: {data}", name)).start() + if not self.command(data, name): + Thread(target=self.broadcast, args=(f"{name}: {data}", name)).start() - def broadcast(self, message, author = None): + def broadcast(self, message, author=None): print(message) for i in self.clients: if i == author: continue try: self.send(self.clients[i], message) - except: - pass + except socket_error: + continue + + def client_quit(self, name): + try: + self.send(self.clients[name], "quit") + self.clients[name].close() + except socket_error: + pass + finally: + del self.clients[name] + self.broadcast(f"{name} is offline !") + + def command(self, command, author): + command = command.lower() + if (command[:1] == self.command_suffix) and (command[1:] in self.commands): + command = command[1:] + elif command[:1] == self.command_suffix: + command = "help" + else: + return False + + try: + print(f"{author} use command {command}") + self.commands[command](author) + except socket_error: + self.client_quit(author) + finally: + return True + + def command_help(self, author): + message = "[Help]" + for i in self.commands: + message += "\n- " + i + self.send(self.clients[author], message) + + def command_players_list(self, author): + message = f"[Players list | {len(self.clients)} online]" + for i in self.clients: + message += "\n- " + i + self.send(self.clients[author], message) if __name__ == "__main__":