mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-12 12:55:21 +00:00
DataPacket: account for splitscreen header when decoding
This commit is contained in:
parent
93597dcd50
commit
04581e2700
@ -39,6 +39,12 @@ abstract class DataPacket extends NetworkBinaryStream{
|
|||||||
|
|
||||||
public const NETWORK_ID = 0;
|
public const NETWORK_ID = 0;
|
||||||
|
|
||||||
|
public const PID_MASK = 0x3ff; //10 bits
|
||||||
|
|
||||||
|
private const SUBCLIENT_ID_MASK = 0x03; //2 bits
|
||||||
|
private const SENDER_SUBCLIENT_ID_SHIFT = 10;
|
||||||
|
private const RECIPIENT_SUBCLIENT_ID_SHIFT = 12;
|
||||||
|
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
public $isEncoded = false;
|
public $isEncoded = false;
|
||||||
/** @var CachedEncapsulatedPacket|null */
|
/** @var CachedEncapsulatedPacket|null */
|
||||||
@ -92,10 +98,13 @@ abstract class DataPacket extends NetworkBinaryStream{
|
|||||||
* @throws \UnexpectedValueException
|
* @throws \UnexpectedValueException
|
||||||
*/
|
*/
|
||||||
protected function decodeHeader(){
|
protected function decodeHeader(){
|
||||||
$pid = $this->getUnsignedVarInt();
|
$header = $this->getUnsignedVarInt();
|
||||||
|
$pid = $header & self::PID_MASK;
|
||||||
if($pid !== static::NETWORK_ID){
|
if($pid !== static::NETWORK_ID){
|
||||||
throw new \UnexpectedValueException("Expected " . static::NETWORK_ID . " for packet ID, got $pid");
|
throw new \UnexpectedValueException("Expected " . static::NETWORK_ID . " for packet ID, got $pid");
|
||||||
}
|
}
|
||||||
|
$this->senderSubId = ($header >> self::SENDER_SUBCLIENT_ID_SHIFT) & self::SUBCLIENT_ID_MASK;
|
||||||
|
$this->recipientSubId = ($header >> self::RECIPIENT_SUBCLIENT_ID_SHIFT) & self::SUBCLIENT_ID_MASK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -123,7 +132,11 @@ abstract class DataPacket extends NetworkBinaryStream{
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function encodeHeader(){
|
protected function encodeHeader(){
|
||||||
$this->putUnsignedVarInt(static::NETWORK_ID);
|
$this->putUnsignedVarInt(
|
||||||
|
static::NETWORK_ID |
|
||||||
|
($this->senderSubId << self::SENDER_SUBCLIENT_ID_SHIFT) |
|
||||||
|
($this->recipientSubId << self::RECIPIENT_SUBCLIENT_ID_SHIFT)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -196,7 +196,7 @@ class PacketPool{
|
|||||||
*/
|
*/
|
||||||
public static function getPacket(string $buffer) : DataPacket{
|
public static function getPacket(string $buffer) : DataPacket{
|
||||||
$offset = 0;
|
$offset = 0;
|
||||||
$pk = static::getPacketById(Binary::readUnsignedVarInt($buffer, $offset));
|
$pk = static::getPacketById(Binary::readUnsignedVarInt($buffer, $offset) & DataPacket::PID_MASK);
|
||||||
$pk->setBuffer($buffer, $offset);
|
$pk->setBuffer($buffer, $offset);
|
||||||
|
|
||||||
return $pk;
|
return $pk;
|
||||||
|
42
tests/phpunit/network/mcpe/protocol/DataPacketTest.php
Normal file
42
tests/phpunit/network/mcpe/protocol/DataPacketTest.php
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\network\mcpe\protocol;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class DataPacketTest extends TestCase{
|
||||||
|
|
||||||
|
public function testHeaderFidelity() : void{
|
||||||
|
$pk = new TestPacket();
|
||||||
|
$pk->senderSubId = 3;
|
||||||
|
$pk->recipientSubId = 2;
|
||||||
|
$pk->encode();
|
||||||
|
|
||||||
|
$pk2 = new TestPacket();
|
||||||
|
$pk2->setBuffer($pk->getBuffer());
|
||||||
|
$pk2->decode();
|
||||||
|
self::assertSame($pk2->senderSubId, 3);
|
||||||
|
self::assertSame($pk2->recipientSubId, 2);
|
||||||
|
}
|
||||||
|
}
|
34
tests/phpunit/network/mcpe/protocol/TestPacket.php
Normal file
34
tests/phpunit/network/mcpe/protocol/TestPacket.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\network\mcpe\protocol;
|
||||||
|
|
||||||
|
use pocketmine\network\mcpe\NetworkSession;
|
||||||
|
|
||||||
|
class TestPacket extends DataPacket{
|
||||||
|
public const NETWORK_ID = 1023;
|
||||||
|
|
||||||
|
public function handle(NetworkSession $handler) : bool{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user