Extract a Packet interface from DataPacket

this is in preparation for clientbound/serverbound packet separation. I did this already on another branch, but the changeset was dependent on a massive refactor to split apart packets and binarystream which i'm still not fully happy with.
This commit is contained in:
Dylan K. Taylor
2019-01-17 12:21:56 +00:00
parent 9c0ebb6350
commit b82e00ffdf
15 changed files with 167 additions and 79 deletions

View File

@ -34,8 +34,8 @@ use pocketmine\network\mcpe\handler\PreSpawnSessionHandler;
use pocketmine\network\mcpe\handler\ResourcePacksSessionHandler;
use pocketmine\network\mcpe\handler\SessionHandler;
use pocketmine\network\mcpe\handler\SimpleSessionHandler;
use pocketmine\network\mcpe\protocol\DataPacket;
use pocketmine\network\mcpe\protocol\DisconnectPacket;
use pocketmine\network\mcpe\protocol\Packet;
use pocketmine\network\mcpe\protocol\PacketPool;
use pocketmine\network\mcpe\protocol\PlayStatusPacket;
use pocketmine\network\mcpe\protocol\ServerToClientHandshakePacket;
@ -209,11 +209,11 @@ class NetworkSession{
}
/**
* @param DataPacket $packet
* @param Packet $packet
*
* @throws BadPacketException
*/
public function handleDataPacket(DataPacket $packet) : void{
public function handleDataPacket(Packet $packet) : void{
$timings = Timings::getReceiveDataPacketTimings($packet);
$timings->startTiming();
@ -237,7 +237,7 @@ class NetworkSession{
$timings->stopTiming();
}
public function sendDataPacket(DataPacket $packet, bool $immediate = false) : bool{
public function sendDataPacket(Packet $packet, bool $immediate = false) : bool{
$timings = Timings::getSendDataPacketTimings($packet);
$timings->startTiming();
try{
@ -260,9 +260,9 @@ class NetworkSession{
/**
* @internal
* @param DataPacket $packet
* @param Packet $packet
*/
public function addToSendBuffer(DataPacket $packet) : void{
public function addToSendBuffer(Packet $packet) : void{
$timings = Timings::getSendDataPacketTimings($packet);
$timings->startTiming();
try{

View File

@ -23,19 +23,19 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe;
use pocketmine\network\mcpe\protocol\DataPacket;
use pocketmine\network\mcpe\protocol\Packet;
use pocketmine\network\mcpe\protocol\PacketPool;
class PacketStream extends NetworkBinaryStream{
public function putPacket(DataPacket $packet) : void{
if(!$packet->isEncoded){
public function putPacket(Packet $packet) : void{
if(!$packet->isEncoded()){
$packet->encode();
}
$this->putString($packet->getBuffer());
}
public function getPacket() : DataPacket{
public function getPacket() : Packet{
return PacketPool::getPacket($this->getString());
}
}

View File

@ -36,12 +36,12 @@ use function is_object;
use function is_string;
use function method_exists;
abstract class DataPacket extends NetworkBinaryStream{
abstract class DataPacket extends NetworkBinaryStream implements Packet{
public const NETWORK_ID = 0;
/** @var bool */
public $isEncoded = false;
private $isEncoded = false;
/** @var int */
public $senderSubId = 0;
@ -108,6 +108,10 @@ abstract class DataPacket extends NetworkBinaryStream{
$this->isEncoded = true;
}
final public function isEncoded() : bool{
return $this->isEncoded;
}
protected function encodeHeader() : void{
$this->putUnsignedVarInt(static::NETWORK_ID);
}

View File

@ -0,0 +1,82 @@
<?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\BadPacketException;
use pocketmine\network\mcpe\handler\SessionHandler;
interface Packet{
public function setOffset(int $offset) : void;
public function setBuffer(string $buffer = "", int $offset = 0);
public function getOffset() : int;
public function getBuffer() : string;
/**
* Returns whether the offset has reached the end of the buffer.
* @return bool
*/
public function feof() : bool;
public function pid() : int;
public function getName() : string;
public function canBeSentBeforeLogin() : bool;
/**
* Returns whether the packet may legally have unread bytes left in the buffer.
* @return bool
*/
public function mayHaveUnreadBytes() : bool;
/**
* @throws BadPacketException
*/
public function decode() : void;
public function encode() : void;
public function isEncoded() : bool;
/**
* Performs handling for this packet. Usually you'll want an appropriately named method in the session handler for
* this.
*
* This method returns a bool to indicate whether the packet was handled or not. If the packet was unhandled, a
* debug message will be logged with a hexdump of the packet.
*
* Typically this method returns the return value of the handler in the supplied SessionHandler. See other packets
* for examples how to implement this.
*
* @param SessionHandler $handler
*
* @return bool true if the packet was handled successfully, false if not.
* @throws BadPacketException if broken data was found in the packet
*/
public function handle(SessionHandler $handler) : bool;
}

View File

@ -28,7 +28,7 @@ use pocketmine\utils\Binary;
use pocketmine\utils\BinaryDataException;
class PacketPool{
/** @var \SplFixedArray<DataPacket> */
/** @var \SplFixedArray<Packet> */
protected static $pool = null;
public static function init() : void{
@ -158,28 +158,28 @@ class PacketPool{
}
/**
* @param DataPacket $packet
* @param Packet $packet
*/
public static function registerPacket(DataPacket $packet) : void{
public static function registerPacket(Packet $packet) : void{
static::$pool[$packet->pid()] = clone $packet;
}
/**
* @param int $pid
*
* @return DataPacket
* @return Packet
*/
public static function getPacketById(int $pid) : DataPacket{
public static function getPacketById(int $pid) : Packet{
return isset(static::$pool[$pid]) ? clone static::$pool[$pid] : new UnknownPacket();
}
/**
* @param string $buffer
*
* @return DataPacket
* @return Packet
* @throws BadPacketException
*/
public static function getPacket(string $buffer) : DataPacket{
public static function getPacket(string $buffer) : Packet{
$offset = 0;
try{
$pk = static::getPacketById(Binary::readUnsignedVarInt($buffer, $offset));