Implemented Channeled packet sending

This commit is contained in:
Shoghi Cervantes 2015-04-14 18:24:40 +02:00
parent bb945446b7
commit 0b176b3fe0
29 changed files with 170 additions and 120 deletions

View File

@ -90,6 +90,7 @@ use pocketmine\nbt\tag\Enum;
use pocketmine\nbt\tag\Float;
use pocketmine\nbt\tag\Int;
use pocketmine\nbt\tag\String;
use pocketmine\network\Network;
use pocketmine\network\protocol\AdventureSettingsPacket;
use pocketmine\network\protocol\AnimatePacket;
use pocketmine\network\protocol\BatchPacket;
@ -589,7 +590,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->chunkX = $x;
$pk->chunkZ = $z;
$pk->data = $payload;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_CHUNKS));
if($this->spawned){
foreach($this->level->getChunkEntities($x, $z) as $entity){
@ -641,7 +642,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk = new SetTimePacket();
$pk->time = $this->level->getTime();
$pk->started = $this->level->stopTime == false;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
$pos = $this->level->getSafeSpawn($this);
@ -651,7 +652,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::PLAYER_SPAWN;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_CHUNKS));
$this->server->getPluginManager()->callEvent($ev = new PlayerJoinEvent($this,
new TranslationContainer(TextFormat::YELLOW . "%multiplayer.player.joined", [
@ -847,7 +848,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->x = (int) $this->spawnPosition->x;
$pk->y = (int) $this->spawnPosition->y;
$pk->z = (int) $this->spawnPosition->z;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
public function stopSleep(){
@ -965,7 +966,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->generator = 1; //0 old, 1 infinite, 2 flat
$pk->gamemode = $this->gamemode & 0x01;
$pk->eid = $this->getId();
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
$this->sendSettings();
return true;
@ -1026,7 +1027,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk = new AdventureSettingsPacket();
$pk->flags = $flags;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
}
public function isSurvival(){
@ -1183,7 +1184,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->bodyYaw = $from->yaw;
$pk->pitch = $from->pitch;
$pk->mode = 1;
$this->directDataPacket($pk);
$this->directDataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
$this->forceMovement = new Vector3($from->x, $from->y, $from->z);
}else{
$this->forceMovement = null;
@ -1258,7 +1259,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk = new TakeItemEntityPacket();
$pk->eid = $this->getId();
$pk->target = $entity->getId();
Server::broadcastPacket($entity->getViewers(), $pk);
Server::broadcastPacket($entity->getViewers(), $pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
$this->inventory->addItem(clone $item);
$entity->kill();
}
@ -1288,7 +1289,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk = new TakeItemEntityPacket();
$pk->eid = $this->getId();
$pk->target = $entity->getId();
Server::broadcastPacket($entity->getViewers(), $pk);
Server::broadcastPacket($entity->getViewers(), $pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
$this->inventory->addItem(clone $item);
$entity->kill();
}
@ -1308,7 +1309,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if(count($this->moveToSend) > 0){
$pk = new MoveEntityPacket();
$pk->entities = $this->moveToSend;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_MOVEMENT));
$this->moveToSend = [];
}
@ -1316,7 +1317,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if(count($this->motionToSend) > 0){
$pk = new SetEntityMotionPacket();
$pk->entities = $this->motionToSend;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_MOVEMENT));
$this->motionToSend = [];
}
@ -1377,13 +1378,13 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_CLIENT;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
}else{
$message = "disconnectionScreen.outdatedServer";
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_SERVER;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
}
$this->close("", $message, false);
@ -1493,7 +1494,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_SUCCESS;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
if($this->spawnPosition === null and isset($this->namedtag->SpawnLevel) and ($level = $this->server->getLevelByName($this->namedtag["SpawnLevel"])) instanceof Level){
$this->spawnPosition = new Position($this->namedtag["SpawnX"], $this->namedtag["SpawnY"], $this->namedtag["SpawnZ"], $level);
@ -1514,35 +1515,35 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->generator = 1; //0 old, 1 infinite, 2 flat
$pk->gamemode = $this->gamemode & 0x01;
$pk->eid = $this->getId(); //Always use EntityID as zero for the actual player
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
$pk = new RespawnPacket();
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
$pk = new SetTimePacket();
$pk->time = $this->level->getTime();
$pk->started = $this->level->stopTime == false;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
$pk = new SetSpawnPositionPacket();
$pk->x = (int) $spawnPosition->x;
$pk->y = (int) $spawnPosition->y;
$pk->z = (int) $spawnPosition->z;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
$pk = new SetHealthPacket();
$pk->health = $this->getHealth();
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
if($this->getHealth() <= 0){
$this->dead = true;
}
$pk = new SetDifficultyPacket();
$pk->difficulty = $this->server->getDifficulty();
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
$this->server->getLogger()->info($this->getServer()->getLanguage()->translateString("pocketmine.player.logIn", [
TextFormat::AQUA . $this->username . TextFormat::WHITE,
@ -1582,7 +1583,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->pitch = $packet->pitch;
$pk->yaw = $packet->yaw;
$pk->mode = 1;
$this->directDataPacket($pk);
$this->directDataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
}else{
$packet->yaw %= 360;
$packet->pitch %= 360;
@ -2113,7 +2114,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk = new AnimatePacket();
$pk->eid = $this->getId();
$pk->action = $ev->getAnimationType();
Server::broadcastPacket($this->getViewers(), $pk);
Server::broadcastPacket($this->getViewers(), $pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
break;
case ProtocolInfo::SET_HEALTH_PACKET: //Not used
break;
@ -2167,8 +2168,8 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk = new EntityEventPacket();
$pk->eid = $this->getId();
$pk->event = 9;
$pk->setChannel(Network::CHANNEL_WORLD_EVENTS);
$this->dataPacket($pk);
$pk->eid = $this->getId();
Server::broadcastPacket($this->getViewers(), $pk);
$amount = $items[$slot->getId()];
@ -2468,7 +2469,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk = new TextPacket();
$pk->type = TextPacket::TYPE_RAW;
$pk->message = $m;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_TEXT));
}
}
}
@ -2486,14 +2487,14 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->type = TextPacket::TYPE_RAW;
$pk->message = $this->server->getLanguage()->translateString($message, $parameters);
}
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_TEXT));
}
public function sendPopup($message){
$pk = new TextPacket();
$pk->type = TextPacket::TYPE_POPUP;
$pk->message = $message;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_TEXT));
}
/**
@ -2511,7 +2512,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($reason != ""){
$pk = new DisconnectPacket;
$pk->message = $reason;
$this->directDataPacket($pk);
$this->directDataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
}
$this->connected = false;
@ -2755,7 +2756,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->x = $pos->x;
$pk->y = $pos->y;
$pk->z = $pos->z;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
}
@ -2764,7 +2765,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($this->spawned === true){
$pk = new SetHealthPacket();
$pk->health = $this->getHealth();
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
}
@ -2789,7 +2790,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk = new EntityEventPacket();
$pk->eid = $this->getId();
$pk->event = 2;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
}
@ -2819,7 +2820,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->pitch = $this->pitch;
$pk->yaw = $this->yaw;
$pk->mode = 1;
$this->directDataPacket($pk);
$this->directDataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
}
}

View File

@ -1751,7 +1751,7 @@ class Server{
$packet->encode();
$packet->isEncoded = true;
if(Network::$BATCH_THRESHOLD >= 0 and strlen($packet->buffer) >= Network::$BATCH_THRESHOLD){
Server::getInstance()->batchPackets($players, [$packet->buffer]);
Server::getInstance()->batchPackets($players, [$packet->buffer], false, $packet->getChannel());
return;
}
@ -1768,13 +1768,17 @@ class Server{
*
* @param Player[] $players
* @param DataPacket[]|string $packets
* @param bool $forceSync
* @param int $channel
*/
public function batchPackets(array $players, array $packets){
public function batchPackets(array $players, array $packets, $forceSync = false, $channel = 0){
$str = "";
foreach($packets as $p){
if(is_object($p)){
$p->encode();
if($p instanceof DataPacket){
if(!$p->isEncoded){
$p->encode();
}
$str .= $p->buffer;
}else{
$str .= $p;
@ -1786,19 +1790,21 @@ class Server{
$targets[] = $this->identifiers[spl_object_hash($p)];
}
if($this->networkCompressionAsync){
if(!$forceSync and $this->networkCompressionAsync){
$task = new CompressBatchedTask();
$task->targets = $targets;
$task->data = $str;
$task->channel = $channel;
$task->level = $this->networkCompressionLevel;
$this->getScheduler()->scheduleAsyncTask($task);
}else{
$this->broadcastPacketsCallback(zlib_encode($str, ZLIB_ENCODING_DEFLATE, $this->networkCompressionLevel), $targets);
$this->broadcastPacketsCallback(zlib_encode($str, ZLIB_ENCODING_DEFLATE, $this->networkCompressionLevel), $targets, $channel);
}
}
public function broadcastPacketsCallback($data, array $identifiers){
public function broadcastPacketsCallback($data, array $identifiers, $channel = 0){
$pk = new BatchPacket();
$pk->setChannel($channel);
$pk->payload = $data;
$pk->encode();
$pk->isEncoded = true;

View File

@ -24,6 +24,7 @@ namespace pocketmine\command\defaults;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\event\TranslationContainer;
use pocketmine\network\Network;
use pocketmine\network\protocol\SetDifficultyPacket;
use pocketmine\Server;
use pocketmine\utils\TextFormat;
@ -61,7 +62,7 @@ class DifficultyCommand extends VanillaCommand{
$pk = new SetDifficultyPacket();
$pk->difficulty = $sender->getServer()->getDifficulty();
Server::broadcastPacket($sender->getServer()->getOnlinePlayers(), $pk);
Server::broadcastPacket($sender->getServer()->getOnlinePlayers(), $pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
Command::broadcastCommandMessage($sender, new TranslationContainer("commands.difficulty.success", [$difficulty]));
}else{

View File

@ -24,6 +24,7 @@ namespace pocketmine\entity;
use pocketmine\level\format\FullChunk;
use pocketmine\level\particle\CriticalParticle;
use pocketmine\nbt\tag\Compound;
use pocketmine\network\Network;
use pocketmine\network\protocol\AddEntityPacket;
use pocketmine\Player;
@ -85,7 +86,7 @@ class Arrow extends Projectile{
$pk->speedY = $this->motionY;
$pk->speedZ = $this->motionZ;
$pk->metadata = $this->dataProperties;
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
parent::spawnTo($player);
}

View File

@ -23,6 +23,7 @@ namespace pocketmine\entity;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityRegainHealthEvent;
use pocketmine\network\Network;
use pocketmine\network\protocol\MobEffectPacket;
use pocketmine\Player;
use pocketmine\Server;
@ -234,21 +235,20 @@ class Effect{
}
public function add(Entity $entity, $modify = false){
$pk = new MobEffectPacket();
$pk->eid = $entity->getId();
$pk->effectId = $this->getId();
$pk->amplifier = $this->getAmplifier();
$pk->particles = $this->isVisible();
$pk->duration = $this->getDuration();
if($modify){
$pk->eventId = MobEffectPacket::EVENT_MODIFY;
}else{
$pk->eventId = MobEffectPacket::EVENT_ADD;
}
Server::broadcastPacket($entity->getViewers(), $pk);
if($entity instanceof Player){
$entity->dataPacket($pk);
$pk = new MobEffectPacket();
$pk->eid = $entity->getId();
$pk->effectId = $this->getId();
$pk->amplifier = $this->getAmplifier();
$pk->particles = $this->isVisible();
$pk->duration = $this->getDuration();
if($modify){
$pk->eventId = MobEffectPacket::EVENT_MODIFY;
}else{
$pk->eventId = MobEffectPacket::EVENT_ADD;
}
$entity->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
if($this->id === Effect::INVISIBILITY){
@ -258,13 +258,13 @@ class Effect{
}
public function remove(Entity $entity){
$pk = new MobEffectPacket();
$pk->eid = $entity->getId();
$pk->eventId = MobEffectPacket::EVENT_REMOVE;
$pk->effectId = $this->getId();
Server::broadcastPacket($entity->getViewers(), $pk);
if($entity instanceof Player){
$entity->dataPacket($pk);
$pk = new MobEffectPacket();
$pk->eid = $entity->getId();
$pk->eventId = MobEffectPacket::EVENT_REMOVE;
$pk->effectId = $this->getId();
$entity->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
if($this->id === Effect::INVISIBILITY){

View File

@ -51,7 +51,7 @@ use pocketmine\nbt\tag\Float;
use pocketmine\nbt\tag\Int;
use pocketmine\nbt\tag\Short;
use pocketmine\nbt\tag\String;
use pocketmine\Network;
use pocketmine\network\Network;
use pocketmine\network\protocol\MobEffectPacket;
use pocketmine\network\protocol\RemoveEntityPacket;
use pocketmine\network\protocol\SetEntityDataPacket;
@ -470,7 +470,7 @@ abstract class Entity extends Location implements Metadatable{
$pk->duration = $effect->getDuration();
$pk->eventId = MobEffectPacket::EVENT_ADD;
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
}
@ -493,7 +493,7 @@ abstract class Entity extends Location implements Metadatable{
$pk = new SetEntityDataPacket();
$pk->eid = $this->id;
$pk->metadata = $data === null ? $this->dataProperties : $data;
Server::broadcastPacket($player, $pk);
Server::broadcastPacket($player, $pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
/**
@ -503,7 +503,7 @@ abstract class Entity extends Location implements Metadatable{
if(isset($this->hasSpawned[$player->getId()])){
$pk = new RemoveEntityPacket();
$pk->eid = $this->id;
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
unset($this->hasSpawned[$player->getId()]);
}
}
@ -956,7 +956,7 @@ abstract class Entity extends Location implements Metadatable{
$pk = new SetTimePacket();
$pk->time = $this->level->getTime();
$pk->started = $this->level->stopTime == false;
$this->dataPacket($pk);
$this->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
$this->chunk = null;

View File

@ -32,6 +32,7 @@ use pocketmine\item\Item as ItemItem;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\Byte;
use pocketmine\nbt\tag\Int;
use pocketmine\network\Network;
use pocketmine\network\protocol\AddEntityPacket;
use pocketmine\Player;
@ -162,7 +163,7 @@ class FallingSand extends Entity{
$pk->yaw = $this->yaw;
$pk->pitch = $this->pitch;
$pk->metadata = $this->dataProperties;
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
parent::spawnTo($player);
}

View File

@ -29,7 +29,7 @@ use pocketmine\nbt\tag\Byte;
use pocketmine\nbt\tag\Compound;
use pocketmine\nbt\tag\Enum;
use pocketmine\nbt\tag\Short;
use pocketmine\Network;
use pocketmine\network\Network;
use pocketmine\network\protocol\AddPlayerPacket;
use pocketmine\network\protocol\RemovePlayerPacket;
use pocketmine\Player;
@ -212,7 +212,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
$pk->skin = $this->skin;
$pk->slim = $this->isSlim;
$pk->metadata = $this->dataProperties;
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
$player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ);
@ -225,7 +225,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
$pk = new RemovePlayerPacket();
$pk->eid = $this->id;
$pk->clientID = $this->id;
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
unset($this->hasSpawned[$player->getId()]);
}
}

View File

@ -31,6 +31,7 @@ use pocketmine\nbt\tag\Byte;
use pocketmine\nbt\tag\Compound;
use pocketmine\nbt\tag\Short;
use pocketmine\nbt\tag\String;
use pocketmine\network\Network;
use pocketmine\network\protocol\AddItemEntityPacket;
use pocketmine\Player;
@ -218,7 +219,7 @@ class Item extends Entity{
$pk->speedY = $this->motionY;
$pk->speedZ = $this->motionZ;
$pk->item = $this->getItem();
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
parent::spawnTo($player);
}

View File

@ -31,6 +31,7 @@ use pocketmine\event\Timings;
use pocketmine\item\Item as ItemItem;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\Short;
use pocketmine\network\Network;
use pocketmine\network\protocol\EntityEventPacket;
use pocketmine\Player;
use pocketmine\Server;
@ -107,7 +108,7 @@ abstract class Living extends Entity implements Damageable{
$pk = new EntityEventPacket();
$pk->eid = $this->getId();
$pk->event = $this->getHealth() <= 0 ? 3 : 2; //Ouch!
Server::broadcastPacket($this->hasSpawned, $pk);
Server::broadcastPacket($this->hasSpawned, $pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
$this->attackTime = 10; //0.5 seconds cooldown
}

View File

@ -27,6 +27,7 @@ use pocketmine\event\entity\EntityRegainHealthEvent;
use pocketmine\event\entity\ExplosionPrimeEvent;
use pocketmine\level\Explosion;
use pocketmine\nbt\tag\Byte;
use pocketmine\network\Network;
use pocketmine\network\protocol\AddEntityPacket;
use pocketmine\Player;
@ -138,7 +139,7 @@ class PrimedTNT extends Entity implements Explosive{
$pk->speedY = $this->motionY;
$pk->speedZ = $this->motionZ;
$pk->metadata = $this->dataProperties;
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
parent::spawnTo($player);
}

View File

@ -24,6 +24,7 @@ namespace pocketmine\entity;
use pocketmine\level\format\FullChunk;
use pocketmine\nbt\tag\Compound;
use pocketmine\network\Network;
use pocketmine\network\protocol\AddEntityPacket;
use pocketmine\Player;
@ -71,7 +72,7 @@ class Snowball extends Projectile{
$pk->speedY = $this->motionY;
$pk->speedZ = $this->motionZ;
$pk->metadata = $this->dataProperties;
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
parent::spawnTo($player);
}

View File

@ -26,6 +26,7 @@ use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\item\Item as ItemItem;
use pocketmine\math\Vector3;
use pocketmine\network\Network;
use pocketmine\network\protocol\AddEntityPacket;
use pocketmine\network\protocol\EntityEventPacket;
use pocketmine\Player;
@ -65,7 +66,7 @@ class Squid extends WaterAnimal implements Ageable{
$pk = new EntityEventPacket();
$pk->eid = $this->getId();
$pk->event = 15;
Server::broadcastPacket($this->hasSpawned, $pk);
Server::broadcastPacket($this->hasSpawned, $pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
}
@ -154,7 +155,7 @@ class Squid extends WaterAnimal implements Ageable{
$pk->yaw = $this->yaw;
$pk->pitch = $this->pitch;
$pk->metadata = $this->dataProperties;
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
parent::spawnTo($player);
}

View File

@ -23,6 +23,7 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\Int;
use pocketmine\network\Network;
use pocketmine\network\protocol\AddEntityPacket;
use pocketmine\Player;
@ -64,7 +65,7 @@ class Villager extends Creature implements NPC, Ageable{
$pk->yaw = $this->yaw;
$pk->pitch = $this->pitch;
$pk->metadata = $this->dataProperties;
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
parent::spawnTo($player);
}

View File

@ -24,6 +24,7 @@ namespace pocketmine\entity;
use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\item\Item as ItemItem;
use pocketmine\network\Network;
use pocketmine\network\protocol\AddEntityPacket;
use pocketmine\Player;
@ -51,7 +52,7 @@ class Zombie extends Monster{
$pk->yaw = $this->yaw;
$pk->pitch = $this->pitch;
$pk->metadata = $this->dataProperties;
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
parent::spawnTo($player);
}

View File

@ -25,6 +25,7 @@ use pocketmine\entity\Entity;
use pocketmine\event\entity\EntityInventoryChangeEvent;
use pocketmine\event\inventory\InventoryOpenEvent;
use pocketmine\item\Item;
use pocketmine\network\Network;
use pocketmine\network\protocol\ContainerSetContentPacket;
use pocketmine\network\protocol\ContainerSetSlotPacket;
use pocketmine\Player;
@ -412,7 +413,7 @@ abstract class BaseInventory implements Inventory{
continue;
}
$pk->windowid = $id;
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
}
@ -435,7 +436,7 @@ abstract class BaseInventory implements Inventory{
continue;
}
$pk->windowid = $id;
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
}

View File

@ -22,6 +22,7 @@
namespace pocketmine\inventory;
use pocketmine\level\Level;
use pocketmine\network\Network;
use pocketmine\network\protocol\TileEventPacket;
use pocketmine\Player;
use pocketmine\Server;
@ -50,7 +51,7 @@ class ChestInventory extends ContainerInventory{
$pk->case1 = 1;
$pk->case2 = 2;
if(($level = $this->getHolder()->getLevel()) instanceof Level){
Server::broadcastPacket($level->getUsingChunk($this->getHolder()->getX() >> 4, $this->getHolder()->getZ() >> 4), $pk);
Server::broadcastPacket($level->getUsingChunk($this->getHolder()->getX() >> 4, $this->getHolder()->getZ() >> 4), $pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
}
}
@ -64,7 +65,7 @@ class ChestInventory extends ContainerInventory{
$pk->case1 = 1;
$pk->case2 = 0;
if(($level = $this->getHolder()->getLevel()) instanceof Level){
Server::broadcastPacket($level->getUsingChunk($this->getHolder()->getX() >> 4, $this->getHolder()->getZ() >> 4), $pk);
Server::broadcastPacket($level->getUsingChunk($this->getHolder()->getX() >> 4, $this->getHolder()->getZ() >> 4), $pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
}
parent::onClose($who);

View File

@ -22,6 +22,7 @@
namespace pocketmine\inventory;
use pocketmine\math\Vector3;
use pocketmine\network\Network;
use pocketmine\network\protocol\ContainerClosePacket;
use pocketmine\network\protocol\ContainerOpenPacket;
use pocketmine\Player;
@ -42,7 +43,7 @@ abstract class ContainerInventory extends BaseInventory{
$pk->x = $pk->y = $pk->z = 0;
}
$who->dataPacket($pk);
$who->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
$this->sendContents($who);
}
@ -50,7 +51,7 @@ abstract class ContainerInventory extends BaseInventory{
public function onClose(Player $who){
$pk = new ContainerClosePacket();
$pk->windowid = $who->getWindowId($this);
$who->dataPacket($pk);
$who->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
parent::onClose($who);
}
}

View File

@ -23,6 +23,7 @@ namespace pocketmine\inventory;
use pocketmine\item\Item;
use pocketmine\level\Level;
use pocketmine\network\Network;
use pocketmine\network\protocol\TileEventPacket;
use pocketmine\Player;
use pocketmine\Server;
@ -105,7 +106,7 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
$pk->case1 = 1;
$pk->case2 = 2;
if(($level = $this->right->getHolder()->getLevel()) instanceof Level){
Server::broadcastPacket($level->getUsingChunk($this->right->getHolder()->getX() >> 4, $this->right->getHolder()->getZ() >> 4), $pk);
Server::broadcastPacket($level->getUsingChunk($this->right->getHolder()->getX() >> 4, $this->right->getHolder()->getZ() >> 4), $pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
}
}
@ -119,7 +120,7 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
$pk->case1 = 1;
$pk->case2 = 0;
if(($level = $this->right->getHolder()->getLevel()) instanceof Level){
Server::broadcastPacket($level->getUsingChunk($this->right->getHolder()->getX() >> 4, $this->right->getHolder()->getZ() >> 4), $pk);
Server::broadcastPacket($level->getUsingChunk($this->right->getHolder()->getX() >> 4, $this->right->getHolder()->getZ() >> 4), $pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
}
parent::onClose($who);

View File

@ -26,6 +26,7 @@ use pocketmine\event\entity\EntityArmorChangeEvent;
use pocketmine\event\entity\EntityInventoryChangeEvent;
use pocketmine\event\player\PlayerItemHeldEvent;
use pocketmine\item\Item;
use pocketmine\network\Network;
use pocketmine\network\protocol\ContainerSetContentPacket;
use pocketmine\network\protocol\ContainerSetSlotPacket;
use pocketmine\network\protocol\PlayerArmorEquipmentPacket;
@ -73,7 +74,7 @@ class PlayerInventory extends BaseInventory{
$pk->meta = $item->getDamage();
$pk->slot = $this->getHeldItemIndex();
Server::broadcastPacket($this->getHolder()->getViewers(), $pk);
Server::broadcastPacket($this->getHolder()->getViewers(), $pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
}
}
@ -147,7 +148,7 @@ class PlayerInventory extends BaseInventory{
if($player === $this->getHolder()){
$this->sendSlot($this->getHeldItemSlot(), $player);
}else{
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
}
}
}
@ -343,7 +344,7 @@ class PlayerInventory extends BaseInventory{
$pk2->slots = $armor;
$player->dataPacket($pk2);
}else{
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
}
}
}
@ -401,7 +402,7 @@ class PlayerInventory extends BaseInventory{
$pk2->item = $this->getItem($index);
$player->dataPacket($pk2);
}else{
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
}
}
}
@ -415,6 +416,7 @@ class PlayerInventory extends BaseInventory{
}
$pk = new ContainerSetContentPacket();
$pk->setChannel(Network::CHANNEL_WORLD_EVENTS);
$pk->slots = [];
for($i = 0; $i < $this->getSize(); ++$i){ //Do not send armor by error here
$pk->slots[$i] = $this->getItem($i);
@ -447,6 +449,7 @@ class PlayerInventory extends BaseInventory{
}
$pk = new ContainerSetSlotPacket();
$pk->setChannel(Network::CHANNEL_WORLD_EVENTS);
$pk->slot = $index;
$pk->item = clone $this->getItem($index);

View File

@ -36,6 +36,7 @@ use pocketmine\nbt\tag\Compound;
use pocketmine\nbt\tag\Double;
use pocketmine\nbt\tag\Enum;
use pocketmine\nbt\tag\Float;
use pocketmine\network\Network;
use pocketmine\network\protocol\ExplodePacket;
use pocketmine\Server;
use pocketmine\utils\Random;
@ -214,7 +215,7 @@ class Explosion{
$pk->z = $this->source->z;
$pk->radius = $this->size;
$pk->records = $send;
Server::broadcastPacket($this->level->getUsingChunk($source->x >> 4, $source->z >> 4), $pk);
Server::broadcastPacket($this->level->getUsingChunk($source->x >> 4, $source->z >> 4), $pk->setChannel(Network::CHANNEL_BLOCKS));
return true;
}

View File

@ -84,6 +84,7 @@ use pocketmine\nbt\tag\Float;
use pocketmine\nbt\tag\Int;
use pocketmine\nbt\tag\Short;
use pocketmine\nbt\tag\String;
use pocketmine\network\Network;
use pocketmine\network\protocol\SetTimePacket;
use pocketmine\network\protocol\UpdateBlockPacket;
use pocketmine\Player;
@ -393,9 +394,9 @@ class Level implements ChunkManager, Metadatable{
if($pk !== null){
if(!is_array($pk)){
Server::broadcastPacket($players, $pk);
Server::broadcastPacket($players, $pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}else{
$this->server->batchPackets($players, $pk);
$this->server->batchPackets($players, $pk, false, Network::CHANNEL_WORLD_EVENTS);
}
}
}
@ -409,9 +410,9 @@ class Level implements ChunkManager, Metadatable{
if($pk !== null){
if(!is_array($pk)){
Server::broadcastPacket($players, $pk);
Server::broadcastPacket($players, $pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}else{
$this->server->batchPackets($players, $pk);
$this->server->batchPackets($players, $pk, false, Network::CHANNEL_WORLD_EVENTS);
}
}
}
@ -530,7 +531,7 @@ class Level implements ChunkManager, Metadatable{
$pk->time = (int) $this->time;
$pk->started = $this->stopTime == false;
Server::broadcastPacket($this->players, $pk);
Server::broadcastPacket($this->players, $pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
/**
@ -634,7 +635,7 @@ class Level implements ChunkManager, Metadatable{
$pk->records[] = [$b->x, $b->z, $b->y, $fullBlock >> 4, $fullBlock & 0xf, $flags];
}
}
Server::broadcastPacket($target, $pk);
Server::broadcastPacket($target, $pk->setChannel(Network::CHANNEL_BLOCKS));
}
public function clearCache(){

View File

@ -29,6 +29,7 @@ class CompressBatchedTask extends AsyncTask{
public $level = 7;
public $data;
public $final;
public $channel = 0;
public $targets = [];
public function onRun(){
@ -40,6 +41,6 @@ class CompressBatchedTask extends AsyncTask{
}
public function onCompletion(Server $server){
$server->broadcastPacketsCallback($this->final, $this->targets);
$server->broadcastPacketsCallback($this->final, $this->targets, $this->channel);
}
}

View File

@ -80,6 +80,16 @@ class Network{
public static $BATCH_THRESHOLD = 512;
const CHANNEL_NONE = 0;
const CHANNEL_PRIORITY = 1; //Priority channel, only to be used when it matters
const CHANNEL_WORLD_CHUNKS = 2; //Chunk sending
const CHANNEL_MOVEMENT = 3; //Movement sending
const CHANNEL_BLOCKS = 4; //Block updates or explosions
const CHANNEL_WORLD_EVENTS = 5; //Entity, level or tile entity events
const CHANNEL_ENTITY_SPAWNING = 6; //Entity spawn/despawn channel
const CHANNEL_TEXT = 7; //Chat and other text stuff
const CHANNEL_END = 31;
/** @var \SplFixedArray */
private $packetPool;

View File

@ -61,9 +61,6 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
/** @var ServerHandler */
private $interface;
/** @var string[][] */
private $batchedPackets = [];
public function __construct(Server $server){
$this->server = $server;
@ -71,6 +68,10 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
$this->rakLib = new RakLibServer($this->server->getLogger(), $this->server->getLoader(), $this->server->getPort(), $this->server->getIp() === "" ? "0.0.0.0" : $this->server->getIp());
$this->interface = new ServerHandler($this->rakLib, $this);
for($i = 0; $i < 256; ++$i){
$this->channelCounts[$i] = 0;
}
}
public function setNetwork(Network $network){
@ -79,7 +80,6 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
public function doTick(){
if(!$this->rakLib->isTerminated()){
$this->sendBatchedPackets();
$this->interface->sendTick();
}else{
$info = $this->rakLib->getTerminationInfo();
@ -88,15 +88,6 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
}
}
private function sendBatchedPackets(){
foreach($this->batchedPackets as $i => $p){
if($this->batchedPackets[$i] !== ""){
$this->server->batchPackets([$this->players[$i]], [$p]);
$this->batchedPackets[$i] = "";
}
}
}
public function process(){
$work = false;
if($this->interface->handlePacket()){
@ -115,7 +106,6 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
$player = $this->players[$identifier];
$this->identifiers->detach($player);
unset($this->players[$identifier]);
unset($this->batchedPackets[$identifier]);
unset($this->identifiersACK[$identifier]);
if(!$player->closed){
$player->close($player->getLeaveMessage(), $reason);
@ -126,7 +116,6 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
public function close(Player $player, $reason = "unknown reason"){
if(isset($this->identifiers[$player])){
unset($this->players[$this->identifiers[$player]]);
unset($this->batchedPackets[$this->identifiers[$player]]);
unset($this->identifiersACK[$this->identifiers[$player]]);
$this->interface->closeSession($this->identifiers[$player], $reason);
$this->identifiers->detach($player);
@ -149,7 +138,6 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
$player = new $class($this, $ev->getClientId(), $ev->getAddress(), $ev->getPort());
$this->players[$identifier] = $player;
$this->identifiersACK[$identifier] = 0;
$this->batchedPackets[$identifier] = "";
$this->identifiers->attach($player, $identifier);
$this->server->addPlayer($identifier, $player);
}
@ -218,7 +206,13 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
$packet->__encapsulatedPacket = new CachedEncapsulatedPacket;
$packet->__encapsulatedPacket->identifierACK = null;
$packet->__encapsulatedPacket->buffer = $packet->buffer;
$packet->__encapsulatedPacket->reliability = 2;
if($packet->getChannel() !== 0){
$packet->__encapsulatedPacket->reliability = 3;
$packet->__encapsulatedPacket->orderChannel = $packet->getChannel();
$packet->__encapsulatedPacket->orderIndex = 0;
}else{
$packet->__encapsulatedPacket->reliability = 2;
}
}
$pk = $packet->__encapsulatedPacket;
}
@ -226,14 +220,21 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
if(!$immediate and !$needACK and $packet->pid() !== ProtocolInfo::BATCH_PACKET
and Network::$BATCH_THRESHOLD >= 0
and strlen($packet->buffer) >= Network::$BATCH_THRESHOLD){
$this->batchedPackets[$this->identifiers[$player]] .= $packet->buffer;
$this->server->batchPackets([$player], [$packet], false, $packet->getChannel());
return null;
}
if($pk === null){
$pk = new EncapsulatedPacket();
$pk->buffer = $packet->buffer;
$pk->reliability = 2;
if($packet->getChannel() !== 0){
$packet->reliability = 3;
$packet->orderChannel = $packet->getChannel();
$packet->orderIndex = 0;
}else{
$packet->reliability = 2;
}
if($needACK === true){
$pk->identifierACK = $this->identifiersACK[$identifier]++;
}

View File

@ -35,6 +35,7 @@ abstract class DataPacket extends \stdClass{
private $offset = 0;
public $buffer = "";
public $isEncoded = false;
private $channel = 0;
abstract public function pid();
@ -47,6 +48,15 @@ abstract class DataPacket extends \stdClass{
$this->offset = 0;
}
public function setChannel($channel){
$this->channel = (int) $channel;
return $this;
}
public function getChannel(){
return $this->channel;
}
public function setBuffer($buffer = ""){
$this->buffer = $buffer;
$this->offset = 0;

View File

@ -34,6 +34,7 @@ use pocketmine\nbt\tag\Byte;
use pocketmine\nbt\tag\Compound;
use pocketmine\nbt\tag\Enum;
use pocketmine\nbt\tag\Short;
use pocketmine\network\Network;
use pocketmine\network\protocol\ContainerSetDataPacket;
class Furnace extends Tile implements InventoryHolder, Container{
@ -255,13 +256,13 @@ class Furnace extends Tile implements InventoryHolder, Container{
$pk->windowid = $windowId;
$pk->property = 0; //Smelting
$pk->value = floor($this->namedtag["CookTime"]);
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
$pk = new ContainerSetDataPacket();
$pk->windowid = $windowId;
$pk->property = 1; //Fire icon
$pk->value = $this->namedtag["BurnTicks"];
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
}
}

View File

@ -24,6 +24,7 @@ namespace pocketmine\tile;
use pocketmine\level\format\FullChunk;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\Compound;
use pocketmine\network\Network;
use pocketmine\network\protocol\TileEntityDataPacket;
use pocketmine\Player;
@ -41,7 +42,7 @@ abstract class Spawnable extends Tile{
$pk->y = $this->y;
$pk->z = $this->z;
$pk->namedtag = $nbt->write();
$player->dataPacket($pk);
$player->dataPacket($pk->setChannel(Network::CHANNEL_WORLD_EVENTS));
return true;
}

@ -1 +1 @@
Subproject commit db92e6b345b14c3c998cb2754fa2ee9e6b16ec7a
Subproject commit a35e3d1535c327013de94e6d24078dc7573b57ec