Esempio n. 1
0
 protected function processPacket()
 {
     @fread($this->fp, 1);
     if (strlen($packet = $this->thread->readMainToThreadPacket()) > 0) {
         $pid = ord($packet[0]);
         $buffer = substr($packet, 1);
         if ($pid === self::PACKET_SEND_PACKET) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             $data = substr($buffer, 4);
             if (!isset($this->sessions[$id])) {
                 $this->closeSession($id);
                 return true;
             }
             $this->sessions[$id]->writeRaw($data);
         } elseif ($pid === self::PACKET_ENABLE_ENCRYPTION) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             $secret = substr($buffer, 4);
             if (!isset($this->sessions[$id])) {
                 $this->closeSession($id);
                 return true;
             }
             $this->sessions[$id]->enableEncryption($secret);
         } elseif ($pid === self::PACKET_SET_COMPRESSION) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             $threshold = Binary::readInt(substr($buffer, 4, 4));
             if (!isset($this->sessions[$id])) {
                 $this->closeSession($id);
                 return true;
             }
             $this->sessions[$id]->setCompression($threshold);
         } elseif ($pid === self::PACKET_CLOSE_SESSION) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             if (isset($this->sessions[$id])) {
                 $this->close($this->sessions[$id]);
             }
         } elseif ($pid === self::PACKET_SHUTDOWN) {
             $this->shutdown = true;
             foreach ($this->sessions as $session) {
                 $session->close();
             }
         } elseif ($pid === self::PACKET_EMERGENCY_SHUTDOWN) {
             $this->shutdown = true;
         }
         return true;
     }
     return false;
 }
Esempio n. 2
0
 protected function processPacket()
 {
     @fread($this->fp, 1);
     if (strlen($packet = $this->thread->readMainToThreadPacket()) > 0) {
         $pid = ord($packet[0]);
         $buffer = substr($packet, 1);
         if ($pid === self::PACKET_SEND_PACKET) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             $data = substr($buffer, 4);
             if (!isset($this->sessions[$id])) {
                 $this->closeSession($id, 0);
                 return true;
             }
             $this->sessions[$id]->writeRaw($data);
         } elseif ($pid === self::PACKET_ENABLE_ENCRYPTION) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             $secret = substr($buffer, 4);
             if (!isset($this->sessions[$id])) {
                 $this->closeSession($id, 0);
                 return true;
             }
             $this->sessions[$id]->enableEncryption($secret);
         } elseif ($pid === self::PACKET_SET_COMPRESSION) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             $threshold = Binary::readInt(substr($buffer, 4, 4));
             if (!isset($this->sessions[$id])) {
                 $this->closeSession($id, 0);
                 return true;
             }
             $this->sessions[$id]->setCompression($threshold);
         } elseif ($pid === self::PACKET_SET_OPTION) {
             $offset = 1;
             $len = ord($packet[$offset++]);
             $name = substr($packet, $offset, $len);
             $offset += $len;
             $value = substr($packet, $offset);
             switch ($name) {
                 case "name":
                     $this->serverdata = json_decode($value, true);
                     break;
             }
         } elseif ($pid === self::PACKET_CLOSE_SESSION) {
             $id = Binary::readInt(substr($buffer, 0, 4));
             if (isset($this->sessions[$id])) {
                 $this->close($this->sessions[$id]);
             } else {
                 $this->closeSession($id, 1);
             }
         } elseif ($pid === self::PACKET_SHUTDOWN) {
             foreach ($this->sessions as $session) {
                 $session->close();
             }
             $this->shutdown();
             stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
             $this->shutdown = true;
         } elseif ($pid === self::PACKET_EMERGENCY_SHUTDOWN) {
             $this->shutdown = true;
         }
         return true;
     }
     return false;
 }