-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
161 lines (125 loc) · 4.63 KB
/
server.py
File metadata and controls
161 lines (125 loc) · 4.63 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
## =================================================================================================
## Python 3.6+
## =================================================================================================
import json
import socket
import sys
import threading
import time
import player
import pysockets
## =================================================================================================
PLAYERS = {}
## =================================================================================================
def main():
## Get device local IP
ip = pysockets.get_ip()
## Create TCP socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
## Avoid port conflict
port = pysockets.PORT
while True:
try:
s.bind((ip, port))
print (f'Server socket bound to {ip}:{port}')
break
except OSError:
port += 1
if port > 65535:
port = 0
## Put the socket into listening mode
s.listen(5)
## Accept connections
try:
while True:
## Establish connection with client
c, addr = s.accept()
print(f'Got connection from {addr[0]}:{addr[1]}')
## Host the connection in its own thread
thr = threading.Thread(target=read_data_thread_function, args=(OnlinePlayer(c, addr),))
thr.start()
time.sleep(0.2)
except KeyboardInterrupt:
for op in PLAYERS.values():
op.c.close()
s.shutdown(socket.SHUT_RDWR)
s.close()
## -------------------------------------------------------------------------------------------------
def json_dumps_players():
'''
Get PLAYERS as JSON string
:return: string
'''
json_data = {'players': []}
for op in PLAYERS.values():
if op.player_data is not None:
json_data['players'].append(op.as_dict())
return json.dumps(json_data)
## -------------------------------------------------------------------------------------------------
def read_data_thread_function(online_player):
'''
Reads data from a particular connection. Each connection will have its own instance of this
function in a unique thread.
:param online_player: OnlinePlayer
:return: None
'''
while True:
## Read player data
try:
msg = pysockets.receive_msg(online_player.c)
online_player.player_data = player.player_from_json(msg)
except json.JSONDecodeError:
continue
except ConnectionResetError:
try:
print(f'Player #{online_player.player_data.id} disconnected (ConnectionResetError)')
del PLAYERS[online_player.player_data.id]
except AttributeError:
print(f'{online_player.addr[0]}:{online_player.addr[1]} disconnected '
f'(ConnectionResetError)')
broadcast_player_info()
return
## Update player info if its in-game, otherwise delete the player
if online_player.player_data.in_game == True:
PLAYERS[online_player.player_data.id] = online_player
broadcast_player_info()
else:
print(f'Player #{online_player.player_data.id} disconnected')
del PLAYERS[online_player.player_data.id]
broadcast_player_info()
return
## -------------------------------------------------------------------------------------------------
def broadcast_player_info():
'''
Broadcast player data to all connections
:return: None
'''
try:
## Broadcast updated player data back to clients
json_data = json_dumps_players()
for p in list(PLAYERS.values()):
pysockets.send_msg(p.c, json_data)
except (ConnectionResetError, ConnectionAbortedError):
pass
## -------------------------------------------------------------------------------------------------
class OnlinePlayer:
def __init__(self, connection, address):
'''
Class to correlate player data with a particular connection
:param connection: TCP connection obj
:param address: TCP address
'''
self.c = connection
self.player_data = None
self.addr = address
def __repr__(self):
return repr(self.player_data)
def as_dict(self):
if self.player_data is not None:
return self.player_data.as_dict()
else:
return {}
## =================================================================================================
if __name__ == '__main__':
assert sys.version_info >= (3, 6)
main()