Merge branch 'ryzom/rites' into main/gingo-test
[ryzomcore.git] / ryzom / server / www / libs / nel_message.php
blob65a415ddf006aea5ef80d177216d4f2ea1a6c1a8
1 <?php
3 $SockTimeOut = 10;
5 function debug($text)
7 // flush();
8 // echo $text;
11 class CMemStream
13 var $Buffer;
14 var $InputStream;
15 var $Pos;
17 function CMemStream ()
19 $this->InputStream = false;
20 $this->Pos = 0;
21 $this->Buffer = "";
22 debug("A : ".gettype($this->Buffer)."<br>");
25 function setBuffer ($Buffer)
27 $this->InputStream = true;
28 $this->Buffer = $Buffer;
29 $this->Pos = 0;
32 function isReading () { return $this->InputStream; }
34 function serialUInt8 (&$val)
36 if ($this->isReading())
38 $val = ord($this->Buffer[$this->Pos++]);
39 debug(sprintf ("read uint8 '%d'<br>\n", $val));
41 else
43 debug("B".gettype($this->Buffer)."<br>");
44 debug(sprintf ("write uint8 Buffer size before = %u<br>\n", strlen($this->Buffer)));
45 $this->Buffer = $this->Buffer . chr($val & 0xFF);
46 $this->Pos++;
47 debug("C".gettype($this->Buffer)."<br>");
48 debug(sprintf ("write uint8 '%d' %d<br>\n", $val, $this->Pos));
49 debug(sprintf ("write uint8 Buffer size after = %u<br>\n", strlen($this->Buffer)));
53 function serialUInt32 (&$val)
55 if ($this->isReading())
57 $val = ord($this->Buffer[$this->Pos++]);
58 $val += ord($this->Buffer[$this->Pos++])*256;
59 $val += ord($this->Buffer[$this->Pos++])*(double)256*256;
60 $val += ord($this->Buffer[$this->Pos++])*(double)256*256*256;
61 debug(sprintf ("read uint32 '%d'<br>\n", $val));
62 // var_dump($val);
64 else
66 debug("D".gettype($this->Buffer)."<br>");
67 $this->Buffer .= chr($val & 0xFF);
68 $this->Buffer .= chr(($val>>8) & 0xFF);
69 $this->Buffer .= chr(($val>>16) & 0xFF);
70 $this->Buffer .= chr(($val>>24) & 0xFF);
71 $this->Pos += 4;
72 debug("E".gettype($this->Buffer)."<br>");
73 debug(sprintf ("write uint32 '%d' %d<br>\n", $val, $this->Pos));
77 function serialString (&$val)
79 if ($this->isReading())
81 $this->serialUInt32($size);
82 debug(sprintf ("read string : size = %u<br>\n", $size));
83 $val = substr ($this->Buffer, $this->Pos, $size);
84 debug(sprintf ("read string '%s'<br>\n", $val));
85 $this->Pos += strlen($val);
87 else
89 $valLen = strlen($val);
90 $this->serialUInt32($valLen);
91 $this->Buffer .= $val;
92 $this->Pos += $valLen;
93 debug(sprintf ("write string '%s' %d<br>\n", $val, $this->Pos));
96 function serialEnum (&$val)
98 if ($this->isReading())
100 $intValue = 0;
101 $this->serialUInt32($intValue);
102 $val->fromInt((int)$intValue);
103 debug(sprintf ("read enum '%s'<br>\n", $val->toString()));
105 else
107 $intValue = $val->toInt();
108 $this->serialUInt32($intValue);
109 debug(sprintf ("write enum '%s' %d<br>\n", $val->toString(), $this->Pos));
114 class CMessage extends CMemStream
116 var $MsgName;
118 function CMessage()
120 $this->CMemStream();
123 function setName($name)
125 $this->MsgName = $name;
129 class CCallbackClient
131 var $ConSock = false;
133 var $MsgNum = 0;
135 function connect($addr, $port, &$res)
137 global $SockTimeOut;
139 debug(sprintf("Connect<br>"));
140 $this->MsgNum = 0;
142 $this->ConSock = fsockopen ($addr, $port, $errno, $errstr, $SockTimeOut);
143 debug("H".gettype($this->ConSock)."<br>");
145 if (!$this->ConSock)
147 $res = "Can't connect to the callback server '$addr:$port' ($errno: $errstr)";
149 return false;
151 else
153 // set time out on the socket to 2 secondes
154 stream_set_timeout($this->ConSock, $SockTimeOut);
155 $res = "";
156 return true;
160 function close()
162 if ($this->ConSock)
164 fclose($this->ConSock);
165 debug(sprintf("Close<br>"));
167 else
168 debug(sprintf("Already Closed !<br>"));
171 function sendMessage(&$message)
173 if (!$this->ConSock)
175 debug(sprintf ("Socket is not valid\n"));
176 return false;
178 debug(sprintf ("sendMessage : message Buffer is '%d'<br>\n", $message->Pos));
179 debug(sprintf ("sendMessage : message Buffer is '%d'<br>\n", strlen($message->Buffer)));
180 $hd = new CMemStream;
181 debug(sprintf("SendMessage number %u<br>", $this->MsgNum));
182 $hd->serialUInt32 ($this->MsgNum); // number the packet
183 $this->MsgNum += 1;
184 debug(sprintf("After SendMessage, number %u<br>", $this->MsgNum));
185 $messageType = 0;
186 $hd->serialUInt8 ($messageType);
187 $hd->serialString ($message->MsgName);
189 debug(sprintf ("sendMessage : header size is '%d'<br>\n", $hd->Pos));
191 // $sb .= $message->Buffer;
193 $size = $hd->Pos + $message->Pos;
194 $Buffer = (string) chr(($size>>24)&0xFF);
195 $Buffer .= chr(($size>>16)&0xFF);
196 $Buffer .= chr(($size>>8)&0xFF);
197 $Buffer .= chr($size&0xFF);
198 debug( "E".gettype($hd->Buffer)."<br>");
199 debug("F".gettype($message->Buffer)."<br>");
200 $Buffer .= (string) $hd->Buffer;
201 $Buffer .= (string) $message->Buffer;
203 debug("G".gettype($this->ConSock)."<br>");
205 if (!fwrite ($this->ConSock, $Buffer))
207 debug(sprintf ("Error writing to socket\n"));
208 return false;
210 debug(sprintf ("sent packet size '%d' (written size = %d) <br>\n", strlen($Buffer), $size));
211 fflush ($this->ConSock);
213 return true;
216 function waitMessage()
218 if (!$this->ConSock)
220 debug(sprintf ("Socket is not valid\n"));
221 return false;
225 $size = 0;
226 $val = fread ($this->ConSock, 1);
227 $info = stream_get_meta_data($this->ConSock);
228 if ($info['timed_out'])
230 debug('Connection timed out!');
231 return false;
233 $size = ord($val) << 24;
234 $val = fread ($this->ConSock, 1);
235 $info = stream_get_meta_data($this->ConSock);
236 if ($info['timed_out'])
238 debug('Connection timed out!');
239 return false;
241 $size = ord($val) << 16;
242 $val = fread ($this->ConSock, 1);
243 $info = stream_get_meta_data($this->ConSock);
244 if ($info['timed_out'])
246 debug('Connection timed out!');
247 return false;
249 $size += ord($val) << 8;
250 $val = fread ($this->ConSock, 1);
251 $info = stream_get_meta_data($this->ConSock);
252 if ($info['timed_out'])
254 debug('Connection timed out!');
255 return false;
257 $size += ord($val);
258 debug(sprintf ("receive packet size '%d'<br>\n", $size));
259 $fake = fread ($this->ConSock, 5);
260 $info = stream_get_meta_data($this->ConSock);
261 if ($info['timed_out'])
263 debug('Connection timed out!');
264 return false;
266 $size -= 5; // remove the fake
268 $Buffer = "";
269 while ($size > 0 && strlen($Buffer) != $size)
271 $Buffer .= fread ($this->ConSock, $size - strlen($Buffer));
272 $info = stream_get_meta_data($this->ConSock);
273 if ($info['timed_out'])
275 debug('Connection timed out!');
276 return false;
279 $msgin = new CMemStream;
280 $msgin->setBuffer ($Buffer);
282 // decode msg name
283 $msgin->serialString($name);
285 debug(sprintf("Message name = '%s'<BR>", $name));
286 $message = new CMessage;
287 $message->setBuffer(substr($msgin->Buffer, $msgin->Pos));
288 $message->setName($name);
290 debug(sprintf("In message name = '%s'<br>", $message->MsgName));
292 return $message;