Skip to content

Commit f14d04d

Browse files
committed
Port code to use ext-encoding
this is a test run to see what it's like to work with the new API. So far, it seems to be OK, although I keep forgetting the parameters for the static methods, which is annoying. Instinctively I'm reaching for instance methods, and I have to keep reminding myself why I chose to implement the new API this way instead of using instance methods (it's because this design is more resistant to mistakes).
1 parent a28d052 commit f14d04d

26 files changed

+308
-218
lines changed

src/generic/Session.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
namespace raklib\generic;
1818

19+
use pmmp\encoding\ByteBufferReader;
20+
use pmmp\encoding\ByteBufferWriter;
1921
use raklib\protocol\ACK;
2022
use raklib\protocol\AcknowledgePacket;
2123
use raklib\protocol\ConnectedPacket;
@@ -28,7 +30,6 @@
2830
use raklib\protocol\NACK;
2931
use raklib\protocol\Packet;
3032
use raklib\protocol\PacketReliability;
31-
use raklib\protocol\PacketSerializer;
3233
use raklib\utils\InternetAddress;
3334
use function hrtime;
3435
use function intdiv;
@@ -220,13 +221,13 @@ public function update(float $time) : void{
220221
}
221222

222223
protected function queueConnectedPacket(ConnectedPacket $packet, int $reliability, int $orderChannel, bool $immediate = false) : void{
223-
$out = new PacketSerializer(); //TODO: reuse streams to reduce allocations
224+
$out = new ByteBufferWriter(); //TODO: reuse streams to reduce allocations
224225
$packet->encode($out);
225226

226227
$encapsulated = new EncapsulatedPacket();
227228
$encapsulated->reliability = $reliability;
228229
$encapsulated->orderChannel = $orderChannel;
229-
$encapsulated->buffer = $out->getBuffer();
230+
$encapsulated->buffer = $out->getData();
230231

231232
$this->sendLayer->addEncapsulatedToQueue($encapsulated, $immediate);
232233
}
@@ -248,14 +249,14 @@ private function handleEncapsulatedPacketRoute(EncapsulatedPacket $packet) : voi
248249
$this->handleRemoteDisconnect();
249250
}elseif($id === MessageIdentifiers::ID_CONNECTED_PING){
250251
$dataPacket = new ConnectedPing();
251-
$dataPacket->decode(new PacketSerializer($packet->buffer));
252+
$dataPacket->decode(new ByteBufferReader($packet->buffer));
252253
$this->queueConnectedPacket(ConnectedPong::create(
253254
$dataPacket->sendPingTime,
254255
$this->getRakNetTimeMS()
255256
), PacketReliability::UNRELIABLE, 0);
256257
}elseif($id === MessageIdentifiers::ID_CONNECTED_PONG){
257258
$dataPacket = new ConnectedPong();
258-
$dataPacket->decode(new PacketSerializer($packet->buffer));
259+
$dataPacket->decode(new ByteBufferReader($packet->buffer));
259260

260261
$this->handlePong($dataPacket->sendPingTime, $dataPacket->sendPongTime);
261262
}

src/protocol/AcknowledgePacket.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616

1717
namespace raklib\protocol;
1818

19-
use pocketmine\utils\Binary;
20-
use function chr;
19+
use pmmp\encoding\BE;
20+
use pmmp\encoding\Byte;
21+
use pmmp\encoding\ByteBufferReader;
22+
use pmmp\encoding\ByteBufferWriter;
23+
use pmmp\encoding\LE;
2124
use function count;
2225
use function sort;
26+
use function strlen;
2327
use const SORT_NUMERIC;
2428

