Inheritance: extends Thread
Ejemplo n.º 1
0
 /**
  * @return bool
  */
 public function handlePacket()
 {
     if (strlen($packet = $this->server->readThreadToMainPacket()) > 0) {
         $id = ord($packet[0]);
         $offset = 1;
         if ($id === RakLib::PACKET_ENCAPSULATED) {
             $len = ord($packet[$offset++]);
             $identifier = substr($packet, $offset, $len);
             $offset += $len;
             $flags = ord($packet[$offset++]);
             $buffer = substr($packet, $offset);
             $this->instance->handleEncapsulated($identifier, EncapsulatedPacket::fromBinary($buffer, true), $flags);
         } elseif ($id === RakLib::PACKET_RAW) {
             $len = ord($packet[$offset++]);
             $address = substr($packet, $offset, $len);
             $offset += $len;
             $port = unpack("n", substr($packet, $offset, 2))[1];
             $offset += 2;
             $payload = substr($packet, $offset);
             $this->instance->handleRaw($address, $port, $payload);
         } elseif ($id === RakLib::PACKET_SET_OPTION) {
             $len = ord($packet[$offset++]);
             $name = substr($packet, $offset, $len);
             $offset += $len;
             $value = substr($packet, $offset);
             $this->instance->handleOption($name, $value);
         } elseif ($id === RakLib::PACKET_OPEN_SESSION) {
             $len = ord($packet[$offset++]);
             $identifier = substr($packet, $offset, $len);
             $offset += $len;
             $len = ord($packet[$offset++]);
             $address = substr($packet, $offset, $len);
             $offset += $len;
             $port = unpack("n", substr($packet, $offset, 2))[1];
             $offset += 2;
             $clientID = Binary::readLong(substr($packet, $offset, 8));
             $this->instance->openSession($identifier, $address, $port, $clientID);
         } elseif ($id === RakLib::PACKET_CLOSE_SESSION) {
             $len = ord($packet[$offset++]);
             $identifier = substr($packet, $offset, $len);
             $offset += $len;
             $len = ord($packet[$offset++]);
             $reason = substr($packet, $offset, $len);
             $this->instance->closeSession($identifier, $reason);
         } elseif ($id === RakLib::PACKET_INVALID_SESSION) {
             $len = ord($packet[$offset++]);
             $identifier = substr($packet, $offset, $len);
             $this->instance->closeSession($identifier, "Invalid session");
         } elseif ($id === RakLib::PACKET_ACK_NOTIFICATION) {
             $len = ord($packet[$offset++]);
             $identifier = substr($packet, $offset, $len);
             $offset += $len;
             $identifierACK = PHP_INT_SIZE === 8 ? unpack("N", substr($packet, $offset, 4))[1] << 32 >> 32 : unpack("N", substr($packet, $offset, 4))[1];
             $this->instance->notifyACK($identifier, $identifierACK);
         }
         return true;
     }
     return false;
 }
Ejemplo n.º 2
0
 public function doTick()
 {
     if (!$this->rakLib->isTerminated()) {
         $this->interface->sendTick();
     } else {
         $info = $this->rakLib->getTerminationInfo();
         $this->network->unregisterInterface($this);
         \ExceptionHandler::handler(E_ERROR, "RakLib Thread crashed [" . $info["scope"] . "]: " . (isset($info["message"]) ? $info["message"] : ""), $info["file"], $info["line"]);
     }
 }
Ejemplo n.º 3
0
 public function process()
 {
     $work = false;
     if ($this->interface->handlePacket()) {
         $work = true;
         while ($this->interface->handlePacket()) {
         }
     }
     if ($this->rakLib->isTerminated()) {
         $this->network->unregisterInterface($this);
         throw new \Exception("RakLib Thread crashed");
     }
     return $work;
 }
 public function process()
 {
     $work = false;
     if ($this->interface->handlePacket()) {
         $work = true;
         while ($this->interface->handlePacket()) {
         }
     }
     if ($this->rakLib->isTerminated()) {
         $info = $this->rakLib->getTerminationInfo();
         $this->network->unregisterInterface($this);
         \ExceptionHandler::handler(E_ERROR, "RakLib Thread crashed [" . $info["scope"] . "]: " . (isset($info["message"]) ? $info["message"] : ""), $info["file"], $info["line"]);
     }
     return $work;
 }
