-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
99 lines (81 loc) · 2.91 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
"""@package docstring
Server.py contains all of the game server functionality. A server should
1) Be created
2) Listen for incoming messages
3) Interpret messages and call other code as necessary
Server.py should not be used to run a server, any instances of a server
should be created in Play.py . Server.py does not handle game logic either, it
only facilitates the communication between the client (where the user interacts)
and the server (where the game specific code is handled).
We may not run a pure client-server model so keep that in mind when moving
code out of this file... like the server loop
"""
''' TODO: Remove the instance of a server loop from here and place it elsewhere
'''
# Server Imports
from socket import *
from Board import *
import pickle
import json
# Server Data
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serverSocket.bind(('', serverPort))
blocksize = 4096
def interpret_data(data):
"""
This will be used to deserialize the received data. Once that's done, the
kind of data we have will determine what's done next
"""
'''
Data received should be easily convertable. The following has a table
with the encode / decode formats for JSON to Python
https://docs.python.org/3/library/json.html#json-to-py-table
'''
try:
# Deserialize the data and then print it to the console so we can
# see what's being received
deserializedData = json.loads(data)
return deserializedData
except Exception as e:
print(e)
def send_board(serverSocket, clientAddress, rawData):
'''
TODO: Fix this
attempts to send multiple packets over pickle
'''
try:
end = b'\x00\x00END_MESSAGE!\x00\x00'[:blocksize]
pickleData = pickle.dumps(rawData)
#following lines separate pickled board into pieces & send
for n in range(len(pickleData) // blocksize + 1):
serverSocket.sendto(pickleData[n * blocksize: (n + 1) * blocksize], clientAddress)
serverSocket.sendto(end)
except Exception as e:
print(e)
def send_json(serverSocket, clientAddress, rawData):
try:
jsonData = json.dumps(rawData)
serverSocket.sendto(jsonData, clientAddress)
except Exception as e:
print(e)
def send_data(serverSocket, clientAddress, rawData):
try:
serverSocket.sendto(pickleData, clientAddress)
except Exception as e:
print(e)
if isinstance(rawData, Board):
try:
print("Sending a board")
send_board(serverSocket, clientAddress, rawData)
except:
print("Type: Board, had issue in send_data")
elif isinstance(rawData, str):
try:
print("Sending json")
send_json(serverSocket, clientAddress, rawData)
except:
print(e)
else:
print("Error, could not determine type")