PocketMine-MP 5.21.1 git-2ff647079265e7c600203af4fd902b15e99d49a4
Datagram.php
1<?php
2
3/*
4 * This file is part of RakLib.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/RakLib>
6 *
7 * RakLib is not affiliated with Jenkins Software LLC nor RakNet.
8 *
9 * RakLib is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 */
14
15declare(strict_types=1);
16
17namespace raklib\protocol;
18
19class Datagram extends Packet{
20 public const BITFLAG_VALID = 0x80;
21 public const BITFLAG_ACK = 0x40;
22 public const BITFLAG_NAK = 0x20; // hasBAndAS for ACKs
23
24 /*
25 * These flags can be set on regular datagrams, but they are useless as per the public version of RakNet
26 * (the receiving client will not use them or pay any attention to them).
27 */
28 public const BITFLAG_PACKET_PAIR = 0x10;
29 public const BITFLAG_CONTINUOUS_SEND = 0x08;
30 public const BITFLAG_NEEDS_B_AND_AS = 0x04;
31
32 public const HEADER_SIZE = 1 + 3; //header flags (1) + sequence number (3)
33
34 public int $headerFlags = 0;
36 public array $packets = [];
37 public int $seqNumber;
38
39 protected function encodeHeader(PacketSerializer $out) : void{
40 $out->putByte(self::BITFLAG_VALID | $this->headerFlags);
41 }
42
43 protected function encodePayload(PacketSerializer $out) : void{
44 $out->putLTriad($this->seqNumber);
45 foreach($this->packets as $packet){
46 $out->put($packet->toBinary());
47 }
48 }
49
53 public function length(){
54 $length = self::HEADER_SIZE;
55 foreach($this->packets as $packet){
56 $length += $packet->getTotalLength();
57 }
58
59 return $length;
60 }
61
62 protected function decodeHeader(PacketSerializer $in) : void{
63 $this->headerFlags = $in->getByte();
64 }
65
66 protected function decodePayload(PacketSerializer $in) : void{
67 $this->seqNumber = $in->getLTriad();
68
69 while(!$in->feof()){
70 $this->packets[] = EncapsulatedPacket::fromBinary($in);
71 }
72 }
73}
decodeHeader(PacketSerializer $in)
Definition: Datagram.php:62
decodePayload(PacketSerializer $in)
Definition: Datagram.php:66
static fromBinary(BinaryStream $stream)