Avoid use of internal fields in BinaryStream

This commit is contained in:
Dylan K. Taylor 2019-01-01 16:42:14 +00:00
parent 4a629e1a26
commit f81bbd60e8
9 changed files with 21 additions and 20 deletions

12
composer.lock generated
View File

@ -233,16 +233,16 @@
}, },
{ {
"name": "pocketmine/binaryutils", "name": "pocketmine/binaryutils",
"version": "0.1.3", "version": "0.1.4",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/pmmp/BinaryUtils.git", "url": "https://github.com/pmmp/BinaryUtils.git",
"reference": "925001c8eff97a36519b16bbd095964b0fc52772" "reference": "8c559e8c437aa56143e1a9231debf734b4de481c"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/pmmp/BinaryUtils/zipball/925001c8eff97a36519b16bbd095964b0fc52772", "url": "https://api.github.com/repos/pmmp/BinaryUtils/zipball/8c559e8c437aa56143e1a9231debf734b4de481c",
"reference": "925001c8eff97a36519b16bbd095964b0fc52772", "reference": "8c559e8c437aa56143e1a9231debf734b4de481c",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -260,10 +260,10 @@
], ],
"description": "Classes and methods for conveniently handling binary data", "description": "Classes and methods for conveniently handling binary data",
"support": { "support": {
"source": "https://github.com/pmmp/BinaryUtils/tree/0.1.3", "source": "https://github.com/pmmp/BinaryUtils/tree/master",
"issues": "https://github.com/pmmp/BinaryUtils/issues" "issues": "https://github.com/pmmp/BinaryUtils/issues"
}, },
"time": "2018-12-30T12:17:51+00:00" "time": "2019-01-01T15:58:16+00:00"
}, },
{ {
"name": "pocketmine/math", "name": "pocketmine/math",

View File

@ -1900,7 +1900,7 @@ class Server{
$stream->putPacket($packet); $stream->putPacket($packet);
} }
if(NetworkCompression::$THRESHOLD < 0 or strlen($stream->buffer) < NetworkCompression::$THRESHOLD){ if(NetworkCompression::$THRESHOLD < 0 or strlen($stream->getBuffer()) < NetworkCompression::$THRESHOLD){
foreach($targets as $target){ foreach($targets as $target){
foreach($ev->getPackets() as $pk){ foreach($ev->getPackets() as $pk){
$target->addToSendBuffer($pk); $target->addToSendBuffer($pk);
@ -1929,17 +1929,18 @@ class Server{
Timings::$playerNetworkSendCompressTimer->startTiming(); Timings::$playerNetworkSendCompressTimer->startTiming();
$compressionLevel = NetworkCompression::$LEVEL; $compressionLevel = NetworkCompression::$LEVEL;
if(NetworkCompression::$THRESHOLD < 0 or strlen($stream->buffer) < NetworkCompression::$THRESHOLD){ $buffer = $stream->getBuffer();
if(NetworkCompression::$THRESHOLD < 0 or strlen($buffer) < NetworkCompression::$THRESHOLD){
$compressionLevel = 0; //Do not compress packets under the threshold $compressionLevel = 0; //Do not compress packets under the threshold
$forceSync = true; $forceSync = true;
} }
$promise = new CompressBatchPromise(); $promise = new CompressBatchPromise();
if(!$forceSync and $this->networkCompressionAsync){ if(!$forceSync and $this->networkCompressionAsync){
$task = new CompressBatchTask($stream, $compressionLevel, $promise); $task = new CompressBatchTask($buffer, $compressionLevel, $promise);
$this->asyncPool->submitTask($task); $this->asyncPool->submitTask($task);
}else{ }else{
$promise->resolve(NetworkCompression::compress($stream->buffer, $compressionLevel)); $promise->resolve(NetworkCompression::compress($buffer, $compressionLevel));
} }
return $promise; return $promise;

View File

@ -105,7 +105,7 @@ class CraftingManager{
$batch->putPacket($pk); $batch->putPacket($pk);
$this->craftingDataCache = new CompressBatchPromise(); $this->craftingDataCache = new CompressBatchPromise();
$this->craftingDataCache->resolve(NetworkCompression::compress($batch->buffer)); $this->craftingDataCache->resolve(NetworkCompression::compress($batch->getBuffer()));
Timings::$craftingDataCacheRebuildTimer->stopTiming(); Timings::$craftingDataCacheRebuildTimer->stopTiming();
} }

View File

@ -56,7 +56,7 @@ class ChunkRequestTask extends AsyncTask{
$stream = new PacketStream(); $stream = new PacketStream();
$stream->putPacket($pk); $stream->putPacket($pk);
$this->setResult(NetworkCompression::compress($stream->buffer, $this->compressionLevel)); $this->setResult(NetworkCompression::compress($stream->getBuffer(), $this->compressionLevel));
} }
public function onCompletion() : void{ public function onCompletion() : void{

View File

@ -31,12 +31,12 @@ class CompressBatchTask extends AsyncTask{
private $data; private $data;
/** /**
* @param PacketStream $stream * @param string $data
* @param int $compressionLevel * @param int $compressionLevel
* @param CompressBatchPromise $promise * @param CompressBatchPromise $promise
*/ */
public function __construct(PacketStream $stream, int $compressionLevel, CompressBatchPromise $promise){ public function __construct(string $data, int $compressionLevel, CompressBatchPromise $promise){
$this->data = $stream->buffer; $this->data = $data;
$this->level = $compressionLevel; $this->level = $compressionLevel;
$this->storeLocal($promise); $this->storeLocal($promise);
} }

View File

@ -198,14 +198,14 @@ class NetworkSession{
$packet->decode(); $packet->decode();
if(!$packet->feof() and !$packet->mayHaveUnreadBytes()){ if(!$packet->feof() and !$packet->mayHaveUnreadBytes()){
$remains = substr($packet->buffer, $packet->offset); $remains = substr($packet->getBuffer(), $packet->getOffset());
$this->server->getLogger()->debug("Still " . strlen($remains) . " bytes unread in " . $packet->getName() . ": 0x" . bin2hex($remains)); $this->server->getLogger()->debug("Still " . strlen($remains) . " bytes unread in " . $packet->getName() . ": 0x" . bin2hex($remains));
} }
$ev = new DataPacketReceiveEvent($this->player, $packet); $ev = new DataPacketReceiveEvent($this->player, $packet);
$ev->call(); $ev->call();
if(!$ev->isCancelled() and !$packet->handle($this->handler)){ if(!$ev->isCancelled() and !$packet->handle($this->handler)){
$this->server->getLogger()->debug("Unhandled " . $packet->getName() . " received from " . $this->player->getName() . ": 0x" . bin2hex($packet->buffer)); $this->server->getLogger()->debug("Unhandled " . $packet->getName() . " received from " . $this->player->getName() . ": 0x" . bin2hex($packet->getBuffer()));
} }
$timings->stopTiming(); $timings->stopTiming();

View File

@ -32,7 +32,7 @@ class PacketStream extends NetworkBinaryStream{
if(!$packet->isEncoded){ if(!$packet->isEncoded){
$packet->encode(); $packet->encode();
} }
$this->putString($packet->buffer); $this->putString($packet->getBuffer());
} }
public function getPacket() : DataPacket{ public function getPacket() : DataPacket{

View File

@ -62,7 +62,7 @@ abstract class DataPacket extends NetworkBinaryStream{
} }
public function decode() : void{ public function decode() : void{
$this->offset = 0; $this->rewind();
$this->decodeHeader(); $this->decodeHeader();
$this->decodePayload(); $this->decodePayload();
} }

View File

@ -262,7 +262,7 @@ class StartGamePacket extends DataPacket{
$stream->putString($v["name"]); $stream->putString($v["name"]);
$stream->putLShort($v["data"]); $stream->putLShort($v["data"]);
} }
self::$runtimeIdTable = $stream->buffer; self::$runtimeIdTable = $stream->getBuffer();
} }
$this->put(self::$runtimeIdTable); $this->put(self::$runtimeIdTable);