Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / web / public_php / tools / nel_message.php
blobd4cbc55d674fb0456aaa30ee5d84be0811f46e14
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 __construct()
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 setName($name)
120 $this->MsgName = $name;
124 class CCallbackClient
126 var $ConSock = false;
128 var $MsgNum = 0;
130 function connect($addr, $port, &$res)
132 global $SockTimeOut;
134 debug(sprintf("Connect<br>"));
135 $this->MsgNum = 0;
137 $this->ConSock = fsockopen ($addr, $port, $errno, $errstr, $SockTimeOut);
138 debug("H".gettype($this->ConSock)."<br>");
140 if (!$this->ConSock)
142 $res = "Can't connect to the callback server '$addr:$port' ($errno: $errstr)";
144 return false;
146 else
148 // set time out on the socket to 2 secondes
149 stream_set_timeout($this->ConSock, $SockTimeOut);
150 $res = "";
151 return true;
155 function close()
157 if ($this->ConSock)
159 fclose($this->ConSock);
160 debug(sprintf("Close<br>"));
162 else
163 debug(sprintf("Already Closed !<br>"));
166 function sendMessage(&$message)
168 if (!$this->ConSock)
170 debug(sprintf ("Socket is not valid\n"));
171 return false;
173 debug(sprintf ("sendMessage : message Buffer is '%d'<br>\n", $message->Pos));
174 debug(sprintf ("sendMessage : message Buffer is '%d'<br>\n", strlen($message->Buffer)));
175 $hd = new CMemStream;
176 debug(sprintf("SendMessage number %u<br>", $this->MsgNum));
177 $hd->serialUInt32 ($this->MsgNum); // number the packet
178 $this->MsgNum += 1;
179 debug(sprintf("After SendMessage, number %u<br>", $this->MsgNum));
180 $messageType = 0;
181 $hd->serialUInt8 ($messageType);
182 $hd->serialString ($message->MsgName);
184 debug(sprintf ("sendMessage : header size is '%d'<br>\n", $hd->Pos));
186 // $sb .= $message->Buffer;
188 $size = $hd->Pos + $message->Pos;
189 $Buffer = (string) chr(($size>>24)&0xFF);
190 $Buffer .= chr(($size>>16)&0xFF);
191 $Buffer .= chr(($size>>8)&0xFF);
192 $Buffer .= chr($size&0xFF);
193 debug( "E".gettype($hd->Buffer)."<br>");
194 debug("F".gettype($message->Buffer)."<br>");
195 $Buffer .= (string) $hd->Buffer;
196 $Buffer .= (string) $message->Buffer;
198 debug("G".gettype($this->ConSock)."<br>");
200 if (!fwrite ($this->ConSock, $Buffer))
202 debug(sprintf ("Error writing to socket\n"));
203 return false;
205 debug(sprintf ("sent packet size '%d' (written size = %d) <br>\n", strlen($Buffer), $size));
206 fflush ($this->ConSock);
208 return true;
211 function waitMessage()
213 if (!$this->ConSock)
215 debug(sprintf ("Socket is not valid\n"));
216 return false;
220 $size = 0;
221 $val = fread ($this->ConSock, 1);
222 $info = stream_get_meta_data($this->ConSock);
223 if ($info['timed_out'])
225 debug('Connection timed out!');
226 return false;
228 $size = ord($val) << 24;
229 $val = fread ($this->ConSock, 1);
230 $info = stream_get_meta_data($this->ConSock);
231 if ($info['timed_out'])
233 debug('Connection timed out!');
234 return false;
236 $size = ord($val) << 16;
237 $val = fread ($this->ConSock, 1);
238 $info = stream_get_meta_data($this->ConSock);
239 if ($info['timed_out'])
241 debug('Connection timed out!');
242 return false;
244 $size += ord($val) << 8;
245 $val = fread ($this->ConSock, 1);
246 $info = stream_get_meta_data($this->ConSock);
247 if ($info['timed_out'])
249 debug('Connection timed out!');
250 return false;
252 $size += ord($val);
253 debug(sprintf ("receive packet size '%d'<br>\n", $size));
254 $fake = fread ($this->ConSock, 5);
255 $info = stream_get_meta_data($this->ConSock);
256 if ($info['timed_out'])
258 debug('Connection timed out!');
259 return false;
261 $size -= 5; // remove the fake
263 $Buffer = "";
264 while ($size > 0 && strlen($Buffer) != $size)
266 $Buffer .= fread ($this->ConSock, $size - strlen($Buffer));
267 $info = stream_get_meta_data($this->ConSock);
268 if ($info['timed_out'])
270 debug('Connection timed out!');
271 return false;
274 $msgin = new CMemStream;
275 $msgin->setBuffer ($Buffer);
277 // decode msg name
278 $msgin->serialString($name);
280 debug(sprintf("Message name = '%s'<BR>", $name));
281 $message = new CMessage;
282 $message->setBuffer(substr($msgin->Buffer, $msgin->Pos));
283 $message->setName($name);
285 debug(sprintf("In message name = '%s'<br>", $message->MsgName));
287 return $message;
292 // class CSessionManagerProxy
293 // {
294 // function createSession($userId, $sessionType, $callbackClient)
295 // {
296 // debug(sprintf("Creating session for user %u, type %s<BR>", $userId, $sessionType));
297 // $msg = new CMessage;
298 // $msg->setName("CSS");
299 // $msg->serialUInt32($userId);
300 // $msg->serialString($sessionType);
302 // $callbackClient->sendMessage($msg);
303 // }
304 // }
306 // class CSessionManagerClientSkel
307 // {
308 // function waitCallback($callbackClient)
309 // {
310 // $message = $callbackClient->waitMessage();
312 // debug(sprintf("Received message '%s'<BR>", $message->MsgName));
314 // switch($message->MsgName)
315 // {
316 // case "CSSR":
317 // debug(sprintf("Create session result<BR>"));
318 // $this->createSessionResult_skel($message);
319 // break;
321 // case "CSNR":
322 // debug(sprintf("Create scenario result<BR>"));
323 // $this->createScenarioResult_skel($message);
324 // break;
325 // };
326 // }
328 // function createSessionResult_skel($message)
329 // {
330 // $userId = 0;
331 // $sessionId = 0;
332 // $result = false;
334 // $message->serialUInt32($userId);
335 // $message->serialUInt32($sessionId);
336 // $message->serialUInt8($result);
338 // createSessionResult($userId, $sessionId, $result);
339 // }
340 // }
342 // printf("creating callback client...<BR>");
344 // $cb = new CCallbackClient;
345 // $ret = "";
346 // $cb->connect("192.168.0.1", "8060", $ret);
348 // $smp = new CSessionManagerProxy;
350 // printf("creating a new sessions...<BR>");
351 // $smp->createSession(10, "st_edit", $cb);
353 // $smcs = new CSessionManagerClientSkel;
354 // $smcs->waitCallback($cb);
357 // function createSessionResult($userId, $sessionId, $result)
358 // {
359 // echo "The session result for user $userId is the session $sessionId with a result of $result\n";
360 // }
363 // This function connect to the AS.
364 // If true, $res contains the url to connect.
365 // If false, $res contains the reason why it s not okay.
367 // function connectToAS(&$fp, &$res)
368 // {
369 // global $ASHost, $ASPort;
370 ///*
371 // $sid = session_id();
372 // $result = sqlquery("SELECT socket_id FROM resident_socket");
373 // if (!$result || sqlnumrows($result) == 0)
374 // {
375 // $fp = pfsockopen ($ASHost, $ASPort, $errno, $errstr, 30);
376 // echo "opened resident socket '$fp'\n";
378 // $result = sqlquery("SELECT socket_id FROM resident_socket WHERE socket_id='$fp'");
379 // if ($result && sqlnumrows($result)>0)
380 // sqlquery("DELETE FROM resident_socket WHERE socket_id='$fp'");
382 // sqlquery("INSERT INTO resident_socket SET socket_id='$fp', session_id='$sid', last_access=NOW()");
383 // }
384 // else
385 // {
386 // $result = sqlfetch($result);
387 // $fp = $result["socket_id"];
388 // }
390 // // remove too old sockets
391 // sqlquery("SELECT socket_id FROM resident_socket WHERE NOW()-last_access > 1800");
392 // while ($result && ($arr=sqlfetch($result)))
393 // {
394 // fclose((int)($arr["socket_id"]));
395 // sqlquery("DELETE FROM resident_socket WHERE socket_id='".$arr["socket_id"]."'");
396 // }
398 // // update current socket last access
399 // sqlquery("UPDATE resident_socket SET last_access=NOW() WHERE socket_id='$fp' AND session_id='$sid'");
400 //*/
402 // // connect to the login service that must be $ASHost:$ASPort
403 // $fp = fsockopen ($ASHost, $ASPort, $errno, $errstr, 30);
404 // if (!$fp)
405 // {
406 // $res = "Can't connect to the admin service '$ASHost:$ASPort' ($errno: $errstr)";
407 // }
408 // else
409 // {
410 // $res = "";
411 // }
413 // }
415 // function disconnectFromAS(&$fp)
416 // {
417 ///*
418 // $result = sqlquery("SELECT socket_id FROM resident_socket WHERE socket_id='$fp'");
419 // if (!$result || sqlnumrows($socket)==0)
420 // fclose($fp);
421 //*/
422 // fclose($fp);
423 // }
425 // function sendMessage ($fp, $msgout)
426 // {
427 // $size = $msgout->Pos;
428 // $Buffer = chr(($size>>24)&0xFF);
429 // $Buffer .= chr(($size>>16)&0xFF);
430 // $Buffer .= chr(($size>>8)&0xFF);
431 // $Buffer .= chr($size&0xFF);
432 // $Buffer .= $msgout->Buffer;
434 // fwrite ($fp, $Buffer);
436 // //printf ("sent packet size '%d'<br>", strlen($Buffer));
438 // fflush ($fp);
439 // }
441 // function waitMessage ($fp, &$msgin)
442 // {
443 // //echo "waiting a message";
444 // $size = 0;
445 // $val = fread ($fp, 1);
446 // $size = ord($val) << 24;
447 // $val = fread ($fp, 1);
448 // $size = ord($val) << 16;
449 // $val = fread ($fp, 1);
450 // $size += ord($val) << 8;
451 // $val = fread ($fp, 1);
452 // $size += ord($val);
453 // //printf ("receive packet size '%d'<br>", $size);
454 // $fake = fread ($fp, 4);
455 // $size -= 4; // remove the fake
457 // $Buffer = fread ($fp, $size);
458 // $msgin = new CMemStream;
459 // $msgin->setBuffer ($Buffer);
460 // }
462 // function logNelQuery($query)
463 // {
464 // global $uid;
465 ///*
466 // $f = fopen("./nel_queries.log", "a");
467 // fwrite($f, date("Y/m/d H:i:s")." ".sprintf("%-16s", $admlogin)." $query\n");
468 // fclose($f);
469 //*/
471 // logUser($uid, "QUERY=".$query);
472 // }
474 // function nel_query($rawvarpath, &$result)
475 // {
476 // global $nel_queries;
478 // $nel_queries[] = $rawvarpath;
479 // $ok = false;
480 // //echo "rawvarpath=$rawvarpath<br>\n";
482 // //logNelQuery($rawvarpath);
484 // connectToAS($fp, $result);
485 // if(strlen($result) != 0)
486 // return $ok;
488 // // send the message that say that we want to add a user
489 // $msgout = new CMemStream;
490 // $fake = 0;
491 // $msgout->serialuint32 ($fake); // fake used to number the packet
492 // $messageType = 0;
493 // $msgout->serialuint8 ($messageType);
494 // $msgout->serialstring ($rawvarpath);
496 // sendMessage ($fp, $msgout);
498 // waitMessage ($fp, $msgin);
500 // $msgin->serialstring($result);
502 // if(strlen($result) == 0)
503 // {
504 // // it failed
505 // }
506 // else
507 // {
508 // // it's ok
509 // $ok = true;
510 // }
512 // //printf("receive response '$result'<br>\n");
514 // disconnectFromAS(&$fp);
515 // //echo "sent OK.<br><br>\n";
517 // return $ok;
518 // }