Archived
1
0
Fork 0

improve exception system and disconnection error, add commands system

This commit is contained in:
Ethanell 2019-07-16 13:37:41 +02:00
parent 33fc1c9e2b
commit 734aa6f3fd
2 changed files with 86 additions and 27 deletions

View file

@ -1,6 +1,7 @@
from threading import Thread from threading import Thread
from SecureSocketService import Socket from SecureSocketService import Socket
from sys import exit from sys import exit
from socket import error as socket_error
class Client(Socket): class Client(Socket):
@ -9,28 +10,42 @@ class Client(Socket):
self.service_id = service_id self.service_id = service_id
print("Connecting...") print("Connecting...")
self.connect_server(host, port) self.connect_server(host, port)
self.receive_th = Thread(target=self.receive_server).start() Thread(target=self.receive_server).start()
self.send_th = Thread(target=self.send_server).start() Thread(target=self.send_input).start()
def receive_server(self): def receive_server(self):
while True: while True:
data = self.receive(self.socket) try:
if data.lower() == "quit": data = self.receive(self.socket)
except socket_error:
self.quit() self.quit()
break else:
print(data) 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: while True:
data = input() data = input()
self.send(self.socket, data) self.send_server(data)
if data.lower() == "quit": if data.lower() == "quit":
break break
def quit(self): def quit(self):
self.socket.close() try:
print("Exit") self.socket.close()
exit() except socket_error:
pass
finally:
print("Disconnected")
exit()
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -1,5 +1,6 @@
from threading import Thread from threading import Thread
from SecureSocketService import Socket from SecureSocketService import Socket
from socket import error as socket_error
class Server(Socket): class Server(Socket):
@ -8,17 +9,23 @@ class Server(Socket):
self.socket.bind((host, port)) self.socket.bind((host, port))
self.socket.listen(5) self.socket.listen(5)
self.service_id = service_id self.service_id = service_id
self.command_suffix = "!"
self.commands = {"help": self.command_help, "players list": self.command_players_list}
self.clients = dict() self.clients = dict()
Thread(target=self.connexion).start() Thread(target=self.connexion).start()
def connexion(self): def connexion(self):
while True: while True:
c, adress = self.connect_client(self.socket) try:
name = self.client_name(c) c, address = self.connect_client(self.socket)
if name: except socket_error:
self.clients[name] = c continue
self.broadcast(f"{name} is online !") else:
Thread(target=self.listen_client, args=(name,)).start() 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): def client_name(self, sock):
while True: while True:
@ -32,7 +39,7 @@ class Server(Socket):
name = None name = None
else: else:
break break
except: except socket_error:
name = None name = None
break break
return name return name
@ -42,24 +49,61 @@ class Server(Socket):
try: try:
data = self.receive(self.clients[name]) data = self.receive(self.clients[name])
assert data.lower() != "quit" assert data.lower() != "quit"
except: except (socket_error, AssertionError):
self.send(self.clients[name], "quit") self.client_quit(name)
self.clients[name].close()
del self.clients[name]
self.broadcast(f"{name} is offline !")
break break
else: 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) print(message)
for i in self.clients: for i in self.clients:
if i == author: if i == author:
continue continue
try: try:
self.send(self.clients[i], message) self.send(self.clients[i], message)
except: except socket_error:
pass 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__": if __name__ == "__main__":