Oops, forgot to do it correctly!

This commit is contained in:
Shoghi Cervantes 2015-04-14 20:39:37 +02:00
parent 6ee3a7b8d7
commit 42eda170b5
No known key found for this signature in database
GPG Key ID: 78464DB0A7837F89
2 changed files with 38 additions and 4 deletions

View File

@ -214,6 +214,8 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
private $needACK = []; private $needACK = [];
private $batchedPackets = [];
/** /**
* @var \pocketmine\scheduler\TaskHandler[] * @var \pocketmine\scheduler\TaskHandler[]
*/ */
@ -590,7 +592,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->chunkX = $x; $pk->chunkX = $x;
$pk->chunkZ = $z; $pk->chunkZ = $z;
$pk->data = $payload; $pk->data = $payload;
$this->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_CHUNKS)); $this->batchDataPacket($pk->setChannel(Network::CHANNEL_WORLD_CHUNKS));
if($this->spawned){ if($this->spawned){
foreach($this->level->getChunkEntities($x, $z) as $entity){ foreach($this->level->getChunkEntities($x, $z) as $entity){
@ -746,6 +748,31 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
return true; return true;
} }
/**
* Batch a Data packet into the channel list to send at the end of the tick
*
* @param DataPacket $packet
*
* @return bool
*/
public function batchDataPacket(DataPacket $packet){
if($this->connected === false){
return false;
}
$this->server->getPluginManager()->callEvent($ev = new DataPacketSendEvent($this, $packet));
if($ev->isCancelled()){
return false;
}
if(!isset($this->batchedPackets[$packet->getChannel()])){
$this->batchedPackets[$packet->getChannel()] = [];
}
$this->batchedPackets[$packet->getChannel()][] = clone $packet;
return true;
}
/** /**
* Sends an ordered DataPacket to the send buffer * Sends an ordered DataPacket to the send buffer
* *
@ -758,6 +785,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($this->connected === false){ if($this->connected === false){
return false; return false;
} }
$this->server->getPluginManager()->callEvent($ev = new DataPacketSendEvent($this, $packet)); $this->server->getPluginManager()->callEvent($ev = new DataPacketSendEvent($this, $packet));
if($ev->isCancelled()){ if($ev->isCancelled()){
return false; return false;
@ -1309,7 +1337,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if(count($this->moveToSend) > 0){ if(count($this->moveToSend) > 0){
$pk = new MoveEntityPacket(); $pk = new MoveEntityPacket();
$pk->entities = $this->moveToSend; $pk->entities = $this->moveToSend;
$this->dataPacket($pk->setChannel(Network::CHANNEL_MOVEMENT)); $this->batchDataPacket($pk->setChannel(Network::CHANNEL_MOVEMENT));
$this->moveToSend = []; $this->moveToSend = [];
} }
@ -1317,10 +1345,16 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if(count($this->motionToSend) > 0){ if(count($this->motionToSend) > 0){
$pk = new SetEntityMotionPacket(); $pk = new SetEntityMotionPacket();
$pk->entities = $this->motionToSend; $pk->entities = $this->motionToSend;
$this->dataPacket($pk->setChannel(Network::CHANNEL_MOVEMENT)); $this->batchDataPacket($pk->setChannel(Network::CHANNEL_MOVEMENT));
$this->motionToSend = []; $this->motionToSend = [];
} }
if(count($this->batchedPackets) > 0){
foreach($this->batchedPackets as $channel => $list){
$this->server->batchPackets([$this], $list, false, $channel);
}
$this->batchedPackets = [];
}
$this->lastUpdate = $currentTick; $this->lastUpdate = $currentTick;

View File

@ -220,7 +220,7 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
if(!$immediate and !$needACK and $packet->pid() !== ProtocolInfo::BATCH_PACKET if(!$immediate and !$needACK and $packet->pid() !== ProtocolInfo::BATCH_PACKET
and Network::$BATCH_THRESHOLD >= 0 and Network::$BATCH_THRESHOLD >= 0
and strlen($packet->buffer) >= Network::$BATCH_THRESHOLD){ and strlen($packet->buffer) >= Network::$BATCH_THRESHOLD){
$this->server->batchPackets([$player], [$packet], false, $packet->getChannel()); $this->server->batchPackets([$player], [$packet], true, $packet->getChannel());
return null; return null;
} }