/** * @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; }
public function decode() { parent::decode(); $this->seqNumber = unpack("V", $this->get(3) . "")[1]; while (!$this->feof()) { $offset = 0; $packet = EncapsulatedPacket::fromBinary(substr($this->buffer, $this->offset), false, $offset); $this->offset += $offset; if (strlen($packet->buffer) === 0) { break; } $this->packets[] = $packet; } }
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 toBinary($internal = \false) { return $this->internalData === \null ? $this->internalData = parent::toBinary($internal) : $this->internalData; }
public function close() { $data = ""; $this->addEncapsulatedToQueue(EncapsulatedPacket::fromBinary($data), RakLib::PRIORITY_IMMEDIATE); //CLIENT_DISCONNECT packet 0x15 $this->sessionManager = \null; }
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; }
public function close() { $this->addEncapsulatedToQueue(EncapsulatedPacket::fromBinary("`")); //CLIENT_DISCONNECT packet 0x15 $this->sessionManager = null; }