2529
abstract class AcknowledgePacket extends Packet{
@@ -29,8 +33,9 @@ abstract class AcknowledgePacket extends Packet{
2933
/** @var int[] */
3034
public array $packets = [];
3135

32-
protected function encodePayload(PacketSerializer $out) : void{
36+
protected function encodePayload(ByteBufferWriter $out) : void{
3337
$payload = "";
38+
$subWriter = new ByteBufferWriter();
3439
sort($this->packets, SORT_NUMERIC);
3540
$count = count($this->packets);
3641
$records = 0;
@@ -47,50 +52,51 @@ protected function encodePayload(PacketSerializer $out) : void{
4752
$last = $current;
4853
}elseif($diff > 1){ //Forget about duplicated packets (bad queues?)
4954
if($start === $last){
50-
$payload .= chr(self::RECORD_TYPE_SINGLE);
51-
$payload .= Binary::writeLTriad($start);
55+
Byte::writeUnsigned($subWriter, self::RECORD_TYPE_SINGLE);
56+
LE::writeUnsignedTriad($subWriter, $start);
5257
$start = $last = $current;
5358
}else{
54-
$payload .= chr(self::RECORD_TYPE_RANGE);
55-
$payload .= Binary::writeLTriad($start);
56-
$payload .= Binary::writeLTriad($last);
59+
Byte::writeUnsigned($subWriter, self::RECORD_TYPE_RANGE);
60+
LE::writeUnsignedTriad($subWriter, $start);
61+
LE::writeUnsignedTriad($subWriter, $last);
5762
$start = $last = $current;
5863
}
5964
++$records;
6065
}
6166
}
6267

6368
if($start === $last){
64-
$payload .= chr(self::RECORD_TYPE_SINGLE);
65-
$payload .= Binary::writeLTriad($start);
69+
Byte::writeUnsigned($subWriter, self::RECORD_TYPE_SINGLE);
70+
LE::writeUnsignedTriad($subWriter, $start);
6671
}else{
67-
$payload .= chr(self::RECORD_TYPE_RANGE);
68-
$payload .= Binary::writeLTriad($start);
69-
$payload .= Binary::writeLTriad($last);
72+
Byte::writeUnsigned($subWriter, self::RECORD_TYPE_RANGE);
73+
LE::writeUnsignedTriad($subWriter, $start);
74+
LE::writeUnsignedTriad($subWriter, $last);
7075
}
7176
++$records;
7277
}
7378

74-
$out->putShort($records);
75-
$out->put($payload);
79+
BE::writeUnsignedShort($out, $records);
80+
$out->writeByteArray($payload);
7681
}
7782

78-
protected function decodePayload(PacketSerializer $in) : void{
79-
$count = $in->getShort();
83+
protected function decodePayload(ByteBufferReader $in) : void{
84+
$count = BE::readUnsignedShort($in);
8085
$this->packets = [];
8186
$cnt = 0;
82-
for($i = 0; $i < $count and !$in->feof() and $cnt < 4096; ++$i){
83-
if($in->getByte() === self::RECORD_TYPE_RANGE){
84-
$start = $in->getLTriad();
85-
$end = $in->getLTriad();
87+
$len = strlen($in->getData());
88+
for($i = 0; $i < $count and $in->getOffset() < $len and $cnt < 4096; ++$i){
89+
if(Byte::readUnsigned($in) === self::RECORD_TYPE_RANGE){
90+
$start = LE::readUnsignedTriad($in);
91+
$end = LE::readUnsignedTriad($in);
8692
if(($end - $start) > 512){
8793
$end = $start + 512;
8894
}
8995
for($c = $start; $c <= $end; ++$c){
9096
$this->packets[$cnt++] = $c;
9197
}
9298
}else{
93-
$this->packets[$cnt++] = $in->getLTriad();
99+
$this->packets[$cnt++] = LE::readUnsignedTriad($in);
94100
}
95101
}
96102
}

src/protocol/AdvertiseSystem.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616

1717
namespace raklib\protocol;
1818

19+
use pmmp\encoding\ByteBufferReader;
20+
use pmmp\encoding\ByteBufferWriter;
21+
1922
class AdvertiseSystem extends Packet{
2023
public static $ID = MessageIdentifiers::ID_ADVERTISE_SYSTEM;
2124

2225
public string $serverName;
2326

24-
protected function encodePayload(PacketSerializer $out) : void{
25-
$out->putString($this->serverName);
27+
protected function encodePayload(ByteBufferWriter $out) : void{
28+
PacketSerializer::putString($out, $this->serverName);
2629
}
2730

28-
protected function decodePayload(PacketSerializer $in) : void{
29-
$this->serverName = $in->getString();
31+
protected function decodePayload(ByteBufferReader $in) : void{
32+
$this->serverName = PacketSerializer::getString($in);
3033
}
3134
}

src/protocol/ConnectedPing.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
namespace raklib\protocol;
1818

19+
use pmmp\encoding\BE;
20+
use pmmp\encoding\ByteBufferReader;
21+
use pmmp\encoding\ByteBufferWriter;
22+
1923
class ConnectedPing extends ConnectedPacket{
2024
public static $ID = MessageIdentifiers::ID_CONNECTED_PING;
2125

@@ -27,11 +31,11 @@ public static function create(int $sendPingTime) : self{
2731
return $result;
2832
}
2933

30-
protected function encodePayload(PacketSerializer $out) : void{
31-
$out->putLong($this->sendPingTime);
34+
protected function encodePayload(ByteBufferWriter $out) : void{
35+
BE::writeUnsignedLong($out, $this->sendPingTime);
3236
}
3337

34-
protected function decodePayload(PacketSerializer $in) : void{
35-
$this->sendPingTime = $in->getLong();
38+
protected function decodePayload(ByteBufferReader $in) : void{
39+
$this->sendPingTime = BE::readUnsignedLong($in);
3640
}
3741
}

src/protocol/ConnectedPong.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
namespace raklib\protocol;
1818

19+
use pmmp\encoding\BE;
20+
use pmmp\encoding\ByteBufferReader;
21+
use pmmp\encoding\ByteBufferWriter;
22+
1923
class ConnectedPong extends ConnectedPacket{
2024
public static $ID = MessageIdentifiers::ID_CONNECTED_PONG;
2125

@@ -29,13 +33,13 @@ public static function create(int $sendPingTime, int $sendPongTime) : self{
2933
return $result;
3034
}
3135

32-
protected function encodePayload(PacketSerializer $out) : void{
33-
$out->putLong($this->sendPingTime);
34-
$out->putLong($this->sendPongTime);
36+
protected function encodePayload(ByteBufferWriter $out) : void{
37+
BE::writeUnsignedLong($out, $this->sendPingTime);
38+
BE::writeUnsignedLong($out, $this->sendPongTime);
3539
}
3640

37-
protected function decodePayload(PacketSerializer $in) : void{
38-
$this->sendPingTime = $in->getLong();
39-
$this->sendPongTime = $in->getLong();
41+
protected function decodePayload(ByteBufferReader $in) : void{
42+
$this->sendPingTime = BE::readUnsignedLong($in);
43+
$this->sendPongTime = BE::readUnsignedLong($in);
4044
}
4145
}

src/protocol/ConnectionRequest.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,27 @@
1616

1717
namespace raklib\protocol;
1818

19+
use pmmp\encoding\BE;
20+
use pmmp\encoding\Byte;
21+
use pmmp\encoding\ByteBufferReader;
22+
use pmmp\encoding\ByteBufferWriter;
23+
1924
class ConnectionRequest extends ConnectedPacket{
2025
public static $ID = MessageIdentifiers::ID_CONNECTION_REQUEST;
2126

2227
public int $clientID;
2328
public int $sendPingTime;
2429
public bool $useSecurity = false;
2530

26-
protected function encodePayload(PacketSerializer $out) : void{
27-
$out->putLong($this->clientID);
28-
$out->putLong($this->sendPingTime);
29-
$out->putByte($this->useSecurity ? 1 : 0);
31+
protected function encodePayload(ByteBufferWriter $out) : void{
32+
BE::writeUnsignedLong($out, $this->clientID);
33+
BE::writeUnsignedLong($out, $this->sendPingTime);
34+
Byte::writeUnsigned($out, $this->useSecurity ? 1 : 0);
3035
}
3136

32-
protected function decodePayload(PacketSerializer $in) : void{
33-
$this->clientID = $in->getLong();
34-
$this->sendPingTime = $in->getLong();
35-
$this->useSecurity = $in->getByte() !== 0;
37+
protected function decodePayload(ByteBufferReader $in) : void{
38+
$this->clientID = BE::readUnsignedLong($in);
39+
$this->sendPingTime = BE::readUnsignedLong($in);
40+
$this->useSecurity = Byte::readUnsigned($in) !== 0;
3641
}
3742
}

src/protocol/ConnectionRequestAccepted.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
namespace raklib\protocol;
1818

19+
use pmmp\encoding\BE;
20+
use pmmp\encoding\ByteBufferReader;
21+
use pmmp\encoding\ByteBufferWriter;
1922
use raklib\RakLib;
2023
use raklib\utils\InternetAddress;
2124
use function strlen;
@@ -45,31 +48,31 @@ public function __construct(){
4548
$this->systemAddresses[] = new InternetAddress("127.0.0.1", 0, 4);
4649
}
4750

48-
protected function encodePayload(PacketSerializer $out) : void{
49-
$out->putAddress($this->address);
50-
$out->putShort(0);
51+
protected function encodePayload(ByteBufferWriter $out) : void{
52+
PacketSerializer::putAddress($out, $this->address);
53+
BE::writeUnsignedShort($out, 0);
5154

5255
$dummy = new InternetAddress("0.0.0.0", 0, 4);
5356
for($i = 0; $i < RakLib::$SYSTEM_ADDRESS_COUNT; ++$i){
54-
$out->putAddress($this->systemAddresses[$i] ?? $dummy);
57+
PacketSerializer::putAddress($out, $this->systemAddresses[$i] ?? $dummy);
5558
}
5659

57-
$out->putLong($this->sendPingTime);
58-
$out->putLong($this->sendPongTime);
60+
BE::writeUnsignedLong($out, $this->sendPingTime);
61+
BE::writeUnsignedLong($out, $this->sendPongTime);
5962
}
6063

61-
protected function decodePayload(PacketSerializer $in) : void{
62-
$this->address = $in->getAddress();
63-
$in->getShort(); //TODO: check this
64+
protected function decodePayload(ByteBufferReader $in) : void{
65+
$this->address = PacketSerializer::getAddress($in);
66+
BE::readUnsignedShort($in); //TODO: check this
6467

65-
$len = strlen($in->getBuffer());
68+
$len = strlen($in->getData());
6669
$dummy = new InternetAddress("0.0.0.0", 0, 4);
6770

6871
for($i = 0; $i < RakLib::$SYSTEM_ADDRESS_COUNT; ++$i){
69-
$this->systemAddresses[$i] = $in->getOffset() + 16 < $len ? $in->getAddress() : $dummy; //HACK: avoids trying to read too many addresses on bad data
72+
$this->systemAddresses[$i] = $in->getOffset() + 16 < $len ? PacketSerializer::getAddress($in) : $dummy; //HACK: avoids trying to read too many addresses on bad data
7073
}
7174

72-
$this->sendPingTime = $in->getLong();
73-
$this->sendPongTime = $in->getLong();
75+
$this->sendPingTime = BE::readUnsignedLong($in);
76+
$this->sendPongTime = BE::readUnsignedLong($in);
7477
}
7578
}

src/protocol/Datagram.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
namespace raklib\protocol;
1818

19+
use pmmp\encoding\Byte;
20+
use pmmp\encoding\ByteBufferReader;
21+
use pmmp\encoding\ByteBufferWriter;
22+
use pmmp\encoding\LE;
23+
use function strlen;
24+
1925
class Datagram extends Packet{
2026
public const BITFLAG_VALID = 0x80;
2127
public const BITFLAG_ACK = 0x40;
@@ -36,14 +42,14 @@ class Datagram extends Packet{
3642
public array $packets = [];
3743
public int $seqNumber;
3844

39-
protected function encodeHeader(PacketSerializer $out) : void{
40-
$out->putByte(self::BITFLAG_VALID | $this->headerFlags);
45+
protected function encodeHeader(ByteBufferWriter $out) : void{
46+
Byte::writeUnsigned($out, self::BITFLAG_VALID | $this->headerFlags);
4147
}
4248

43-
protected function encodePayload(PacketSerializer $out) : void{
44-
$out->putLTriad($this->seqNumber);
49+
protected function encodePayload(ByteBufferWriter $out) : void{
50+
LE::writeUnsignedTriad($out, $this->seqNumber);
4551
foreach($this->packets as $packet){
46-
$out->put($packet->toBinary());
52+
$packet->toBinary($out);
4753
}
4854
}
4955

@@ -59,14 +65,15 @@ public function length(){
5965
return $length;
6066
}
6167

62-
protected function decodeHeader(PacketSerializer $in) : void{
63-
$this->headerFlags = $in->getByte();
68+
protected function decodeHeader(ByteBufferReader $in) : void{
69+
$this->headerFlags = Byte::readUnsigned($in);
6470
}
6571

66-
protected function decodePayload(PacketSerializer $in) : void{
67-
$this->seqNumber = $in->getLTriad();
72+
protected function decodePayload(ByteBufferReader $in) : void{
73+
$this->seqNumber = LE::readUnsignedTriad($in);
6874

69-
while(!$in->feof()){
75+
$len = strlen($in->getData());
76+
while($in->getOffset() < $len){
7077
$this->packets[] = EncapsulatedPacket::fromBinary($in);
7178
}
7279
}

src/protocol/DisconnectionNotification.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616

1717
namespace raklib\protocol;
1818

19+
use pmmp\encoding\ByteBufferReader;
20+
use pmmp\encoding\ByteBufferWriter;
21+
1922
class DisconnectionNotification extends ConnectedPacket{
2023
public static $ID = MessageIdentifiers::ID_DISCONNECTION_NOTIFICATION;
2124

22-
protected function encodePayload(PacketSerializer $out) : void{
25+
protected function encodePayload(ByteBufferWriter $out) : void{
2326

2427
}
2528

26-
protected function decodePayload(PacketSerializer $in) : void{
29+
protected function decodePayload(ByteBufferReader $in) : void{
2730

2831
}
2932
}

0 commit comments

Comments
 (0)