Ejemplo n.º 5
0
 public function process()
 {
     $work = false;
     if ($this->interface->handlePacket()) {
         $work = true;
         if ($this->timeout < 0) {
             while ($this->interface->handlePacket()) {
             }
         } else {
             $timestamp = time();
             while ($this->interface->handlePacket()) {
                 if (time() - $timestamp >= $this->timeout) {
                     break;
                 }
             }
         }
     }
     if ($this->rakLib->isTerminated()) {
         $this->network->unregisterInterface($this);
         throw new \Exception("A RakLib Thread crashed!");
     }
     return $work;
 }
Ejemplo n.º 6
0
 public function receiveStream()
 {
     if (\strlen($packet = $this->server->readMainToThreadPacket()) > 0) {
         $id = \ord($packet[0]);
         $offset = 1;
         if ($id === RakLib::PACKET_ENCAPSULATED) {
             $len = \ord($packet[$offset++]);
             $identifier = \substr($packet, $offset, $len);
             $offset += $len;
             if (isset($this->sessions[$identifier])) {
                 $flags = \ord($packet[$offset++]);
                 $buffer = \substr($packet, $offset);
                 $this->sessions[$identifier]->addEncapsulatedToQueue(EncapsulatedPacket::fromBinary($buffer, \true), $flags);
             } else {
                 $this->streamInvalid($identifier);
             }
         } elseif ($id === RakLib::PACKET_RAW) {
             $len = \ord($packet[$offset++]);
             $address = \substr($packet, $offset, $len);
             $offset += $len;
             $port = \unpack("n", \substr($packet, $offset, 2))[1];
             $offset += 2;
             $payload = \substr($packet, $offset);
             $this->socket->writePacket($payload, $address, $port);
         } elseif ($id === RakLib::PACKET_CLOSE_SESSION) {
             $len = \ord($packet[$offset++]);
             $identifier = \substr($packet, $offset, $len);
             if (isset($this->sessions[$identifier])) {
                 $this->removeSession($this->sessions[$identifier]);
             } else {
                 $this->streamInvalid($identifier);
             }
         } elseif ($id === RakLib::PACKET_INVALID_SESSION) {
             $len = \ord($packet[$offset++]);
             $identifier = \substr($packet, $offset, $len);
             if (isset($this->sessions[$identifier])) {
                 $this->removeSession($this->sessions[$identifier]);
             }
         } elseif ($id === RakLib::PACKET_SET_OPTION) {
             $len = \ord($packet[$offset++]);
             $name = \substr($packet, $offset, $len);
             $offset += $len;
             $value = \substr($packet, $offset);
             switch ($name) {
                 case "name":
                     $this->name = $value;
                     break;
                 case "portChecking":
                     $this->portChecking = (bool) $value;
                     break;
                 case "packetLimit":
                     $this->packetLimit = (int) $value;
                     break;
             }
         } elseif ($id === RakLib::PACKET_BLOCK_ADDRESS) {
             $len = \ord($packet[$offset++]);
             $address = \substr($packet, $offset, $len);
             $offset += $len;
             $timeout = \PHP_INT_SIZE === 8 ? \unpack("N", \substr($packet, $offset, 4))[1] << 32 >> 32 : \unpack("N", \substr($packet, $offset, 4))[1];
             $this->blockAddress($address, $timeout);
         } elseif ($id === RakLib::PACKET_SHUTDOWN) {
             foreach ($this->sessions as $session) {
                 $this->removeSession($session);
             }
             $this->socket->close();
             $this->shutdown = \true;
         } elseif ($id === RakLib::PACKET_EMERGENCY_SHUTDOWN) {
             $this->shutdown = \true;
         } else {
             return \false;
         }
         return \true;
     }
     return \false;
 }
