raklib\protocol\EncapsulatedPacket::fromBinary PHP Method

fromBinary() public static method

public static fromBinary ( string $binary, boolean $internal = false, &$offset = null ) : EncapsulatedPacket
$binary string
$internal boolean
return EncapsulatedPacket
    public static function fromBinary($binary, $internal = false, &$offset = null)
    {
        $packet = new EncapsulatedPacket();
        $flags = ord($binary[0]);
        $packet->reliability = $reliability = ($flags & 0b11100000) >> 5;
        $packet->hasSplit = $hasSplit = ($flags & 0b10000) > 0;
        if ($internal) {
            $length = Binary::readInt(substr($binary, 1, 4));
            $packet->identifierACK = Binary::readInt(substr($binary, 5, 4));
            $offset = 9;
        } else {
            $length = (int) ceil(Binary::readShort(substr($binary, 1, 2)) / 8);
            $offset = 3;
            $packet->identifierACK = null;
        }
        if ($reliability > PacketReliability::UNRELIABLE) {
            if ($reliability >= PacketReliability::RELIABLE and $reliability !== PacketReliability::UNRELIABLE_WITH_ACK_RECEIPT) {
                $packet->messageIndex = Binary::readLTriad(substr($binary, $offset, 3));
                $offset += 3;
            }
            if ($reliability <= PacketReliability::RELIABLE_SEQUENCED and $reliability !== PacketReliability::RELIABLE) {
                $packet->orderIndex = Binary::readLTriad(substr($binary, $offset, 3));
                $offset += 3;
                $packet->orderChannel = ord($binary[$offset++]);
            }
        }
        if ($hasSplit) {
            $packet->splitCount = Binary::readInt(substr($binary, $offset, 4));
            $offset += 4;
            $packet->splitID = Binary::readShort(substr($binary, $offset, 2));
            $offset += 2;
            $packet->splitIndex = Binary::readInt(substr($binary, $offset, 4));
            $offset += 4;
        }
        $packet->buffer = substr($binary, $offset, $length);
        $offset += $length;
        return $packet;
    }

Usage Example

 /**
  * @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;
 }
All Usage Examples Of raklib\protocol\EncapsulatedPacket::fromBinary