-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.py
109 lines (88 loc) · 2.97 KB
/
Server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import socket
import threading
import sys
from time import sleep
from random import randint
HEADER = 64
HOST = socket.getaddrinfo(socket.gethostname(), None)[2][4][0]
PORT = 5050
connections = []
new_snakes = []
apples = [[randint(5, 110)*10, randint(5, 70)*10] for _ in range(10)]
def handle_client(conn, addr):
global new_snakes, score
nickname = receive(conn)
data = receive(conn)
threading.current_thread().nickname = nickname
threading.current_thread().conn = conn
# sending all players new player joined
send_to_all(data, conn)
# sending all snakes
new_snakes = []
for c in connections:
if c != conn:
send(c, "[ASK_POS]")
sleep(0.5)
send(conn, new_snakes[-1])
# sending all apples
for a in apples:
send(c, f"[APPLE+]|{','.join(map(str,a))}")
send_to_all(f"[CHAT]|{nickname} joined!")
while 1:
data = receive(conn)
prefix = data.split("|")[0]
if prefix == "[NEW]":
new_snakes.append(data)
elif prefix == "[APPLE-]":
apples.remove(list(map(int, data.split("|")[1].split(","))))
send_to_all(data, conn)
apples.append([randint(5, 100)*10, randint(5, 70)*10])
send_to_all(f"[APPLE+]|{','.join(map(str,apples[-1]))}")
elif prefix == "[CHAT]":
send_to_all(data)
else:
send_to_all(data, conn)
def handle_disconnect(args):
errors = [ConnectionResetError,
ConnectionAbortedError,
ConnectionError,
ConnectionRefusedError]
nickname = threading.current_thread().nickname
if args.exc_type in errors:
print(
f"Connection lost with client {nickname}. Err: {args.exc_type.__name__}")
connections.remove(threading.current_thread().conn)
send_to_all(f"[CHAT]|{nickname} left")
send_to_all(f"[LEFT]|{nickname}")
else:
sys.__excepthook__(
args.exc_type, args.exc_value, args.exc_traceback)
def send_to_all(msg, excluded=None):
for c in connections:
if c != excluded:
send(c, msg)
def receive(conn):
msg_len = conn.recv(HEADER)
msg_len = int(msg_len.decode())
msg = conn.recv(msg_len).decode()
return msg
def send(conn, msg):
msg_len = str(len(msg.encode()))
msg_len = msg_len.encode() + b" "*(HEADER-len(msg_len))
conn.send(msg_len)
conn.send(msg.encode())
def main():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind((HOST, PORT))
sock.setblocking(True)
sock.listen()
print(f"Server is listening on {(HOST,PORT)}")
while True:
conn, addr = sock.accept()
print(f"New client connected from {addr}")
connections.append(conn)
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
if __name__ == "__main__":
threading.excepthook = handle_disconnect
main()