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; }
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; }