Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
uses: shivammathur/setup-php@2.32.0
with:
php-version: ${{ matrix.php }}
extensions: encoding-pmmp/ext-encoding@1.0.0

- name: Cache Composer packages
id: composer-cache
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"require": {
"php": "^8.0",
"php-64bit": "*",
"pocketmine/binaryutils": "^0.2.0",
"ext-encoding": "~1.0.0",
"pocketmine/raklib": "^0.15.0 || ^1.0.0"
},
"require-dev": {
Expand Down
26 changes: 13 additions & 13 deletions src/RakLibToUserThreadMessageReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace raklib\server\ipc;

use pocketmine\utils\Binary;
use pmmp\encoding\LE;
use raklib\generic\DisconnectReason;
use raklib\server\ipc\RakLibToUserThreadMessageProtocol as ITCProtocol;
use raklib\server\ServerEventListener;
Expand All @@ -35,25 +35,25 @@ public function handle(ServerEventListener $listener) : bool{
$id = ord($packet[0]);
$offset = 1;
if($id === ITCProtocol::PACKET_ENCAPSULATED){
$sessionId = Binary::readInt(substr($packet, $offset, 4));
$sessionId = LE::unpackUnsignedInt(substr($packet, $offset, 4));
$offset += 4;
$buffer = substr($packet, $offset);
$listener->onPacketReceive($sessionId, $buffer);
}elseif($id === ITCProtocol::PACKET_RAW){
$len = ord($packet[$offset++]);
$address = substr($packet, $offset, $len);
$offset += $len;
$port = Binary::readShort(substr($packet, $offset, 2));
$port = LE::unpackUnsignedShort(substr($packet, $offset, 2));
$offset += 2;
$payload = substr($packet, $offset);
$listener->onRawPacketReceive($address, $port, $payload);
}elseif($id === ITCProtocol::PACKET_REPORT_BANDWIDTH_STATS){
$sentBytes = Binary::readLong(substr($packet, $offset, 8));
$sentBytes = LE::unpackUnsignedLong(substr($packet, $offset, 8));
$offset += 8;
$receivedBytes = Binary::readLong(substr($packet, $offset, 8));
$receivedBytes = LE::unpackUnsignedLong(substr($packet, $offset, 8));
$listener->onBandwidthStatsUpdate($sentBytes, $receivedBytes);
}elseif($id === ITCProtocol::PACKET_OPEN_SESSION){
$sessionId = Binary::readInt(substr($packet, $offset, 4));
$sessionId = LE::unpackUnsignedInt(substr($packet, $offset, 4));
$offset += 4;
$len = ord($packet[$offset++]);
$rawAddr = substr($packet, $offset, $len);
Expand All @@ -62,26 +62,26 @@ public function handle(ServerEventListener $listener) : bool{
if($address === false){
throw new \RuntimeException("Unexpected invalid IP address in inter-thread message");
}
$port = Binary::readShort(substr($packet, $offset, 2));
$port = LE::unpackUnsignedShort(substr($packet, $offset, 2));
$offset += 2;
$clientID = Binary::readLong(substr($packet, $offset, 8));
$clientID = LE::unpackUnsignedLong(substr($packet, $offset, 8));
$listener->onClientConnect($sessionId, $address, $port, $clientID);
}elseif($id === ITCProtocol::PACKET_CLOSE_SESSION){
$sessionId = Binary::readInt(substr($packet, $offset, 4));
$sessionId = LE::unpackUnsignedInt(substr($packet, $offset, 4));
$offset += 4;
//don't love this, but we know the sender will never send an invalid value, so this is fine
/** @phpstan-var DisconnectReason::* $reason */
$reason = ord($packet[$offset]);
$listener->onClientDisconnect($sessionId, $reason);
}elseif($id === ITCProtocol::PACKET_ACK_NOTIFICATION){
$sessionId = Binary::readInt(substr($packet, $offset, 4));
$sessionId = LE::unpackUnsignedInt(substr($packet, $offset, 4));
$offset += 4;
$identifierACK = Binary::readInt(substr($packet, $offset, 4));
$identifierACK = LE::unpackSignedInt(substr($packet, $offset, 4));
$listener->onPacketAck($sessionId, $identifierACK);
}elseif($id === ITCProtocol::PACKET_REPORT_PING){
$sessionId = Binary::readInt(substr($packet, $offset, 4));
$sessionId = LE::unpackUnsignedInt(substr($packet, $offset, 4));
$offset += 4;
$pingMS = Binary::readInt(substr($packet, $offset, 4));
$pingMS = LE::unpackUnsignedInt(substr($packet, $offset, 4));
$listener->onPingMeasure($sessionId, $pingMS);
}

Expand Down
26 changes: 13 additions & 13 deletions src/RakLibToUserThreadMessageSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace raklib\server\ipc;

use pocketmine\utils\Binary;
use pmmp\encoding\LE;
use raklib\server\ipc\RakLibToUserThreadMessageProtocol as ITCProtocol;
use raklib\server\ServerEventListener;
use function chr;
Expand All @@ -36,25 +36,25 @@ public function onClientConnect(int $sessionId, string $address, int $port, int
}
$this->channel->write(
chr(ITCProtocol::PACKET_OPEN_SESSION) .
Binary::writeInt($sessionId) .
LE::packUnsignedInt($sessionId) .
chr(strlen($rawAddr)) . $rawAddr .
Binary::writeShort($port) .
Binary::writeLong($clientId)
LE::packUnsignedShort($port) .
LE::packUnsignedLong($clientId)
);
}

public function onClientDisconnect(int $sessionId, int $reason) : void{
$this->channel->write(
chr(ITCProtocol::PACKET_CLOSE_SESSION) .
Binary::writeInt($sessionId) .
LE::packUnsignedInt($sessionId) .
chr($reason)
);
}

public function onPacketReceive(int $sessionId, string $packet) : void{
$this->channel->write(
chr(ITCProtocol::PACKET_ENCAPSULATED) .
Binary::writeInt($sessionId) .
LE::packUnsignedInt($sessionId) .
$packet
);
}
Expand All @@ -63,32 +63,32 @@ public function onRawPacketReceive(string $address, int $port, string $payload)
$this->channel->write(
chr(ITCProtocol::PACKET_RAW) .
chr(strlen($address)) . $address .
Binary::writeShort($port) .
LE::packUnsignedShort($port) .
$payload
);
}

public function onPacketAck(int $sessionId, int $identifierACK) : void{
$this->channel->write(
chr(ITCProtocol::PACKET_ACK_NOTIFICATION) .
Binary::writeInt($sessionId) .
Binary::writeInt($identifierACK)
LE::packUnsignedInt($sessionId) .
LE::packSignedInt($identifierACK)
);
}

public function onBandwidthStatsUpdate(int $bytesSentDiff, int $bytesReceivedDiff) : void{
$this->channel->write(
chr(ITCProtocol::PACKET_REPORT_BANDWIDTH_STATS) .
Binary::writeLong($bytesSentDiff) .
Binary::writeLong($bytesReceivedDiff)
LE::packUnsignedLong($bytesSentDiff) .
LE::packSignedLong($bytesReceivedDiff)
);
}

public function onPingMeasure(int $sessionId, int $pingMS) : void{
$this->channel->write(
chr(ITCProtocol::PACKET_REPORT_PING) .
Binary::writeInt($sessionId) .
Binary::writeInt($pingMS)
LE::packUnsignedInt($sessionId) .
LE::packUnsignedInt($pingMS)
);
}
}
14 changes: 7 additions & 7 deletions src/UserToRakLibThreadMessageReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace raklib\server\ipc;

