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