28 public function __construct(
33 if(($packet = $this->channel->read()) !==
null){
34 $id = ord($packet[0]);
36 if($id === ITCProtocol::PACKET_ENCAPSULATED){
37 $sessionId = Binary::readInt(substr($packet, $offset, 4));
39 $buffer = substr($packet, $offset);
40 $listener->onPacketReceive($sessionId, $buffer);
41 }elseif($id === ITCProtocol::PACKET_RAW){
42 $len = ord($packet[$offset++]);
43 $address = substr($packet, $offset, $len);
45 $port = Binary::readShort(substr($packet, $offset, 2));
47 $payload = substr($packet, $offset);
48 $listener->onRawPacketReceive($address, $port, $payload);
49 }elseif($id === ITCProtocol::PACKET_REPORT_BANDWIDTH_STATS){
50 $sentBytes = Binary::readLong(substr($packet, $offset, 8));
52 $receivedBytes = Binary::readLong(substr($packet, $offset, 8));
53 $listener->onBandwidthStatsUpdate($sentBytes, $receivedBytes);
54 }elseif($id === ITCProtocol::PACKET_OPEN_SESSION){
55 $sessionId = Binary::readInt(substr($packet, $offset, 4));
57 $len = ord($packet[$offset++]);
58 $rawAddr = substr($packet, $offset, $len);
60 $address = inet_ntop($rawAddr);
61 if($address ===
false){
62 throw new \RuntimeException(
"Unexpected invalid IP address in inter-thread message");
64 $port = Binary::readShort(substr($packet, $offset, 2));
66 $clientID = Binary::readLong(substr($packet, $offset, 8));
67 $listener->onClientConnect($sessionId, $address, $port, $clientID);
68 }elseif($id === ITCProtocol::PACKET_CLOSE_SESSION){
69 $sessionId = Binary::readInt(substr($packet, $offset, 4));
71 $reason = ord($packet[$offset]);
73 }elseif($id === ITCProtocol::PACKET_ACK_NOTIFICATION){
74 $sessionId = Binary::readInt(substr($packet, $offset, 4));
76 $identifierACK = Binary::readInt(substr($packet, $offset, 4));
77 $listener->onPacketAck($sessionId, $identifierACK);
78 }elseif($id === ITCProtocol::PACKET_REPORT_PING){
79 $sessionId = Binary::readInt(substr($packet, $offset, 4));
81 $pingMS = Binary::readInt(substr($packet, $offset, 4));
82 $listener->onPingMeasure($sessionId, $pingMS);