Server->batchPackets() now only accepts DataPacket objects, fixed players receiving double PlayerListPackets with their own data

This commit is contained in:
Dylan K. Taylor 2017-05-17 17:15:42 +01:00
parent 15b47fcb2f
commit fe8cb8cd86
2 changed files with 29 additions and 30 deletions

View File

@ -1750,7 +1750,7 @@ class Server{
$packet->encode();
$packet->isEncoded = true;
if(Network::$BATCH_THRESHOLD >= 0 and strlen($packet->buffer) >= Network::$BATCH_THRESHOLD){
$this->batchPackets($players, [$packet->buffer], false);
$this->batchPackets($players, [$packet], false);
return;
}
@ -1765,20 +1765,14 @@ class Server{
/**
* Broadcasts a list of packets in a batch to a list of players
*
* @param Player[] $players
* @param DataPacket[]|string $packets
* @param bool $forceSync
* @param bool $immediate
* @param Player[] $players
* @param DataPacket[] $packets
* @param bool $forceSync
* @param bool $immediate
*/
public function batchPackets(array $players, array $packets, $forceSync = false, bool $immediate = false){
Timings::$playerNetworkTimer->startTiming();
$pk = new BatchPacket();
foreach($packets as $p){
$pk->addPacket($p);
}
$targets = [];
foreach($players as $p){
if($p->isConnected()){
@ -1786,12 +1780,20 @@ class Server{
}
}
if(!$forceSync and $this->networkCompressionAsync){
$task = new CompressBatchedTask($pk, $targets, $this->networkCompressionLevel, $immediate);
$this->getScheduler()->scheduleAsyncTask($task);
}else{
$pk->compress($this->networkCompressionLevel);
$this->broadcastPacketsCallback($pk, $targets, $immediate);
if(count($targets) > 0){
$pk = new BatchPacket();
foreach($packets as $p){
$pk->addPacket($p);
}
if(!$forceSync and $this->networkCompressionAsync){
$task = new CompressBatchedTask($pk, $targets, $this->networkCompressionLevel, $immediate);
$this->getScheduler()->scheduleAsyncTask($task);
}else{
$pk->compress($this->networkCompressionLevel);
$this->broadcastPacketsCallback($pk, $targets, $immediate);
}
}
Timings::$playerNetworkTimer->stopTiming();
@ -2168,9 +2170,9 @@ class Server{
}
public function addOnlinePlayer(Player $player){
$this->playerList[$player->getRawUniqueId()] = $player;
$this->updatePlayerListData($player->getUniqueId(), $player->getId(), $player->getDisplayName(), $player->getSkinId(), $player->getSkinData());
$this->playerList[$player->getRawUniqueId()] = $player;
}
public function removeOnlinePlayer(Player $player){

View File

@ -54,20 +54,17 @@ class BatchPacket extends DataPacket{
}
/**
* @param DataPacket|string $packet
* @param DataPacket $packet
*/
public function addPacket($packet){
if($packet instanceof DataPacket){
if(!$packet->canBeBatched()){
throw new \InvalidArgumentException(get_class($packet) . " cannot be put inside a BatchPacket");
}
if(!$packet->isEncoded){
$packet->encode();
}
$packet = $packet->buffer;
public function addPacket(DataPacket $packet){
if(!$packet->canBeBatched()){
throw new \InvalidArgumentException(get_class($packet) . " cannot be put inside a BatchPacket");
}
if(!$packet->isEncoded){
$packet->encode();
}
$this->payload .= Binary::writeUnsignedVarInt(strlen($packet)) . $packet;
$this->payload .= Binary::writeUnsignedVarInt(strlen($packet->buffer)) . $packet->buffer;
}
public function compress(int $level = 7){