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 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,27 +10,41 @@ 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:
|
||||||
|
try:
|
||||||
data = self.receive(self.socket)
|
data = self.receive(self.socket)
|
||||||
|
except socket_error:
|
||||||
|
self.quit()
|
||||||
|
else:
|
||||||
if data.lower() == "quit":
|
if data.lower() == "quit":
|
||||||
self.quit()
|
self.quit()
|
||||||
break
|
break
|
||||||
print(data)
|
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):
|
||||||
|
try:
|
||||||
self.socket.close()
|
self.socket.close()
|
||||||
print("Exit")
|
except socket_error:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
print("Disconnected")
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
60
server.py
60
server.py
|
@ -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,12 +9,18 @@ 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:
|
||||||
|
c, address = self.connect_client(self.socket)
|
||||||
|
except socket_error:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
name = self.client_name(c)
|
name = self.client_name(c)
|
||||||
if name:
|
if name:
|
||||||
self.clients[name] = c
|
self.clients[name] = c
|
||||||
|
@ -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,13 +49,11 @@ 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:
|
||||||
|
if not self.command(data, name):
|
||||||
Thread(target=self.broadcast, args=(f"{name}: {data}", name)).start()
|
Thread(target=self.broadcast, args=(f"{name}: {data}", name)).start()
|
||||||
|
|
||||||
def broadcast(self, message, author=None):
|
def broadcast(self, message, author=None):
|
||||||
|
@ -58,8 +63,47 @@ class Server(Socket):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
self.send(self.clients[i], message)
|
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
|
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__":
|
||||||
|
|
Reference in a new issue