Merge branch 'ryzom/rites' into main/gingo-test
[ryzomcore.git] / ryzom / server / tools / nel_message.py
blobff2a2a2a1f1dee9d3ac4198c042ee50d9e7d49d9
1 import logging
2 import socket
4 SockTimeOut=10
6 class CMemStream:
7 def __init__(self):
8 self.InputStream = False
9 self.Pos = 0
10 self.Buffer = b""
12 def setBuffer(self, Buffer):
13 self.InputStream = True
14 self.Buffer = Buffer
15 self.Pos = 0
18 def isReading(self):
19 return self.InputStream
21 def serialUInt8(self, val=b""):
22 if self.isReading():
23 val = ord(self.Buffer[self.Pos])
24 self.Pos += 1
25 return val
26 else:
27 self.Buffer += bytes([val & 0xFF])
28 self.Pos += 1
30 def serialUInt32(self, val=b""):
31 if self.isReading():
32 val = ord(self.Buffer[self.Pos])
33 self.Pos += 1
34 val += ord(self.Buffer[self.Pos])*256
35 self.Pos += 1
36 val += ord(self.Buffer[self.Pos])*256*256
37 self.Pos += 1
38 val += ord(self.Buffer[self.Pos])*256*256*256
39 self.Pos += 1
40 return val
41 else:
42 self.Buffer += bytes([val & 0xFF])
43 self.Buffer += bytes([(val>>8) & 0xFF])
44 self.Buffer += bytes([(val>>16) & 0xFF])
45 self.Buffer += bytes([(val>>24) & 0xFF])
46 self.Pos += 4
48 def serialString(self, val=b""):
49 if self.isReading():
50 size = self.serialUInt32()
51 val = self.Buffer[self.Pos:self.Pos+size]
52 self.Pos += len(val)
53 return val
54 else:
55 try:
56 #Convert to bytes if need
57 val = val.encode("utf-8")
58 except AttributeError:
59 pass
60 valLen = len(val)
61 self.serialUInt32(valLen)
62 self.Buffer += val
63 self.Pos += valLen
65 def serialEnum(self, val=""):
66 pass
69 class CMessage(CMemStream):
71 def __init__(self):
72 super().__init__()
74 def setName(self, name):
75 self.MsgName = name
78 class CCallbackClient():
80 ConSock = False
81 MsgNum = 0
83 def connect(self, addr, port, res):
84 global SockTimeOut
85 self.MsgNum = 0
88 self.ConSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
89 try:
90 self.ConSock.connect((addr, port))#, $errno, $errstr, $SockTimeOut)
91 except socket.error:
92 print("Connection failed...")
93 return False
95 print("Connection enabled!")
96 return True
98 #TODO: set time out on the socket to 2 secondes
99 #stream_set_timeout(self.ConSock, $SockTimeOut)
101 def close(self):
102 if self.ConSock:
103 self.ConSock.close()
106 def sendMessage(self, message):
107 hd = CMemStream()
108 hd.serialUInt32(self.MsgNum) #number the packet
109 self.MsgNum += 1
110 messageType = 0
111 hd.serialUInt8(messageType)
112 hd.serialString(message.MsgName)
114 size = hd.Pos + message.Pos
115 Buffer = chr((size>>24)&0xFF)
116 Buffer += chr((size>>16)&0xFF)
117 Buffer += chr((size>>8)&0xFF)
118 Buffer += chr(size&0xFF)
119 Buffer = Buffer.encode("iso-8859-1")
120 Buffer += hd.Buffer
121 Buffer += message.Buffer
123 print(Buffer.decode("utf-8", errors="replace"))
124 sent = self.ConSock.send(Buffer)
125 if not sent:
126 raise RuntimeError("socket connection broken")
128 def waitMessage(self):
129 size = 0
130 val = self.ConSock.recv(1)
131 if val:
132 size = ord(val) << 24
133 else:
134 print("Error")
136 val = self.ConSock.recv(1)
137 if val:
138 size = ord(val) << 16
139 else:
140 print("Error")
142 val = self.ConSock.recv(1)
143 size += ord(val) << 8
145 val = self.ConSock.recv(1)
146 size += ord(val)
148 fake = self.ConSock.recv(5)
149 size -= 5 #remove the fake
151 Buffer = ""
152 while size > 0 and len(Buffer) != size:
153 Buffer += self.ConSock.recv(size - len(Buffer)).decode("utf-8", errors="replace")
155 msgin = CMemStream()
156 msgin.setBuffer(Buffer)
158 #decode msg name
159 name = ""
160 name = msgin.serialString(name)
162 logging.info("Message name = '%s'" % name)
164 message = CMessage()
165 message.setBuffer(msgin.Buffer[msgin.Pos:])
166 message.setName(name)
168 return message