Ejemplo n.º 7
0
 public function receiveStream()
 {
     if (strlen($packet = $this->server->readMainToThreadPacket()) > 0) {
         $id = ord($packet[0]);
         $offset = 1;
         if ($id === RakLib::PACKET_ENCAPSULATED) {
             $len = ord($packet[$offset++]);
             $identifier = substr($packet, $offset, $len);
             $offset += $len;
             if (isset($this->sessions[$identifier])) {
                 $flags = ord($packet[$offset++]);
                 $buffer = substr($packet, $offset);
                 $this->sessions[$identifier]->addEncapsulatedToQueue(EncapsulatedPacket::fromBinary($buffer, true), $flags);
             } else {
                 $this->streamInvalid($identifier);
             }
         } elseif ($id === RakLib::PACKET_RAW) {
             $len = ord($packet[$offset++]);
             $address = substr($packet, $offset, $len);
             $offset += $len;
             $port = unpack("n", substr($packet, $offset, 2))[1];
             $offset += 2;
             $payload = substr($packet, $offset);
             $this->socket->writePacket($payload, $address, $port);
         } elseif ($id === RakLib::PACKET_TICK) {
             $time = microtime(true);
             foreach ($this->sessions as $session) {
                 $session->update($time);
             }
             foreach ($this->ipSec as $address => $count) {
                 if ($count >= $this->packetLimit) {
                     $this->blockAddress($address);
                 }
             }
             $this->ipSec = [];
             if (($this->ticks & 0b1111) === 0) {
                 $diff = max(0.005, $time - $this->lastMeasure);
                 $this->streamOption("bandwidth", serialize(["up" => $this->sendBytes / $diff, "down" => $this->receiveBytes / $diff]));
                 $this->lastMeasure = $time;
                 $this->sendBytes = 0;
                 $this->receiveBytes = 0;
                 if (count($this->block) > 0) {
                     asort($this->block);
                     $now = microtime(true);
                     foreach ($this->block as $address => $timeout) {
                         if ($timeout <= $now) {
                             unset($this->block[$address]);
                         } else {
                             break;
                         }
                     }
                 }
                 \gc_collect_cycles();
             }
             ++$this->ticks;
         } elseif ($id === RakLib::PACKET_CLOSE_SESSION) {
             $len = ord($packet[$offset++]);
             $identifier = substr($packet, $offset, $len);
             if (isset($this->sessions[$identifier])) {
                 $this->removeSession($this->sessions[$identifier]);
             } else {
                 $this->streamInvalid($identifier);
             }
         } elseif ($id === RakLib::PACKET_INVALID_SESSION) {
             $len = ord($packet[$offset++]);
             $identifier = substr($packet, $offset, $len);
             if (isset($this->sessions[$identifier])) {
                 $this->removeSession($this->sessions[$identifier]);
             }
         } elseif ($id === RakLib::PACKET_SET_OPTION) {
             $len = ord($packet[$offset++]);
             $name = substr($packet, $offset, $len);
             $offset += $len;
             $value = substr($packet, $offset);
             switch ($name) {
                 case "name":
                     $this->name = $value;
                     break;
                 case "portChecking":
                     $this->portChecking = (bool) $value;
                     break;
                 case "packetLimit":
                     $this->packetLimit = (int) $value;
                     break;
             }
         } elseif ($id === RakLib::PACKET_BLOCK_ADDRESS) {
             $len = ord($packet[$offset++]);
             $address = substr($packet, $offset, $len);
             $offset += $len;
             $timeout = PHP_INT_SIZE === 8 ? unpack("N", substr($packet, $offset, 4))[1] << 32 >> 32 : unpack("N", substr($packet, $offset, 4))[1];
             $this->blockAddress($address, $timeout);
         } elseif ($id === RakLib::PACKET_SHUTDOWN) {
             foreach ($this->sessions as $session) {
                 $this->removeSession($session);
             }
             $this->socket->close();
             $this->shutdown = true;
         } elseif ($id === RakLib::PACKET_EMERGENCY_SHUTDOWN) {
             $this->shutdown = true;
         } else {
             return false;
         }
         return true;
     }
     return false;
 }