-
Notifications
You must be signed in to change notification settings - Fork 0
/
Protocol.py
149 lines (134 loc) · 5.47 KB
/
Protocol.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
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
import socket
import sys
import struct
import connection
class Protocol(object):
def __init__(self, serverip, port, gamestatus):
try:
connection.connect(serverip, port)
except socket.error as e:
#print e
if e.errno == 115:
#This error means that everything is alright
#(Operation now in Progress)
self.__setup(gamestatus)
else:
sys.exit()
else:
self.__setup(gamestatus)
def __setup(self, gamestatus):
if not hasattr(gamestatus, "objects"):
gamestatus["objects"] = dict()
self.gamestatus = gamestatus
connection.magic_number = '\x21'
connection.rules = {
'\x23' : {'name': 'update',
'processor': ['=HffffBBBB', 'id', 'velx', 'vely', 'posx',
'posy', 'gfx', 'colR', 'colG', 'colB']
},
'\x24' : {'name': 'updatePos',
'processor': ['=Hffff', 'id', 'velx', 'vely', 'posx',
'posy']
},
'\x25' : {'name': 'updateGfx',
'processor': ['=HBBBB', 'id', 'gfx', 'colR', 'colG',
'colB']
},
'\x26' : {'name': 'delete', 'processor' : ['H', 'id']},
'\x27' : {'name': 'loginAck', 'processor': ['B', 'status']},
'\x28' : {'name': 'status', 'processor': status},
'\x29' : {'name': 'message', 'processor': message},
'\x2A' : {'name': 'GTFO', 'processor': ['']}
}
def login(self, user, serverpw, userpw):
packet = struct.pack("=B16s16s16s", 0x42, user, serverpw, userpw)
connection.send(packet)
def sendMouse(self, position=(0.0, 0.0), velocity=(0.0, 0.0)):
packet = struct.pack("=Bffff", 0x44, position[0], position[1],
velocity[0], velocity[1])
try:
connection.send(packet)
except socket.error as e:
if e.errno == 32:
raise DisconnectException()
def sendKeyStatus(self, key, status):
packet = struct.pack("=BBB", 0x43, key, status)
try:
connection.send(packet)
except:
if e.errno == 32:
raise DisconnectException()
def parse(self):
for i in connection.parse():
if i.name == "message":
print i.message
elif i.name == "status":
for j in i.strings:
print j
elif i.name.startswith("update"):
#check wether the object exists
if i.id in self.gamestatus["objects"]:
updated = self.gamestatus["objects"][i.id]
elif i.name == "update":
#if object does not exist, create it, but only if the data
#is complete
updated = self.gamestatus["objects"][i.id] = {}
else:
#Discard the packet and continue processing
print i.name, " packet with invalid ID recieved"
continue
if i.name == "updatePos" or i.name == "update":
updated["pos"] = [i.posx, i.posy]
updated["vel"] = (i.velx, i.vely)
if i.name == "updateGfx" or i.name == "update":
updated["gfx"] = i.gfx
updated["color"] = (i.colR, i.colG, i.colB)
elif i.name == "delete":
if i.id == 0xffff:
for item in self.gamestatus["objects"].keys():
del self.gamestatus["objects"][item]
elif i.id in self.gamestatus["objects"]:
del self.gamestatus["objects"][i]
else:
#The server tries to delete an object that does not exist
print "The object with id %i does not exist" %(i.id)
elif i.name == "loginAck":
self.gamestatus.status = i.status
elif i.name == "GTFO":
raise GTFOException()
def update(self, time):
for obj in self.gamestatus["objects"].itervalues():
obj["pos"][0] += obj["vel"][0]*time
obj["pos"][1] += obj["vel"][1]*time
def __del__(self):
connection.close()
def status(stringbuffer):
try:
packet = connection.NetworkPacket('status')
packet.strings = []
sstrings = stringbuffer.read(1)
strings = struct.unpack('B', buffer)[0]
while strings > 0:
slength = stringbuffer.read(2)
length = struct.unpack('H', slength)[0]
packet.strings.append(stringbuffer.read(length))
strings -= 1
except OutOfStreamException as e:
raise connection.StreamIncompleteException()
return packet
def message(stringbuffer):
try:
slength = stringbuffer.read(2)
length = struct.unpack('H', slength)[0]
string = stringbuffer.read(length)
packet = connection.NetworkPacket('message')
packet.message = string
except OutOfStreamException as e:
raise connection.StreamIncompleteException()
return packet
class GTFOException(Exception):
def __str__(self):
return "GoD says: GTFO!!1!!"
class DisconnectException(Exception):
def __str__(self):
return "Disconnected from the server"