improve exception system and disconnection error, add commands system
This commit is contained in:
parent
33fc1c9e2b
commit
734aa6f3fd
2 changed files with 86 additions and 27 deletions
25
client.py
25
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,27 +10,41 @@ 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:
|
||||
try:
|
||||
data = self.receive(self.socket)
|
||||
except socket_error:
|
||||
self.quit()
|
||||
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):
|
||||
try:
|
||||
self.socket.close()
|
||||
print("Exit")
|
||||
except socket_error:
|
||||
pass
|
||||
finally:
|
||||
print("Disconnected")
|
||||
exit()
|
||||
|
||||
|
||||
|
|
60
server.py
60
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,12 +9,18 @@ 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)
|
||||
try:
|
||||
c, address = self.connect_client(self.socket)
|
||||
except socket_error:
|
||||
continue
|
||||
else:
|
||||
name = self.client_name(c)
|
||||
if name:
|
||||
self.clients[name] = c
|
||||
|
@ -32,7 +39,7 @@ class Server(Socket):
|
|||
name = None
|
||||
else:
|
||||
break
|
||||
except:
|
||||
except socket_error:
|
||||
name = None
|
||||
break
|
||||
return name
|
||||
|
@ -42,13 +49,11 @@ 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:
|
||||
if not self.command(data, name):
|
||||
Thread(target=self.broadcast, args=(f"{name}: {data}", name)).start()
|
||||
|
||||
def broadcast(self, message, author=None):
|
||||
|
@ -58,8 +63,47 @@ class Server(Socket):
|
|||
continue
|
||||
try:
|
||||
self.send(self.clients[i], message)
|
||||
except:
|
||||
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__":
|
||||
|
|
Reference in a new issue