17 function CMemStream ()
19 $this->InputStream
= false;
22 debug("A : ".gettype($this->Buffer
)."<br>");
25 function setBuffer ($Buffer)
27 $this->InputStream
= true;
28 $this->Buffer
= $Buffer;
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));
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);
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));
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);
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);
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())
101 $this->serialUInt32($intValue);
102 $val->fromInt((int)$intValue);
103 debug(sprintf ("read enum '%s'<br>\n", $val->toString()));
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
123 function setName($name)
125 $this->MsgName
= $name;
129 class CCallbackClient
131 var $ConSock = false;
135 function connect($addr, $port, &$res)
139 debug(sprintf("Connect<br>"));
142 $this->ConSock
= fsockopen ($addr, $port, $errno, $errstr, $SockTimeOut);
143 debug("H".gettype($this->ConSock
)."<br>");
147 $res = "Can't connect to the callback server '$addr:$port' ($errno: $errstr)";
153 // set time out on the socket to 2 secondes
154 stream_set_timeout($this->ConSock
, $SockTimeOut);
164 fclose($this->ConSock
);
165 debug(sprintf("Close<br>"));
168 debug(sprintf("Already Closed !<br>"));
171 function sendMessage(&$message)
175 debug(sprintf ("Socket is not valid\n"));
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
184 debug(sprintf("After SendMessage, number %u<br>", $this->MsgNum
));
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"));
210 debug(sprintf ("sent packet size '%d' (written size = %d) <br>\n", strlen($Buffer), $size));
211 fflush ($this->ConSock
);
216 function waitMessage()
220 debug(sprintf ("Socket is not valid\n"));
226 $val = fread ($this->ConSock
, 1);
227 $info = stream_get_meta_data($this->ConSock
);
228 if ($info['timed_out'])
230 debug('Connection timed out!');
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!');
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!');
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!');
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!');
266 $size -= 5; // remove the fake
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!');
279 $msgin = new CMemStream
;
280 $msgin->setBuffer ($Buffer);
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
));