forked from ARYANTECH123/pythonRaycaster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_network.py
More file actions
193 lines (150 loc) · 5.09 KB
/
server_network.py
File metadata and controls
193 lines (150 loc) · 5.09 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import socket
import threading
import json
import struct
import sys
import os
import signal
from logger import get_logger
from raycaster import Map
log = get_logger(__name__)
clients = {} # conn: player_id
players_state = {} # player_id: {px, py, pa}
shutdown_event = threading.Event()
# === Helper functions ===
def send_message(conn, message_dict):
message = json.dumps(message_dict).encode()
length = struct.pack('!I', len(message))
conn.sendall(length + message)
def recv_exact(sock, n):
data = b''
while len(data) < n:
packet = sock.recv(n - len(data))
if not packet:
return None
data += packet
return data
def broadcast():
log.debug(f"Broadcasting: {players_state}")
for conn in list(clients.keys()):
try:
send_message(conn, players_state)
except Exception as e:
log.error(f"Broadcast error: {e}")
conn.close()
clients.pop(conn, None)
def handle_client(conn, addr, player_id, map_data):
log.info(f"New connection from {addr}")
clients[conn] = player_id
try:
# Send init_id
send_message(conn, {"init_id": player_id})
log.info(f"Sent init_id to {addr}")
# Send map data
send_message(conn, {"map_data": map_data})
log.info(f"Sent map_data to {addr}")
# Wait for ACK before broadcasting
raw_len = recv_exact(conn, 4)
if not raw_len:
log.info(f"No ACK received from {addr}")
return
msg_len = struct.unpack('!I', raw_len)[0]
data = recv_exact(conn, msg_len)
try:
ack_msg = json.loads(data.decode())
except:
ack_msg = {}
log.warning(f"No valid ACK from {addr}")
if 'ack' not in ack_msg:
log.warning(f"Invalid ACK from {addr}")
return
log.info(f"Received ACK from {addr}")
broadcast()
# Main receive loop
while not shutdown_event.is_set():
raw_len = recv_exact(conn, 4)
if not raw_len:
break
msg_len = struct.unpack('!I', raw_len)[0]
data = recv_exact(conn, msg_len)
message = json.loads(data.decode())
players_state[player_id] = message
broadcast()
except ConnectionAbortedError:
log.info(f"Connection aborted: {addr}")
except ConnectionResetError:
log.info(f"Connexion reset by client {addr}")
except Exception as e:
log.warning(f"Client error: {e}")
finally:
log.info(f"Connection lost: {addr}")
conn.close()
clients.pop(conn, None)
players_state.pop(player_id, None)
broadcast()
def choose_map():
log.info("Choosing map...")
MAPS_DIRECTORY = 'maps/'
# List all files
files = [f for f in os.listdir(MAPS_DIRECTORY) if os.path.isfile(os.path.join(MAPS_DIRECTORY, f))]
if not files:
print("No map files found!")
sys.exit(1)
print("Available maps:")
for idx, filename in enumerate(files):
print(f"{idx + 1}. {filename}")
while True:
try:
choice = int(input("Choose a map by number: ")) - 1
if 0 <= choice < len(files):
chosen_map = files[choice]
break
else:
print("Invalid choice.")
except ValueError:
print("Enter a valid number.")
log.info(f"Loading map: {chosen_map}")
return Map.load_from_file(os.path.join(MAPS_DIRECTORY, chosen_map)).map_to_dict()
def start_server(host='127.0.0.1', port=5555):
log.info("Starting server...")
map_data = choose_map()
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((host, port))
server.listen()
server.settimeout(0.5) # Periodically check for shutdown
log.info(f"Listening on {host}:{port}")
next_player_id = 1
threads = []
# Signal handler to cleanly shutdown
def signal_handler(sig, frame):
log.info("Shutdown signal received.")
shutdown_event.set()
signal.signal(signal.SIGINT, signal_handler)
try:
while not shutdown_event.is_set():
try:
conn, addr = server.accept()
except socket.timeout:
continue
except OSError:
break # Socket closed
log.info(f"Accepted connection from {addr}")
player_id = next_player_id
next_player_id += 1
t = threading.Thread(target=handle_client, args=(conn, addr, player_id, map_data))
t.start()
threads.append(t)
except Exception as e:
log.error(f"Error in server loop: {e}")
finally:
log.info("Closing server socket...")
server.close()
log.info("Waiting for client threads to exit...")
shutdown_event.set() # Inform all threads to shut down
for t in threads:
t.join()
log.info("Server shutdown complete.")
sys.exit(0)
if __name__ == "__main__":
start_server()