Added encode packet timings

these changes required some new APIs in BedrockProtocol.
This commit is contained in:
Dylan K. Taylor
2023-02-19 16:47:20 +00:00
parent 324bc27b5a
commit 9cdb641936
5 changed files with 49 additions and 15 deletions

View File

@ -120,6 +120,7 @@ use pocketmine\player\XboxLivePlayerInfo;
use pocketmine\Server;
use pocketmine\timings\Timings;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\BinaryStream;
use pocketmine\utils\ObjectSet;
use pocketmine\utils\TextFormat;
use pocketmine\utils\Utils;
@ -176,7 +177,7 @@ class NetworkSession{
private ?EncryptionContext $cipher = null;
/** @var Packet[] */
/** @var ClientboundPacket[] */
private array $sendBuffer = [];
/**
@ -263,6 +264,23 @@ class NetworkSession{
);
}
/**
* @param ClientboundPacket[] $packets
*/
public static function encodePacketBatchTimed(BinaryStream $stream, PacketSerializerContext $context, array $packets) : void{
PacketBatch::encodeRaw($stream, array_map(function(ClientboundPacket $packet) use ($context) : string{
$timings = Timings::getEncodeDataPacketTimings($packet);
$timings->startTiming();
try{
$stream = PacketSerializer::encoder($this->packetSerializerContext);
$packet->encode($stream);
return $stream->getBuffer();
}finally{
$timings->stopTiming();
}
}, $packets));
}
private function onPlayerCreated(Player $player) : void{
if(!$this->isConnected()){
//the remote player might have disconnected before spawn terrain generation was finished
@ -502,12 +520,14 @@ class NetworkSession{
$syncMode = false;
}
$batch = PacketBatch::fromPackets($this->packetSerializerContext, ...$this->sendBuffer);
$stream = new BinaryStream();
self::encodePacketBatchTimed($stream, $this->packetSerializerContext, $this->sendBuffer);
if($this->enableCompression){
$promise = $this->server->prepareBatch($batch, $this->compressor, $syncMode);
$promise = $this->server->prepareBatch(new PacketBatch($stream->getBuffer()), $this->compressor, $syncMode);
}else{
$promise = new CompressBatchPromise();
$promise->resolve($batch->getBuffer());
$promise->resolve($stream->getBuffer());
}
$this->sendBuffer = [];
$this->queueCompressedNoBufferFlush($promise, $immediate);

View File

@ -25,6 +25,7 @@ namespace pocketmine\network\mcpe;
use pocketmine\network\mcpe\protocol\serializer\PacketBatch;
use pocketmine\Server;
use pocketmine\utils\BinaryStream;
use function spl_object_id;
final class StandardPacketBroadcaster implements PacketBroadcaster{
@ -38,7 +39,9 @@ final class StandardPacketBroadcaster implements PacketBroadcaster{
$serializerContext = $recipient->getPacketSerializerContext();
$bufferId = spl_object_id($serializerContext);
if(!isset($buffers[$bufferId])){
$buffers[$bufferId] = PacketBatch::fromPackets($serializerContext, ...$packets);
$stream = new BinaryStream();
NetworkSession::encodePacketBatchTimed($stream, $serializerContext, $packets);
$buffers[$bufferId] = $stream->getBuffer();
}
//TODO: different compressors might be compatible, it might not be necessary to split them up by object
@ -52,14 +55,14 @@ final class StandardPacketBroadcaster implements PacketBroadcaster{
$buffer = $buffers[$bufferId];
foreach($compressorMap as $compressorId => $compressorTargets){
$compressor = $compressors[$compressorId];
if(!$compressor->willCompress($buffer->getBuffer())){
if(!$compressor->willCompress($buffer)){
foreach($compressorTargets as $target){
foreach($packets as $pk){
$target->addToSendBuffer($pk);
}
}
}else{
$promise = $this->server->prepareBatch($buffer, $compressor);
$promise = $this->server->prepareBatch(new PacketBatch($buffer), $compressor);
foreach($compressorTargets as $target){
$target->queueCompressed($promise);
}