diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 40de7de1..b2a16c01 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,9 +17,10 @@ jobs: - uses: actions/checkout@v4 - name: Setup PHP - uses: shivammathur/setup-php@2.32.0 + uses: shivammathur/setup-php@2.35.4 with: php-version: ${{ matrix.php }} + extensions: encoding-pmmp/ext-encoding@1.0.0 - name: Cache Composer packages id: composer-cache diff --git a/composer.json b/composer.json index d94d83de..c72e30f2 100644 --- a/composer.json +++ b/composer.json @@ -7,9 +7,9 @@ "php": "^8.1", "php-ipv6": "*", "php-64bit": "*", + "ext-encoding": "~1.0.0", "ext-sockets": "*", - "pocketmine/log": "^0.3.0 || ^0.4.0", - "pocketmine/binaryutils": "^0.2.0" + "pocketmine/log": "^0.3.0 || ^0.4.0" }, "require-dev": { "phpstan/phpstan": "2.1.0", diff --git a/src/generic/Session.php b/src/generic/Session.php index 2235d15e..eef487dd 100644 --- a/src/generic/Session.php +++ b/src/generic/Session.php @@ -16,6 +16,8 @@ namespace raklib\generic; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use raklib\protocol\ACK; use raklib\protocol\AcknowledgePacket; use raklib\protocol\ConnectedPacket; @@ -28,7 +30,6 @@ use raklib\protocol\NACK; use raklib\protocol\Packet; use raklib\protocol\PacketReliability; -use raklib\protocol\PacketSerializer; use raklib\utils\InternetAddress; use function hrtime; use function intdiv; @@ -220,13 +221,13 @@ public function update(float $time) : void{ } protected function queueConnectedPacket(ConnectedPacket $packet, int $reliability, int $orderChannel, bool $immediate = false) : void{ - $out = new PacketSerializer(); //TODO: reuse streams to reduce allocations + $out = new ByteBufferWriter(); //TODO: reuse streams to reduce allocations $packet->encode($out); $encapsulated = new EncapsulatedPacket(); $encapsulated->reliability = $reliability; $encapsulated->orderChannel = $orderChannel; - $encapsulated->buffer = $out->getBuffer(); + $encapsulated->buffer = $out->getData(); $this->sendLayer->addEncapsulatedToQueue($encapsulated, $immediate); } @@ -248,14 +249,14 @@ private function handleEncapsulatedPacketRoute(EncapsulatedPacket $packet) : voi $this->handleRemoteDisconnect(); }elseif($id === MessageIdentifiers::ID_CONNECTED_PING){ $dataPacket = new ConnectedPing(); - $dataPacket->decode(new PacketSerializer($packet->buffer)); + $dataPacket->decode(new ByteBufferReader($packet->buffer)); $this->queueConnectedPacket(ConnectedPong::create( $dataPacket->sendPingTime, $this->getRakNetTimeMS() ), PacketReliability::UNRELIABLE, 0); }elseif($id === MessageIdentifiers::ID_CONNECTED_PONG){ $dataPacket = new ConnectedPong(); - $dataPacket->decode(new PacketSerializer($packet->buffer)); + $dataPacket->decode(new ByteBufferReader($packet->buffer)); $this->handlePong($dataPacket->sendPingTime, $dataPacket->sendPongTime); } diff --git a/src/protocol/AcknowledgePacket.php b/src/protocol/AcknowledgePacket.php index 6a187330..4d56317b 100644 --- a/src/protocol/AcknowledgePacket.php +++ b/src/protocol/AcknowledgePacket.php @@ -16,10 +16,14 @@ namespace raklib\protocol; -use pocketmine\utils\Binary; -use function chr; +use pmmp\encoding\BE; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; use function count; use function sort; +use function strlen; use const SORT_NUMERIC; abstract class AcknowledgePacket extends Packet{ @@ -29,8 +33,8 @@ abstract class AcknowledgePacket extends Packet{ /** @var int[] */ public array $packets = []; - protected function encodePayload(PacketSerializer $out) : void{ - $payload = ""; + protected function encodePayload(ByteBufferWriter $out) : void{ + $subWriter = new ByteBufferWriter(); sort($this->packets, SORT_NUMERIC); $count = count($this->packets); $records = 0; @@ -47,13 +51,13 @@ protected function encodePayload(PacketSerializer $out) : void{ $last = $current; }elseif($diff > 1){ //Forget about duplicated packets (bad queues?) if($start === $last){ - $payload .= chr(self::RECORD_TYPE_SINGLE); - $payload .= Binary::writeLTriad($start); + Byte::writeUnsigned($subWriter, self::RECORD_TYPE_SINGLE); + LE::writeUnsignedTriad($subWriter, $start); $start = $last = $current; }else{ - $payload .= chr(self::RECORD_TYPE_RANGE); - $payload .= Binary::writeLTriad($start); - $payload .= Binary::writeLTriad($last); + Byte::writeUnsigned($subWriter, self::RECORD_TYPE_RANGE); + LE::writeUnsignedTriad($subWriter, $start); + LE::writeUnsignedTriad($subWriter, $last); $start = $last = $current; } ++$records; @@ -61,28 +65,29 @@ protected function encodePayload(PacketSerializer $out) : void{ } if($start === $last){ - $payload .= chr(self::RECORD_TYPE_SINGLE); - $payload .= Binary::writeLTriad($start); + Byte::writeUnsigned($subWriter, self::RECORD_TYPE_SINGLE); + LE::writeUnsignedTriad($subWriter, $start); }else{ - $payload .= chr(self::RECORD_TYPE_RANGE); - $payload .= Binary::writeLTriad($start); - $payload .= Binary::writeLTriad($last); + Byte::writeUnsigned($subWriter, self::RECORD_TYPE_RANGE); + LE::writeUnsignedTriad($subWriter, $start); + LE::writeUnsignedTriad($subWriter, $last); } ++$records; } - $out->putShort($records); - $out->put($payload); + BE::writeUnsignedShort($out, $records); + $out->writeByteArray($subWriter->getData()); } - protected function decodePayload(PacketSerializer $in) : void{ - $count = $in->getShort(); + protected function decodePayload(ByteBufferReader $in) : void{ + $count = BE::readUnsignedShort($in); $this->packets = []; $cnt = 0; - for($i = 0; $i < $count and !$in->feof() and $cnt < 4096; ++$i){ - if($in->getByte() === self::RECORD_TYPE_RANGE){ - $start = $in->getLTriad(); - $end = $in->getLTriad(); + $len = strlen($in->getData()); + for($i = 0; $i < $count and $in->getOffset() < $len and $cnt < 4096; ++$i){ + if(Byte::readUnsigned($in) === self::RECORD_TYPE_RANGE){ + $start = LE::readUnsignedTriad($in); + $end = LE::readUnsignedTriad($in); if(($end - $start) > 512){ $end = $start + 512; } @@ -90,7 +95,7 @@ protected function decodePayload(PacketSerializer $in) : void{ $this->packets[$cnt++] = $c; } }else{ - $this->packets[$cnt++] = $in->getLTriad(); + $this->packets[$cnt++] = LE::readUnsignedTriad($in); } } } diff --git a/src/protocol/AdvertiseSystem.php b/src/protocol/AdvertiseSystem.php index e6beca3f..396c6cee 100644 --- a/src/protocol/AdvertiseSystem.php +++ b/src/protocol/AdvertiseSystem.php @@ -16,16 +16,19 @@ namespace raklib\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; + class AdvertiseSystem extends Packet{ public static $ID = MessageIdentifiers::ID_ADVERTISE_SYSTEM; public string $serverName; - protected function encodePayload(PacketSerializer $out) : void{ - $out->putString($this->serverName); + protected function encodePayload(ByteBufferWriter $out) : void{ + PacketSerializer::putString($out, $this->serverName); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->serverName = $in->getString(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->serverName = PacketSerializer::getString($in); } } diff --git a/src/protocol/ConnectedPing.php b/src/protocol/ConnectedPing.php index 82830af9..506c9b74 100644 --- a/src/protocol/ConnectedPing.php +++ b/src/protocol/ConnectedPing.php @@ -16,6 +16,10 @@ namespace raklib\protocol; +use pmmp\encoding\BE; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; + class ConnectedPing extends ConnectedPacket{ public static $ID = MessageIdentifiers::ID_CONNECTED_PING; @@ -27,11 +31,11 @@ public static function create(int $sendPingTime) : self{ return $result; } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLong($this->sendPingTime); + protected function encodePayload(ByteBufferWriter $out) : void{ + BE::writeUnsignedLong($out, $this->sendPingTime); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->sendPingTime = $in->getLong(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->sendPingTime = BE::readUnsignedLong($in); } } diff --git a/src/protocol/ConnectedPong.php b/src/protocol/ConnectedPong.php index bd42c870..9a5864ec 100644 --- a/src/protocol/ConnectedPong.php +++ b/src/protocol/ConnectedPong.php @@ -16,6 +16,10 @@ namespace raklib\protocol; +use pmmp\encoding\BE; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; + class ConnectedPong extends ConnectedPacket{ public static $ID = MessageIdentifiers::ID_CONNECTED_PONG; @@ -29,13 +33,13 @@ public static function create(int $sendPingTime, int $sendPongTime) : self{ return $result; } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLong($this->sendPingTime); - $out->putLong($this->sendPongTime); + protected function encodePayload(ByteBufferWriter $out) : void{ + BE::writeUnsignedLong($out, $this->sendPingTime); + BE::writeUnsignedLong($out, $this->sendPongTime); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->sendPingTime = $in->getLong(); - $this->sendPongTime = $in->getLong(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->sendPingTime = BE::readUnsignedLong($in); + $this->sendPongTime = BE::readUnsignedLong($in); } } diff --git a/src/protocol/ConnectionRequest.php b/src/protocol/ConnectionRequest.php index 94a888fd..eb4ce51e 100644 --- a/src/protocol/ConnectionRequest.php +++ b/src/protocol/ConnectionRequest.php @@ -16,6 +16,11 @@ namespace raklib\protocol; +use pmmp\encoding\BE; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; + class ConnectionRequest extends ConnectedPacket{ public static $ID = MessageIdentifiers::ID_CONNECTION_REQUEST; @@ -23,15 +28,15 @@ class ConnectionRequest extends ConnectedPacket{ public int $sendPingTime; public bool $useSecurity = false; - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLong($this->clientID); - $out->putLong($this->sendPingTime); - $out->putByte($this->useSecurity ? 1 : 0); + protected function encodePayload(ByteBufferWriter $out) : void{ + BE::writeUnsignedLong($out, $this->clientID); + BE::writeUnsignedLong($out, $this->sendPingTime); + Byte::writeUnsigned($out, $this->useSecurity ? 1 : 0); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->clientID = $in->getLong(); - $this->sendPingTime = $in->getLong(); - $this->useSecurity = $in->getByte() !== 0; + protected function decodePayload(ByteBufferReader $in) : void{ + $this->clientID = BE::readUnsignedLong($in); + $this->sendPingTime = BE::readUnsignedLong($in); + $this->useSecurity = Byte::readUnsigned($in) !== 0; } } diff --git a/src/protocol/ConnectionRequestAccepted.php b/src/protocol/ConnectionRequestAccepted.php index 275ec96b..d2467ee2 100644 --- a/src/protocol/ConnectionRequestAccepted.php +++ b/src/protocol/ConnectionRequestAccepted.php @@ -16,6 +16,9 @@ namespace raklib\protocol; +use pmmp\encoding\BE; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use raklib\RakLib; use raklib\utils\InternetAddress; use function strlen; @@ -45,31 +48,31 @@ public function __construct(){ $this->systemAddresses[] = new InternetAddress("127.0.0.1", 0, 4); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putAddress($this->address); - $out->putShort(0); + protected function encodePayload(ByteBufferWriter $out) : void{ + PacketSerializer::putAddress($out, $this->address); + BE::writeUnsignedShort($out, 0); $dummy = new InternetAddress("0.0.0.0", 0, 4); for($i = 0; $i < RakLib::$SYSTEM_ADDRESS_COUNT; ++$i){ - $out->putAddress($this->systemAddresses[$i] ?? $dummy); + PacketSerializer::putAddress($out, $this->systemAddresses[$i] ?? $dummy); } - $out->putLong($this->sendPingTime); - $out->putLong($this->sendPongTime); + BE::writeUnsignedLong($out, $this->sendPingTime); + BE::writeUnsignedLong($out, $this->sendPongTime); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->address = $in->getAddress(); - $in->getShort(); //TODO: check this + protected function decodePayload(ByteBufferReader $in) : void{ + $this->address = PacketSerializer::getAddress($in); + BE::readUnsignedShort($in); //TODO: check this - $len = strlen($in->getBuffer()); + $len = strlen($in->getData()); $dummy = new InternetAddress("0.0.0.0", 0, 4); for($i = 0; $i < RakLib::$SYSTEM_ADDRESS_COUNT; ++$i){ - $this->systemAddresses[$i] = $in->getOffset() + 16 < $len ? $in->getAddress() : $dummy; //HACK: avoids trying to read too many addresses on bad data + $this->systemAddresses[$i] = $in->getOffset() + 16 < $len ? PacketSerializer::getAddress($in) : $dummy; //HACK: avoids trying to read too many addresses on bad data } - $this->sendPingTime = $in->getLong(); - $this->sendPongTime = $in->getLong(); + $this->sendPingTime = BE::readUnsignedLong($in); + $this->sendPongTime = BE::readUnsignedLong($in); } } diff --git a/src/protocol/Datagram.php b/src/protocol/Datagram.php index e93e2996..4d48ba83 100644 --- a/src/protocol/Datagram.php +++ b/src/protocol/Datagram.php @@ -16,6 +16,12 @@ namespace raklib\protocol; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\LE; +use function strlen; + class Datagram extends Packet{ public const BITFLAG_VALID = 0x80; public const BITFLAG_ACK = 0x40; @@ -36,14 +42,14 @@ class Datagram extends Packet{ public array $packets = []; public int $seqNumber; - protected function encodeHeader(PacketSerializer $out) : void{ - $out->putByte(self::BITFLAG_VALID | $this->headerFlags); + protected function encodeHeader(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, self::BITFLAG_VALID | $this->headerFlags); } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLTriad($this->seqNumber); + protected function encodePayload(ByteBufferWriter $out) : void{ + LE::writeUnsignedTriad($out, $this->seqNumber); foreach($this->packets as $packet){ - $out->put($packet->toBinary()); + $packet->toBinary($out); } } @@ -59,14 +65,15 @@ public function length(){ return $length; } - protected function decodeHeader(PacketSerializer $in) : void{ - $this->headerFlags = $in->getByte(); + protected function decodeHeader(ByteBufferReader $in) : void{ + $this->headerFlags = Byte::readUnsigned($in); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->seqNumber = $in->getLTriad(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->seqNumber = LE::readUnsignedTriad($in); - while(!$in->feof()){ + $len = strlen($in->getData()); + while($in->getOffset() < $len){ $this->packets[] = EncapsulatedPacket::fromBinary($in); } } diff --git a/src/protocol/DisconnectionNotification.php b/src/protocol/DisconnectionNotification.php index ae90c97d..5d9f07b1 100644 --- a/src/protocol/DisconnectionNotification.php +++ b/src/protocol/DisconnectionNotification.php @@ -16,14 +16,17 @@ namespace raklib\protocol; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; + class DisconnectionNotification extends ConnectedPacket{ public static $ID = MessageIdentifiers::ID_DISCONNECTION_NOTIFICATION; - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ } } diff --git a/src/protocol/EncapsulatedPacket.php b/src/protocol/EncapsulatedPacket.php index 0c41251e..e68b7402 100644 --- a/src/protocol/EncapsulatedPacket.php +++ b/src/protocol/EncapsulatedPacket.php @@ -16,11 +16,13 @@ namespace raklib\protocol; -use pocketmine\utils\Binary; -use pocketmine\utils\BinaryDataException; -use pocketmine\utils\BinaryStream; +use pmmp\encoding\BE; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\LE; use function ceil; -use function chr; use function strlen; class EncapsulatedPacket{ @@ -41,53 +43,64 @@ class EncapsulatedPacket{ public ?int $identifierACK = null; /** - * @throws BinaryDataException + * @throws DataDecodeException */ - public static function fromBinary(BinaryStream $stream) : EncapsulatedPacket{ + public static function fromBinary(ByteBufferReader $stream) : EncapsulatedPacket{ $packet = new EncapsulatedPacket(); - $flags = $stream->getByte(); + $flags = Byte::readUnsigned($stream); $packet->reliability = $reliability = ($flags & self::RELIABILITY_FLAGS) >> self::RELIABILITY_SHIFT; $hasSplit = ($flags & self::SPLIT_FLAG) !== 0; - $length = (int) ceil($stream->getShort() / 8); + $length = (int) ceil(BE::readUnsignedShort($stream) / 8); if($length === 0){ - throw new BinaryDataException("Encapsulated payload length cannot be zero"); + throw new DataDecodeException("Encapsulated payload length cannot be zero"); } if(PacketReliability::isReliable($reliability)){ - $packet->messageIndex = $stream->getLTriad(); + $packet->messageIndex = LE::readUnsignedTriad($stream); } if(PacketReliability::isSequenced($reliability)){ - $packet->sequenceIndex = $stream->getLTriad(); + $packet->sequenceIndex = LE::readUnsignedTriad($stream); } if(PacketReliability::isSequencedOrOrdered($reliability)){ - $packet->orderIndex = $stream->getLTriad(); - $packet->orderChannel = $stream->getByte(); + $packet->orderIndex = LE::readUnsignedTriad($stream); + $packet->orderChannel = Byte::readUnsigned($stream); } if($hasSplit){ - $splitCount = $stream->getInt(); - $splitID = $stream->getShort(); - $splitIndex = $stream->getInt(); + $splitCount = BE::readUnsignedInt($stream); + $splitID = BE::readUnsignedShort($stream); + $splitIndex = BE::readUnsignedInt($stream); $packet->splitInfo = new SplitPacketInfo($splitID, $splitIndex, $splitCount); } - $packet->buffer = $stream->get($length); + $packet->buffer = $stream->readByteArray($length); return $packet; } - public function toBinary() : string{ - return - chr(($this->reliability << self::RELIABILITY_SHIFT) | ($this->splitInfo !== null ? self::SPLIT_FLAG : 0)) . - Binary::writeShort(strlen($this->buffer) << 3) . - (PacketReliability::isReliable($this->reliability) ? Binary::writeLTriad($this->messageIndex) : "") . - (PacketReliability::isSequenced($this->reliability) ? Binary::writeLTriad($this->sequenceIndex) : "") . - (PacketReliability::isSequencedOrOrdered($this->reliability) ? Binary::writeLTriad($this->orderIndex) . chr($this->orderChannel) : "") . - ($this->splitInfo !== null ? Binary::writeInt($this->splitInfo->getTotalPartCount()) . Binary::writeShort($this->splitInfo->getId()) . Binary::writeInt($this->splitInfo->getPartIndex()) : "") - . $this->buffer; + public function toBinary(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, ($this->reliability << self::RELIABILITY_SHIFT) | ($this->splitInfo !== null ? self::SPLIT_FLAG : 0)); + + BE::writeUnsignedShort($out, strlen($this->buffer) << 3); + if(PacketReliability::isReliable($this->reliability)){ + LE::writeUnsignedTriad($out, $this->messageIndex); + } + if(PacketReliability::isSequenced($this->reliability)){ + LE::writeUnsignedTriad($out, $this->sequenceIndex); + } + if(PacketReliability::isSequencedOrOrdered($this->reliability)){ + LE::writeUnsignedTriad($out, $this->orderIndex); + Byte::writeUnsigned($out, $this->orderChannel); + } + if($this->splitInfo !== null){ + BE::writeUnsignedInt($out, $this->splitInfo->getTotalPartCount()); + BE::writeUnsignedShort($out, $this->splitInfo->getId()); + BE::writeUnsignedInt($out, $this->splitInfo->getPartIndex()); + } + $out->writeByteArray($this->buffer); } /** @@ -106,8 +119,4 @@ public function getHeaderLength() : int{ public function getTotalLength() : int{ return $this->getHeaderLength() + strlen($this->buffer); } - - public function __toString() : string{ - return $this->toBinary(); - } } diff --git a/src/protocol/IncompatibleProtocolVersion.php b/src/protocol/IncompatibleProtocolVersion.php index c3575cd2..d30dfe14 100644 --- a/src/protocol/IncompatibleProtocolVersion.php +++ b/src/protocol/IncompatibleProtocolVersion.php @@ -16,6 +16,11 @@ namespace raklib\protocol; +use pmmp\encoding\BE; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; + class IncompatibleProtocolVersion extends OfflineMessage{ public static $ID = MessageIdentifiers::ID_INCOMPATIBLE_PROTOCOL_VERSION; @@ -29,15 +34,15 @@ public static function create(int $protocolVersion, int $serverId) : self{ return $result; } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putByte($this->protocolVersion); + protected function encodePayload(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, $this->protocolVersion); $this->writeMagic($out); - $out->putLong($this->serverId); + BE::writeUnsignedLong($out, $this->serverId); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->protocolVersion = $in->getByte(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->protocolVersion = Byte::readUnsigned($in); $this->readMagic($in); - $this->serverId = $in->getLong(); + $this->serverId = BE::readUnsignedLong($in); } } diff --git a/src/protocol/NewIncomingConnection.php b/src/protocol/NewIncomingConnection.php index 5ae4e55b..1b103926 100644 --- a/src/protocol/NewIncomingConnection.php +++ b/src/protocol/NewIncomingConnection.php @@ -16,6 +16,9 @@ namespace raklib\protocol; +use pmmp\encoding\BE; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use raklib\RakLib; use raklib\utils\InternetAddress; use function strlen; @@ -29,30 +32,30 @@ class NewIncomingConnection extends ConnectedPacket{ public int $sendPingTime; public int $sendPongTime; - protected function encodePayload(PacketSerializer $out) : void{ - $out->putAddress($this->address); + protected function encodePayload(ByteBufferWriter $out) : void{ + PacketSerializer::putAddress($out, $this->address); foreach($this->systemAddresses as $address){ - $out->putAddress($address); + PacketSerializer::putAddress($out, $address); } - $out->putLong($this->sendPingTime); - $out->putLong($this->sendPongTime); + BE::writeUnsignedLong($out, $this->sendPingTime); + BE::writeUnsignedLong($out, $this->sendPongTime); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->address = $in->getAddress(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->address = PacketSerializer::getAddress($in); //TODO: HACK! - $stopOffset = strlen($in->getBuffer()) - 16; //buffer length - sizeof(sendPingTime) - sizeof(sendPongTime) + $stopOffset = strlen($in->getData()) - 16; //buffer length - sizeof(sendPingTime) - sizeof(sendPongTime) $dummy = new InternetAddress("0.0.0.0", 0, 4); for($i = 0; $i < RakLib::$SYSTEM_ADDRESS_COUNT; ++$i){ if($in->getOffset() >= $stopOffset){ $this->systemAddresses[$i] = clone $dummy; }else{ - $this->systemAddresses[$i] = $in->getAddress(); + $this->systemAddresses[$i] = PacketSerializer::getAddress($in); } } - $this->sendPingTime = $in->getLong(); - $this->sendPongTime = $in->getLong(); + $this->sendPingTime = BE::readUnsignedLong($in); + $this->sendPongTime = BE::readUnsignedLong($in); } } diff --git a/src/protocol/OfflineMessage.php b/src/protocol/OfflineMessage.php index d157d0fa..9cdfe6b8 100644 --- a/src/protocol/OfflineMessage.php +++ b/src/protocol/OfflineMessage.php @@ -16,8 +16,9 @@ namespace raklib\protocol; -use pocketmine\utils\BinaryDataException; -use pocketmine\utils\BinaryStream; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; abstract class OfflineMessage extends Packet{ @@ -30,17 +31,17 @@ abstract class OfflineMessage extends Packet{ /** * @return void - * @throws BinaryDataException + * @throws DataDecodeException */ - protected function readMagic(BinaryStream $in){ - $this->magic = $in->get(16); + protected function readMagic(ByteBufferReader $in){ + $this->magic = $in->readByteArray(16); } /** * @return void */ - protected function writeMagic(BinaryStream $out){ - $out->put($this->magic); + protected function writeMagic(ByteBufferWriter $out){ + $out->writeByteArray($this->magic); } public function isValid() : bool{ diff --git a/src/protocol/OpenConnectionReply1.php b/src/protocol/OpenConnectionReply1.php index eaa227c4..3a741528 100644 --- a/src/protocol/OpenConnectionReply1.php +++ b/src/protocol/OpenConnectionReply1.php @@ -16,6 +16,11 @@ namespace raklib\protocol; +use pmmp\encoding\BE; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; + class OpenConnectionReply1 extends OfflineMessage{ public static $ID = MessageIdentifiers::ID_OPEN_CONNECTION_REPLY_1; @@ -31,17 +36,17 @@ public static function create(int $serverId, bool $serverSecurity, int $mtuSize) return $result; } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ $this->writeMagic($out); - $out->putLong($this->serverID); - $out->putByte($this->serverSecurity ? 1 : 0); - $out->putShort($this->mtuSize); + BE::writeUnsignedLong($out, $this->serverID); + Byte::writeUnsigned($out, $this->serverSecurity ? 1 : 0); + BE::writeUnsignedShort($out, $this->mtuSize); } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->readMagic($in); - $this->serverID = $in->getLong(); - $this->serverSecurity = $in->getByte() !== 0; - $this->mtuSize = $in->getShort(); + $this->serverID = BE::readUnsignedLong($in); + $this->serverSecurity = Byte::readUnsigned($in) !== 0; + $this->mtuSize = BE::readUnsignedShort($in); } } diff --git a/src/protocol/OpenConnectionReply2.php b/src/protocol/OpenConnectionReply2.php index f0eff657..a999bcfa 100644 --- a/src/protocol/OpenConnectionReply2.php +++ b/src/protocol/OpenConnectionReply2.php @@ -16,6 +16,10 @@ namespace raklib\protocol; +use pmmp\encoding\BE; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use raklib\utils\InternetAddress; class OpenConnectionReply2 extends OfflineMessage{ @@ -35,19 +39,19 @@ public static function create(int $serverId, InternetAddress $clientAddress, int return $result; } - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ $this->writeMagic($out); - $out->putLong($this->serverID); - $out->putAddress($this->clientAddress); - $out->putShort($this->mtuSize); - $out->putByte($this->serverSecurity ? 1 : 0); + BE::writeUnsignedLong($out, $this->serverID); + PacketSerializer::putAddress($out, $this->clientAddress); + BE::writeUnsignedShort($out, $this->mtuSize); + Byte::writeUnsigned($out, $this->serverSecurity ? 1 : 0); } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->readMagic($in); - $this->serverID = $in->getLong(); - $this->clientAddress = $in->getAddress(); - $this->mtuSize = $in->getShort(); - $this->serverSecurity = $in->getByte() !== 0; + $this->serverID = BE::readUnsignedLong($in); + $this->clientAddress = PacketSerializer::getAddress($in); + $this->mtuSize = BE::readUnsignedShort($in); + $this->serverSecurity = Byte::readUnsigned($in) !== 0; } } diff --git a/src/protocol/OpenConnectionRequest1.php b/src/protocol/OpenConnectionRequest1.php index fc9d8e60..db0be4df 100644 --- a/src/protocol/OpenConnectionRequest1.php +++ b/src/protocol/OpenConnectionRequest1.php @@ -16,6 +16,9 @@ namespace raklib\protocol; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use raklib\RakLib; use function str_repeat; use function strlen; @@ -26,16 +29,16 @@ class OpenConnectionRequest1 extends OfflineMessage{ public int $protocol = RakLib::DEFAULT_PROTOCOL_VERSION; public int $mtuSize; - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ $this->writeMagic($out); - $out->putByte($this->protocol); - $out->put(str_repeat("\x00", $this->mtuSize - strlen($out->getBuffer()))); + Byte::writeUnsigned($out, $this->protocol); + $out->writeByteArray(str_repeat("\x00", $this->mtuSize - strlen($out->getData()))); } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->readMagic($in); - $this->protocol = $in->getByte(); - $this->mtuSize = strlen($in->getBuffer()); - $in->getRemaining(); //silence unread warnings + $this->protocol = Byte::readUnsigned($in); + $this->mtuSize = strlen($in->getData()); + $in->setOffset(strlen($in->getData())); //silence unread warnings } } diff --git a/src/protocol/OpenConnectionRequest2.php b/src/protocol/OpenConnectionRequest2.php index bc221835..08fc2df6 100644 --- a/src/protocol/OpenConnectionRequest2.php +++ b/src/protocol/OpenConnectionRequest2.php @@ -16,6 +16,9 @@ namespace raklib\protocol; +use pmmp\encoding\BE; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; use raklib\utils\InternetAddress; class OpenConnectionRequest2 extends OfflineMessage{ @@ -25,17 +28,17 @@ class OpenConnectionRequest2 extends OfflineMessage{ public InternetAddress $serverAddress; public int $mtuSize; - protected function encodePayload(PacketSerializer $out) : void{ + protected function encodePayload(ByteBufferWriter $out) : void{ $this->writeMagic($out); - $out->putAddress($this->serverAddress); - $out->putShort($this->mtuSize); - $out->putLong($this->clientID); + PacketSerializer::putAddress($out, $this->serverAddress); + BE::writeUnsignedShort($out, $this->mtuSize); + BE::writeUnsignedLong($out, $this->clientID); } - protected function decodePayload(PacketSerializer $in) : void{ + protected function decodePayload(ByteBufferReader $in) : void{ $this->readMagic($in); - $this->serverAddress = $in->getAddress(); - $this->mtuSize = $in->getShort(); - $this->clientID = $in->getLong(); + $this->serverAddress = PacketSerializer::getAddress($in); + $this->mtuSize = BE::readUnsignedShort($in); + $this->clientID = BE::readUnsignedLong($in); } } diff --git a/src/protocol/Packet.php b/src/protocol/Packet.php index 27e24c19..37bbcc95 100644 --- a/src/protocol/Packet.php +++ b/src/protocol/Packet.php @@ -16,40 +16,43 @@ namespace raklib\protocol; -use pocketmine\utils\BinaryDataException; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; abstract class Packet{ /** @var int */ public static $ID = -1; - public function encode(PacketSerializer $out) : void{ + public function encode(ByteBufferWriter $out) : void{ $this->encodeHeader($out); $this->encodePayload($out); } - protected function encodeHeader(PacketSerializer $out) : void{ - $out->putByte(static::$ID); + protected function encodeHeader(ByteBufferWriter $out) : void{ + Byte::writeUnsigned($out, static::$ID); } - abstract protected function encodePayload(PacketSerializer $out) : void; + abstract protected function encodePayload(ByteBufferWriter $out) : void; /** - * @throws BinaryDataException + * @throws DataDecodeException */ - public function decode(PacketSerializer $in) : void{ + public function decode(ByteBufferReader $in) : void{ $this->decodeHeader($in); $this->decodePayload($in); } /** - * @throws BinaryDataException + * @throws DataDecodeException */ - protected function decodeHeader(PacketSerializer $in) : void{ - $in->getByte(); //PID + protected function decodeHeader(ByteBufferReader $in) : void{ + Byte::readUnsigned($in); //PID } /** - * @throws BinaryDataException + * @throws DataDecodeException */ - abstract protected function decodePayload(PacketSerializer $in) : void; + abstract protected function decodePayload(ByteBufferReader $in) : void; } diff --git a/src/protocol/PacketSerializer.php b/src/protocol/PacketSerializer.php index 9c1c7f9e..08a8313a 100644 --- a/src/protocol/PacketSerializer.php +++ b/src/protocol/PacketSerializer.php @@ -16,8 +16,12 @@ namespace raklib\protocol; -use pocketmine\utils\BinaryDataException; -use pocketmine\utils\BinaryStream; +use pmmp\encoding\BE; +use pmmp\encoding\Byte; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; +use pmmp\encoding\LE; use raklib\utils\InternetAddress; use function assert; use function count; @@ -27,65 +31,68 @@ use function strlen; use const AF_INET6; -final class PacketSerializer extends BinaryStream{ +final class PacketSerializer{ + private function __construct(){ + //NOOP + } /** - * @throws BinaryDataException + * @throws DataDecodeException */ - public function getString() : string{ - return $this->get($this->getShort()); + public static function getString(ByteBufferReader $in) : string{ + return $in->readByteArray(BE::readUnsignedShort($in)); } /** - * @throws BinaryDataException + * @throws DataDecodeException */ - public function getAddress() : InternetAddress{ - $version = $this->getByte(); + public static function getAddress(ByteBufferReader $in) : InternetAddress{ + $version = Byte::readUnsigned($in); if($version === 4){ - $addr = ((~$this->getByte()) & 0xff) . "." . ((~$this->getByte()) & 0xff) . "." . ((~$this->getByte()) & 0xff) . "." . ((~$this->getByte()) & 0xff); - $port = $this->getShort(); + $addr = ((~Byte::readUnsigned($in)) & 0xff) . "." . ((Byte::readUnsigned($in)) & 0xff) . "." . ((~Byte::readUnsigned($in)) & 0xff) . "." . ((~Byte::readUnsigned($in)) & 0xff); + $port = BE::readUnsignedShort($in); return new InternetAddress($addr, $port, $version); }elseif($version === 6){ //http://man7.org/linux/man-pages/man7/ipv6.7.html - $this->getLShort(); //Family, AF_INET6 - $port = $this->getShort(); - $this->getInt(); //flow info - $addr = inet_ntop($this->get(16)); + LE::readUnsignedShort($in); //Family, AF_INET6 + $port = BE::readUnsignedShort($in); + BE::readUnsignedInt($in); //flow info + $addr = inet_ntop($in->readByteArray(16)); if($addr === false){ - throw new BinaryDataException("Failed to parse IPv6 address"); + throw new DataDecodeException("Failed to parse IPv6 address"); } - $this->getInt(); //scope ID + BE::readUnsignedInt($in); //scope ID return new InternetAddress($addr, $port, $version); }else{ - throw new BinaryDataException("Unknown IP address version $version"); + throw new DataDecodeException("Unknown IP address version $version"); } } - public function putString(string $v) : void{ - $this->putShort(strlen($v)); - $this->put($v); + public static function putString(ByteBufferWriter $out, string $v) : void{ + BE::writeUnsignedShort($out, strlen($v)); + $out->writeByteArray($v); } - public function putAddress(InternetAddress $address) : void{ + public static function putAddress(ByteBufferWriter $out, InternetAddress $address) : void{ $version = $address->getVersion(); - $this->putByte($version); + Byte::writeUnsigned($out, $version); if($version === 4){ $parts = explode(".", $address->getIp()); assert(count($parts) === 4, "Wrong number of parts in IPv4 IP, expected 4, got " . count($parts)); foreach($parts as $b){ - $this->putByte((~((int) $b)) & 0xff); + Byte::writeUnsigned($out, (~((int) $b)) & 0xff); } - $this->putShort($address->getPort()); + BE::writeUnsignedShort($out, $address->getPort()); }elseif($version === 6){ - $this->putLShort(AF_INET6); - $this->putShort($address->getPort()); - $this->putInt(0); + LE::writeUnsignedShort($out, AF_INET6); + BE::writeUnsignedShort($out, $address->getPort()); + BE::writeUnsignedInt($out, 0); $rawIp = inet_pton($address->getIp()); if($rawIp === false){ throw new \InvalidArgumentException("Invalid IPv6 address could not be encoded"); } - $this->put($rawIp); - $this->putInt(0); + $out->writeByteArray($rawIp); + BE::writeUnsignedInt($out, 0); }else{ throw new \InvalidArgumentException("IP version $version is not supported"); } diff --git a/src/protocol/UnconnectedPing.php b/src/protocol/UnconnectedPing.php index cc474238..8c52cc5d 100644 --- a/src/protocol/UnconnectedPing.php +++ b/src/protocol/UnconnectedPing.php @@ -16,21 +16,25 @@ namespace raklib\protocol; +use pmmp\encoding\BE; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; + class UnconnectedPing extends OfflineMessage{ public static $ID = MessageIdentifiers::ID_UNCONNECTED_PING; public int $sendPingTime; public int $clientId; - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLong($this->sendPingTime); + protected function encodePayload(ByteBufferWriter $out) : void{ + BE::writeUnsignedLong($out, $this->sendPingTime); $this->writeMagic($out); - $out->putLong($this->clientId); + BE::writeUnsignedLong($out, $this->clientId); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->sendPingTime = $in->getLong(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->sendPingTime = BE::readUnsignedLong($in); $this->readMagic($in); - $this->clientId = $in->getLong(); + $this->clientId = BE::readUnsignedLong($in); } } diff --git a/src/protocol/UnconnectedPong.php b/src/protocol/UnconnectedPong.php index 827652db..06249684 100644 --- a/src/protocol/UnconnectedPong.php +++ b/src/protocol/UnconnectedPong.php @@ -16,6 +16,10 @@ namespace raklib\protocol; +use pmmp\encoding\BE; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; + class UnconnectedPong extends OfflineMessage{ public static $ID = MessageIdentifiers::ID_UNCONNECTED_PONG; @@ -31,17 +35,17 @@ public static function create(int $sendPingTime, int $serverId, string $serverNa return $result; } - protected function encodePayload(PacketSerializer $out) : void{ - $out->putLong($this->sendPingTime); - $out->putLong($this->serverId); + protected function encodePayload(ByteBufferWriter $out) : void{ + BE::writeUnsignedLong($out, $this->sendPingTime); + BE::writeUnsignedLong($out, $this->serverId); $this->writeMagic($out); - $out->putString($this->serverName); + PacketSerializer::putString($out, $this->serverName); } - protected function decodePayload(PacketSerializer $in) : void{ - $this->sendPingTime = $in->getLong(); - $this->serverId = $in->getLong(); + protected function decodePayload(ByteBufferReader $in) : void{ + $this->sendPingTime = BE::readUnsignedLong($in); + $this->serverId = BE::readUnsignedLong($in); $this->readMagic($in); - $this->serverName = $in->getString(); + $this->serverName = PacketSerializer::getString($in); } } diff --git a/src/server/Server.php b/src/server/Server.php index f9c3fb5c..5b09fcec 100644 --- a/src/server/Server.php +++ b/src/server/Server.php @@ -16,7 +16,9 @@ namespace raklib\server; -use pocketmine\utils\BinaryDataException; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\ByteBufferWriter; +use pmmp\encoding\DataDecodeException; use raklib\generic\DisconnectReason; use raklib\generic\PacketHandlingException; use raklib\generic\Session; @@ -26,7 +28,6 @@ use raklib\protocol\EncapsulatedPacket; use raklib\protocol\NACK; use raklib\protocol\Packet; -use raklib\protocol\PacketSerializer; use raklib\utils\ExceptionTraceCleaner; use raklib\utils\InternetAddress; use function asort; @@ -272,7 +273,7 @@ private function receivePacket() : bool{ }else{ $packet = new Datagram(); } - $packet->decode(new PacketSerializer($buffer)); + $packet->decode(new ByteBufferReader($buffer)); try{ $session->handlePacket($packet); }catch(PacketHandlingException $e){ @@ -303,7 +304,7 @@ private function receivePacket() : bool{ $this->logger->debug("Ignored packet from $address due to no session opened (0x" . bin2hex($buffer[0]) . ")"); } } - }catch(BinaryDataException $e){ + }catch(DataDecodeException $e){ if($this->packetErrorsSinceLastUpdate < $this->packetErrorSuppressionThreshold){ $logFn = function() use ($address, $e, $buffer) : void{ $this->logger->debug("Packet from $address (" . strlen($buffer) . " bytes): 0x" . bin2hex($buffer)); @@ -329,10 +330,10 @@ private function receivePacket() : bool{ } public function sendPacket(Packet $packet, InternetAddress $address) : void{ - $out = new PacketSerializer(); //TODO: reusable streams to reduce allocations + $out = new ByteBufferWriter(); //TODO: reusable streams to reduce allocations $packet->encode($out); try{ - $this->sendBytes += $this->socket->writePacket($out->getBuffer(), $address->getIp(), $address->getPort()); + $this->sendBytes += $this->socket->writePacket($out->getData(), $address->getIp(), $address->getPort()); }catch(SocketException $e){ $this->logger->debug($e->getMessage()); } diff --git a/src/server/ServerSession.php b/src/server/ServerSession.php index 13e29184..bc61beca 100644 --- a/src/server/ServerSession.php +++ b/src/server/ServerSession.php @@ -16,6 +16,7 @@ namespace raklib\server; +use pmmp\encoding\ByteBufferReader; use raklib\generic\Session; use raklib\protocol\ConnectionRequest; use raklib\protocol\ConnectionRequestAccepted; @@ -23,7 +24,6 @@ use raklib\protocol\NewIncomingConnection; use raklib\protocol\Packet; use raklib\protocol\PacketReliability; -use raklib\protocol\PacketSerializer; use raklib\utils\InternetAddress; use function ord; @@ -72,7 +72,7 @@ final protected function handleRakNetConnectionPacket(string $packet) : void{ $id = ord($packet[0]); if($id === MessageIdentifiers::ID_CONNECTION_REQUEST){ $dataPacket = new ConnectionRequest(); - $dataPacket->decode(new PacketSerializer($packet)); + $dataPacket->decode(new ByteBufferReader($packet)); $this->queueConnectedPacket(ConnectionRequestAccepted::create( $this->address, [], @@ -81,7 +81,7 @@ final protected function handleRakNetConnectionPacket(string $packet) : void{ ), PacketReliability::UNRELIABLE, 0, true); }elseif($id === MessageIdentifiers::ID_NEW_INCOMING_CONNECTION){ $dataPacket = new NewIncomingConnection(); - $dataPacket->decode(new PacketSerializer($packet)); + $dataPacket->decode(new ByteBufferReader($packet)); if($dataPacket->address->getPort() === $this->server->getPort() or !$this->server->portChecking){ $this->state = self::STATE_CONNECTED; //FINALLY! diff --git a/src/server/UnconnectedMessageHandler.php b/src/server/UnconnectedMessageHandler.php index 2ab7badb..b2172c68 100644 --- a/src/server/UnconnectedMessageHandler.php +++ b/src/server/UnconnectedMessageHandler.php @@ -16,7 +16,8 @@ namespace raklib\server; -use pocketmine\utils\BinaryDataException; +use pmmp\encoding\ByteBufferReader; +use pmmp\encoding\DataDecodeException; use raklib\generic\Session; use raklib\protocol\IncompatibleProtocolVersion; use raklib\protocol\MessageIdentifiers; @@ -25,7 +26,6 @@ use raklib\protocol\OpenConnectionReply2; use raklib\protocol\OpenConnectionRequest1; use raklib\protocol\OpenConnectionRequest2; -use raklib\protocol\PacketSerializer; use raklib\protocol\UnconnectedPing; use raklib\protocol\UnconnectedPingOpenConnections; use raklib\protocol\UnconnectedPong; @@ -51,7 +51,7 @@ public function __construct( } /** - * @throws BinaryDataException + * @throws DataDecodeException */ public function handleRaw(string $payload, InternetAddress $address) : bool{ if($payload === ""){ @@ -61,13 +61,13 @@ public function handleRaw(string $payload, InternetAddress $address) : bool{ if($pk === null){ return false; } - $reader = new PacketSerializer($payload); + $reader = new ByteBufferReader($payload); $pk->decode($reader); if(!$pk->isValid()){ return false; } - if(!$reader->feof()){ - $remains = substr($reader->getBuffer(), $reader->getOffset()); + if($reader->getOffset() < strlen($reader->getData())){ + $remains = substr($reader->getData(), $reader->getOffset()); $this->server->getLogger()->debug("Still " . strlen($remains) . " bytes unread in " . get_class($pk) . " from $address"); } return $this->handle($pk, $address); diff --git a/tests/phpstan/configs/encapsulated-packet-sucks.neon b/tests/phpstan/configs/encapsulated-packet-sucks.neon index aa9da44f..9ae6e800 100644 --- a/tests/phpstan/configs/encapsulated-packet-sucks.neon +++ b/tests/phpstan/configs/encapsulated-packet-sucks.neon @@ -1,17 +1,20 @@ parameters: ignoreErrors: - - message: "#^Only numeric types are allowed in \\+, int\\|null given on the left side\\.$#" + message: '#^Only numeric types are allowed in \+, int\|null given on the left side\.$#' + identifier: plus.leftNonNumeric count: 1 path: ../../../src/generic/ReceiveReliabilityLayer.php - - message: "#^Parameter \\#1 \\$codepoint of function chr expects int, int\\|null given\\.$#" + message: '#^Parameter \#2 \$value of static method pmmp\\encoding\\Byte\:\:writeUnsigned\(\) expects int, int\|null given\.$#' + identifier: argument.type count: 1 path: ../../../src/protocol/EncapsulatedPacket.php - - message: "#^Parameter \\#1 \\$value of static method pocketmine\\\\utils\\\\Binary\\:\\:writeLTriad\\(\\) expects int, int\\|null given\\.$#" + message: '#^Parameter \#2 \$value of static method pmmp\\encoding\\LE\:\:writeUnsignedTriad\(\) expects int, int\|null given\.$#' + identifier: argument.type count: 3 path: ../../../src/protocol/EncapsulatedPacket.php diff --git a/tools/proxy.php b/tools/proxy.php index afed79e7..718b6c26 100644 --- a/tools/proxy.php +++ b/tools/proxy.php @@ -21,17 +21,11 @@ declare(strict_types=1); -use pocketmine\utils\Limits; use raklib\client\ClientSocket; use raklib\generic\SocketException; use raklib\protocol\MessageIdentifiers; -use raklib\protocol\UnconnectedPong; -use raklib\server\ProtocolAcceptor; -use raklib\server\Server; use raklib\server\ServerSocket; -use raklib\server\SimpleProtocolAcceptor; use raklib\utils\InternetAddress; -use raklib\generic\Socket; require dirname(__DIR__) . '/vendor/autoload.php'; @@ -127,7 +121,7 @@ function serverToClientRelay(ClientSession $client, ServerSocket $clientProxySoc /** @var ClientSession[][] $clients */ $clients = []; -$serverId = mt_rand(0, Limits::INT32_MAX); +$serverId = mt_rand(0, 0x7f_ff_ff_ff); $mostRecentPong = null; while(true){ diff --git a/tools/scan.php b/tools/scan.php index 1f3a6f0f..11a87ce3 100644 --- a/tools/scan.php +++ b/tools/scan.php @@ -2,10 +2,9 @@ declare(strict_types=1); -use pocketmine\utils\Limits; +use pmmp\encoding\ByteBufferWriter; use raklib\generic\SocketException; use raklib\protocol\MessageIdentifiers; -use raklib\protocol\PacketSerializer; use raklib\protocol\UnconnectedPing; use raklib\server\ServerSocket; use raklib\utils\InternetAddress; @@ -28,7 +27,7 @@ $socket = new ServerSocket($bindAddress); $socket->enableBroadcast(); -$clientId = mt_rand(0, Limits::INT32_MAX); +$clientId = mt_rand(0, 0x7f_ff_ff_ff); \GlobalLogger::get()->info("Listening on " . $bindAddress); \GlobalLogger::get()->info("Press CTRL+C to stop"); @@ -37,9 +36,9 @@ function sendPing(ServerSocket $socket, string $broadcastAddress, int $port, int $ping->clientId = $clientId; $ping->sendPingTime = intdiv(hrtime(true), 1_000_000); - $serializer = new PacketSerializer(); + $serializer = new ByteBufferWriter(); $ping->encode($serializer); - $socket->writePacket($serializer->getBuffer(), $broadcastAddress, $port); + $socket->writePacket($serializer->getData(), $broadcastAddress, $port); } sendPing($socket, $broadcastAddress, $port, $clientId); @@ -55,4 +54,4 @@ function sendPing(ServerSocket $socket, string $broadcastAddress, int $port, int sendPing($socket, $broadcastAddress, $port, $clientId); } } -} \ No newline at end of file +}