15declare(strict_types=1);
17namespace raklib\protocol;
27 private const RELIABILITY_SHIFT = 5;
28 private const RELIABILITY_FLAGS = 0b111 << self::RELIABILITY_SHIFT;
30 private const SPLIT_FLAG = 0b00010000;
32 public const SPLIT_INFO_LENGTH = 4 + 2 + 4;
34 public int $reliability;
35 public ?
int $messageIndex =
null;
36 public ?
int $sequenceIndex =
null;
37 public ?
int $orderIndex =
null;
38 public ?
int $orderChannel =
null;
40 public string $buffer =
"";
41 public ?
int $identifierACK =
null;
50 $packet->reliability = $reliability = ($flags & self::RELIABILITY_FLAGS) >> self::RELIABILITY_SHIFT;
51 $hasSplit = ($flags & self::SPLIT_FLAG) !== 0;
53 $length = (int) ceil($stream->
getShort() / 8);
58 if(PacketReliability::isReliable($reliability)){
59 $packet->messageIndex = $stream->
getLTriad();
62 if(PacketReliability::isSequenced($reliability)){
63 $packet->sequenceIndex = $stream->
getLTriad();
66 if(PacketReliability::isSequencedOrOrdered($reliability)){
67 $packet->orderIndex = $stream->
getLTriad();
68 $packet->orderChannel = $stream->
getByte();
72 $splitCount = $stream->
getInt();
74 $splitIndex = $stream->
getInt();
75 $packet->splitInfo =
new SplitPacketInfo($splitID, $splitIndex, $splitCount);
78 $packet->buffer = $stream->
get($length);
82 public function toBinary() : string{
84 chr(($this->reliability << self::RELIABILITY_SHIFT) | ($this->splitInfo !== null ? self::SPLIT_FLAG : 0)) .
85 Binary::writeShort(strlen($this->buffer) << 3) .
86 (PacketReliability::isReliable($this->reliability) ? Binary::writeLTriad($this->messageIndex) :
"") .
87 (PacketReliability::isSequenced($this->reliability) ? Binary::writeLTriad($this->sequenceIndex) :
"") .
88 (PacketReliability::isSequencedOrOrdered($this->reliability) ? Binary::writeLTriad($this->orderIndex) . chr($this->orderChannel) :
"") .
89 ($this->splitInfo !== null ? Binary::writeInt($this->splitInfo->getTotalPartCount()) . Binary::writeShort($this->splitInfo->getId()) . Binary::writeInt($this->splitInfo->getPartIndex()) :
"")
103 ($this->splitInfo !== null ? self::SPLIT_INFO_LENGTH : 0);
106 public function getTotalLength() : int{
107 return $this->getHeaderLength() + strlen($this->buffer);
110 public function __toString() : string{
111 return $this->toBinary();
static fromBinary(BinaryStream $stream)