use pocketmine\utils\Binary;
use pmmp\encoding\LE;
use raklib\protocol\EncapsulatedPacket;
use raklib\protocol\PacketReliability;
use raklib\server\ipc\UserToRakLibThreadMessageProtocol as ITCProtocol;
Expand All @@ -36,7 +36,7 @@ public function process(ServerInterface $server) : bool{
$id = ord($packet[0]);
$offset = 1;
if($id === ITCProtocol::PACKET_ENCAPSULATED){
$sessionId = Binary::readInt(substr($packet, $offset, 4));
$sessionId = LE::unpackUnsignedInt(substr($packet, $offset, 4));
$offset += 4;
$flags = ord($packet[$offset++]);
$immediate = ($flags & ITCProtocol::ENCAPSULATED_FLAG_IMMEDIATE) !== 0;
Expand All @@ -46,7 +46,7 @@ public function process(ServerInterface $server) : bool{
$encapsulated->reliability = ord($packet[$offset++]);

if($needACK){
$encapsulated->identifierACK = Binary::readInt(substr($packet, $offset, 4));
$encapsulated->identifierACK = LE::unpackSignedInt(substr($packet, $offset, 4));
$offset += 4;
}

Expand All @@ -60,12 +60,12 @@ public function process(ServerInterface $server) : bool{
$len = ord($packet[$offset++]);
$address = substr($packet, $offset, $len);
$offset += $len;
$port = Binary::readShort(substr($packet, $offset, 2));
$port = LE::unpackUnsignedShort(substr($packet, $offset, 2));
$offset += 2;
$payload = substr($packet, $offset);
$server->sendRaw($address, $port, $payload);
}elseif($id === ITCProtocol::PACKET_CLOSE_SESSION){
$sessionId = Binary::readInt(substr($packet, $offset, 4));
$sessionId = LE::unpackUnsignedInt(substr($packet, $offset, 4));
$server->closeSession($sessionId);
}elseif($id === ITCProtocol::PACKET_SET_NAME){
$server->setName(substr($packet, $offset));
Expand All @@ -74,13 +74,13 @@ public function process(ServerInterface $server) : bool{
}elseif($id === ITCProtocol::PACKET_DISABLE_PORT_CHECK){
$server->setPortCheck(false);
}elseif($id === ITCProtocol::PACKET_SET_PACKETS_PER_TICK_LIMIT){
$limit = Binary::readLong(substr($packet, $offset, 8));
$limit = LE::unpackUnsignedLong(substr($packet, $offset, 8));
$server->setPacketsPerTickLimit($limit);
}elseif($id === ITCProtocol::PACKET_BLOCK_ADDRESS){
$len = ord($packet[$offset++]);
$address = substr($packet, $offset, $len);
$offset += $len;
$timeout = Binary::readInt(substr($packet, $offset, 4));
$timeout = LE::unpackUnsignedInt(substr($packet, $offset, 4));
$server->blockAddress($address, $timeout);
}elseif($id === ITCProtocol::PACKET_UNBLOCK_ADDRESS){
$len = ord($packet[$offset++]);
Expand Down
14 changes: 7 additions & 7 deletions src/UserToRakLibThreadMessageSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace raklib\server\ipc;

use pocketmine\utils\Binary;
use pmmp\encoding\LE;
use raklib\protocol\EncapsulatedPacket;
use raklib\protocol\PacketReliability;
use raklib\server\ipc\UserToRakLibThreadMessageProtocol as ITCProtocol;
Expand All @@ -36,22 +36,22 @@ public function sendEncapsulated(int $sessionId, EncapsulatedPacket $packet, boo
($packet->identifierACK !== null ? ITCProtocol::ENCAPSULATED_FLAG_NEED_ACK : 0);

$buffer = chr(ITCProtocol::PACKET_ENCAPSULATED) .
Binary::writeInt($sessionId) .
LE::packUnsignedInt($sessionId) .
chr($flags) .
chr($packet->reliability) .
($packet->identifierACK !== null ? Binary::writeInt($packet->identifierACK) : "") .
($packet->identifierACK !== null ? LE::packSignedInt($packet->identifierACK) : "") .
(PacketReliability::isSequencedOrOrdered($packet->reliability) ? chr($packet->orderChannel) : "") .
$packet->buffer;
$this->channel->write($buffer);
}

public function sendRaw(string $address, int $port, string $payload) : void{
$buffer = chr(ITCProtocol::PACKET_RAW) . chr(strlen($address)) . $address . Binary::writeShort($port) . $payload;
$buffer = chr(ITCProtocol::PACKET_RAW) . chr(strlen($address)) . $address . LE::packUnsignedShort($port) . $payload;
$this->channel->write($buffer);
}

public function closeSession(int $sessionId) : void{
$buffer = chr(ITCProtocol::PACKET_CLOSE_SESSION) . Binary::writeInt($sessionId);
$buffer = chr(ITCProtocol::PACKET_CLOSE_SESSION) . LE::packUnsignedInt($sessionId);
$this->channel->write($buffer);
}

Expand All @@ -64,11 +64,11 @@ public function setPortCheck(bool $value) : void{
}

public function setPacketsPerTickLimit(int $limit) : void{
$this->channel->write(chr(ITCProtocol::PACKET_SET_PACKETS_PER_TICK_LIMIT) . Binary::writeLong($limit));
$this->channel->write(chr(ITCProtocol::PACKET_SET_PACKETS_PER_TICK_LIMIT) . LE::packUnsignedLong($limit));
}

public function blockAddress(string $address, int $timeout) : void{
$buffer = chr(ITCProtocol::PACKET_BLOCK_ADDRESS) . chr(strlen($address)) . $address . Binary::writeInt($timeout);
$buffer = chr(ITCProtocol::PACKET_BLOCK_ADDRESS) . chr(strlen($address)) . $address . LE::packUnsignedInt($timeout);
$this->channel->write($buffer);
}

Expand Down