Skip to content

Commit 9f0e325

Browse files
committed
Fully remove BinaryUtils dependency
1 parent d471383 commit 9f0e325

File tree

9 files changed

+26
-32
lines changed

9 files changed

+26
-32
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"php": "^8.1",
88
"php-ipv6": "*",
99
"php-64bit": "*",
10+
"ext-encoding": "~1.0.0",
1011
"ext-sockets": "*",
11-
"pocketmine/log": "^0.3.0 || ^0.4.0",
12-
"pocketmine/binaryutils": "^0.2.0"
12+
"pocketmine/log": "^0.3.0 || ^0.4.0"
1313
},
1414
"require-dev": {
1515
"phpstan/phpstan": "2.1.0",

src/protocol/EncapsulatedPacket.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
use pmmp\encoding\Byte;
2121
use pmmp\encoding\ByteBufferReader;
2222
use pmmp\encoding\ByteBufferWriter;
23+
use pmmp\encoding\DataDecodeException;
2324
use pmmp\encoding\LE;
24-
use pocketmine\utils\BinaryDataException;
2525
use function ceil;
2626
use function strlen;
2727

@@ -43,7 +43,7 @@ class EncapsulatedPacket{
4343
public ?int $identifierACK = null;
4444

4545
/**
46-
* @throws BinaryDataException
46+
* @throws DataDecodeException
4747
*/
4848
public static function fromBinary(ByteBufferReader $stream) : EncapsulatedPacket{
4949
$packet = new EncapsulatedPacket();
@@ -54,7 +54,7 @@ public static function fromBinary(ByteBufferReader $stream) : EncapsulatedPacket
5454

5555
$length = (int) ceil(BE::readUnsignedShort($stream) / 8);
5656
if($length === 0){
57-
throw new BinaryDataException("Encapsulated payload length cannot be zero");
57+
throw new DataDecodeException("Encapsulated payload length cannot be zero");
5858
}
5959

6060
if(PacketReliability::isReliable($reliability)){

src/protocol/OfflineMessage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
use pmmp\encoding\ByteBufferReader;
2020
use pmmp\encoding\ByteBufferWriter;
21-
use pocketmine\utils\BinaryDataException;
21+
use pmmp\encoding\DataDecodeException;
2222

2323
abstract class OfflineMessage extends Packet{
2424

@@ -31,7 +31,7 @@ abstract class OfflineMessage extends Packet{
3131

3232
/**
3333
* @return void
34-
* @throws BinaryDataException
34+
* @throws DataDecodeException
3535
*/
3636
protected function readMagic(ByteBufferReader $in){
3737
$this->magic = $in->readByteArray(16);

src/protocol/Packet.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use pmmp\encoding\Byte;
2020
use pmmp\encoding\ByteBufferReader;
2121
use pmmp\encoding\ByteBufferWriter;
22-
use pocketmine\utils\BinaryDataException;
22+
use pmmp\encoding\DataDecodeException;
2323

2424
abstract class Packet{
2525
/** @var int */
@@ -37,22 +37,22 @@ protected function encodeHeader(ByteBufferWriter $out) : void{
3737
abstract protected function encodePayload(ByteBufferWriter $out) : void;
3838

3939
/**
40-
* @throws BinaryDataException
40+
* @throws DataDecodeException
4141
*/
4242
public function decode(ByteBufferReader $in) : void{
4343
$this->decodeHeader($in);
4444
$this->decodePayload($in);
4545
}
4646

4747
/**
48-
* @throws BinaryDataException
48+
* @throws DataDecodeException
4949
*/
5050
protected function decodeHeader(ByteBufferReader $in) : void{
5151
Byte::readUnsigned($in); //PID
5252
}
5353

5454
/**
55-
* @throws BinaryDataException
55+
* @throws DataDecodeException
5656
*/
5757
abstract protected function decodePayload(ByteBufferReader $in) : void;
5858
}

src/protocol/PacketSerializer.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
use pmmp\encoding\Byte;
2121
use pmmp\encoding\ByteBufferReader;
2222
use pmmp\encoding\ByteBufferWriter;
23+
use pmmp\encoding\DataDecodeException;
2324
use pmmp\encoding\LE;
24-
use pocketmine\utils\BinaryDataException;
25-
use pocketmine\utils\BinaryStream;
2625
use raklib\utils\InternetAddress;
2726
use function assert;
2827
use function count;
@@ -32,17 +31,20 @@
3231
use function strlen;
3332
use const AF_INET6;
3433

35-
final class PacketSerializer extends BinaryStream{
34+
final class PacketSerializer{
35+
private function __construct(){
36+
//NOOP
37+
}
3638

3739
/**
38-
* @throws BinaryDataException
40+
* @throws DataDecodeException
3941
*/
4042
public static function getString(ByteBufferReader $in) : string{
4143
return $in->readByteArray(BE::readUnsignedShort($in));
4244
}
4345

4446
/**
45-
* @throws BinaryDataException
47+
* @throws DataDecodeException
4648
*/
4749
public static function getAddress(ByteBufferReader $in) : InternetAddress{
4850
$version = Byte::readUnsigned($in);
@@ -57,12 +59,12 @@ public static function getAddress(ByteBufferReader $in) : InternetAddress{
5759
BE::readUnsignedInt($in); //flow info
5860
$addr = inet_ntop($in->readByteArray(16));
5961
if($addr === false){
60-
throw new BinaryDataException("Failed to parse IPv6 address");
62+
throw new DataDecodeException("Failed to parse IPv6 address");
6163
}
6264
BE::readUnsignedInt($in); //scope ID
6365
return new InternetAddress($addr, $port, $version);
6466
}else{
65-
throw new BinaryDataException("Unknown IP address version $version");
67+
throw new DataDecodeException("Unknown IP address version $version");
6668
}
6769
}
6870

src/server/Server.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
use pmmp\encoding\ByteBufferReader;
2020
use pmmp\encoding\ByteBufferWriter;
21-
use pocketmine\utils\BinaryDataException;
21+
use pmmp\encoding\DataDecodeException;
2222
use raklib\generic\DisconnectReason;
2323
use raklib\generic\PacketHandlingException;
2424
use raklib\generic\Session;
@@ -304,7 +304,7 @@ private function receivePacket() : bool{
304304
$this->logger->debug("Ignored packet from $address due to no session opened (0x" . bin2hex($buffer[0]) . ")");
305305
}
306306
}
307-
}catch(BinaryDataException $e){
307+
}catch(DataDecodeException $e){
308308
if($this->packetErrorsSinceLastUpdate < $this->packetErrorSuppressionThreshold){
309309
$logFn = function() use ($address, $e, $buffer) : void{
310310
$this->logger->debug("Packet from $address (" . strlen($buffer) . " bytes): 0x" . bin2hex($buffer));

src/server/UnconnectedMessageHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
namespace raklib\server;
1818

1919
use pmmp\encoding\ByteBufferReader;
20-
use pocketmine\utils\BinaryDataException;
20+
use pmmp\encoding\DataDecodeException;
2121
use raklib\generic\Session;
2222
use raklib\protocol\IncompatibleProtocolVersion;
2323
use raklib\protocol\MessageIdentifiers;
@@ -51,7 +51,7 @@ public function __construct(
5151
}
5252

5353
/**
54-
* @throws BinaryDataException
54+
* @throws DataDecodeException
5555
*/
5656
public function handleRaw(string $payload, InternetAddress $address) : bool{
5757
if($payload === ""){

tools/proxy.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,11 @@
2121

2222
declare(strict_types=1);
2323

24-
use pocketmine\utils\Limits;
2524
use raklib\client\ClientSocket;
2625
use raklib\generic\SocketException;
2726
use raklib\protocol\MessageIdentifiers;
28-
use raklib\protocol\UnconnectedPong;
29-
use raklib\server\ProtocolAcceptor;
30-
use raklib\server\Server;
3127
use raklib\server\ServerSocket;
32-
use raklib\server\SimpleProtocolAcceptor;
3328
use raklib\utils\InternetAddress;
34-
use raklib\generic\Socket;
3529

3630
require dirname(__DIR__) . '/vendor/autoload.php';
3731

@@ -127,7 +121,7 @@ function serverToClientRelay(ClientSession $client, ServerSocket $clientProxySoc
127121
/** @var ClientSession[][] $clients */
128122
$clients = [];
129123

130-
$serverId = mt_rand(0, Limits::INT32_MAX);
124+
$serverId = mt_rand(0, 0x7f_ff_ff_ff);
131125
$mostRecentPong = null;
132126

133127
while(true){

tools/scan.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
declare(strict_types=1);
44

55
use pmmp\encoding\ByteBufferWriter;
6-
use pocketmine\utils\Limits;
76
use raklib\generic\SocketException;
87
use raklib\protocol\MessageIdentifiers;
9-
use raklib\protocol\PacketSerializer;
108
use raklib\protocol\UnconnectedPing;
119
use raklib\server\ServerSocket;
1210
use raklib\utils\InternetAddress;
@@ -29,7 +27,7 @@
2927

3028
$socket = new ServerSocket($bindAddress);
3129
$socket->enableBroadcast();
32-
$clientId = mt_rand(0, Limits::INT32_MAX);
30+
$clientId = mt_rand(0, 0x7f_ff_ff_ff);
3331
\GlobalLogger::get()->info("Listening on " . $bindAddress);
3432
\GlobalLogger::get()->info("Press CTRL+C to stop");
3533

0 commit comments

Comments
 (0)