forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyDatagram.py
More file actions
executable file
·129 lines (118 loc) · 4.99 KB
/
PyDatagram.py
File metadata and controls
executable file
·129 lines (118 loc) · 4.99 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
# This is built as a subclass instead of an extension so we can define the
# class variable FuncDict and so we can import DCSubatomicType at the top
# of the file rather than every time we call the putArg function.
from panda3d.core import Datagram
from panda3d.direct import *
# Import the type numbers
from direct.distributed.MsgTypes import *
class PyDatagram(Datagram):
# This is a little helper Dict to replace the huge <if> statement
# for trying to match up datagram subatomic types with add funtions
# If Python had an O(1) "case" statement we would use that instead
FuncDict = {
STInt8: (Datagram.addInt8, int),
STInt16: (Datagram.addInt16, int),
STInt32: (Datagram.addInt32, int),
STInt64: (Datagram.addInt64, int),
STUint8: (Datagram.addUint8, int),
STUint16: (Datagram.addUint16, int),
STUint32: (Datagram.addUint32, int),
STUint64: (Datagram.addUint64, int),
STFloat64: (Datagram.addFloat64, None),
STString: (Datagram.addString, None),
STBlob: (Datagram.addBlob, None),
STBlob32: (Datagram.addBlob32, None),
}
addChannel = Datagram.addUint64
def addServerHeader(self, channel, sender, code):
self.addInt8(1)
self.addChannel(channel)
self.addChannel(sender)
self.addUint16(code)
def addOldServerHeader(self, channel, sender, code):
self.addChannel(channel)
self.addChannel(sender)
self.addChannel('A')
self.addUint16(code)
def addServerControlHeader(self, code):
self.addInt8(1)
self.addChannel(CONTROL_CHANNEL)
self.addUint16(code)
def putArg(self, arg, subatomicType, divisor=1):
if (divisor == 1):
funcSpecs = self.FuncDict.get(subatomicType)
if funcSpecs:
addFunc, argFunc = funcSpecs
if argFunc:
arg = argFunc(arg)
addFunc(self, arg)
# Ok, arrays are not handled by the FuncDict yet
elif subatomicType == STInt8array:
self.addUint16(len(arg))
for i in arg:
self.addInt8(int(i))
elif subatomicType == STInt16array:
self.addUint16(len(arg) << 1)
for i in arg:
self.addInt16(int(i))
elif subatomicType == STInt32array:
self.addUint16(len(arg) << 2)
for i in arg:
self.addInt32(int(i))
elif subatomicType == STUint8array:
self.addUint16(len(arg))
for i in arg:
self.addUint8(int(i))
elif subatomicType == STUint16array:
self.addUint16(len(arg) << 1)
for i in arg:
self.addUint16(int(i))
elif subatomicType == STUint32array:
self.addUint16(len(arg) << 2)
for i in arg:
self.addUint32(int(i))
elif subatomicType == STUint32uint8array:
self.addUint16(len(arg) * 5)
for i in arg:
self.addUint32(int(i[0]))
self.addUint8(int(i[1]))
else:
raise Exception("Error: No such type as: " + str(subatomicType))
else:
funcSpecs = self.FuncDict.get(subatomicType)
if funcSpecs:
# argFunc is only used if divisor == 1
addFunc, argFunc = funcSpecs
addFunc(self, int(round(arg * divisor)))
# Ok, arrays are not handled by the FuncDict yet
elif subatomicType == STInt8array:
self.addUint16(len(arg))
for i in arg:
self.addInt8(int(round(i*divisor)))
elif subatomicType == STInt16array:
self.addUint16(len(arg) << 1)
for i in arg:
self.addInt16(int(round(i*divisor)))
elif subatomicType == STInt32array:
self.addUint16(len(arg) << 2)
for i in arg:
self.addInt32(int(round(i*divisor)))
elif subatomicType == STUint8array:
self.addUint16(len(arg))
for i in arg:
self.addUint8(int(round(i*divisor)))
elif subatomicType == STUint16array:
self.addUint16(len(arg) << 1)
for i in arg:
self.addUint16(int(round(i*divisor)))
elif subatomicType == STUint32array:
self.addUint16(len(arg) << 2)
for i in arg:
self.addUint32(int(round(i*divisor)))
elif subatomicType == STUint32uint8array:
self.addUint16(len(arg) * 5)
for i in arg:
self.addUint32(int(round(i[0]*divisor)))
self.addUint8(int(round(i[1]*divisor)))
else:
raise Exception("Error: type does not accept divisor: " + str(subatomicType))