BatchPacket, compress any packet depending on the size, really fast threaded chunk sending

This commit is contained in:
Shoghi Cervantes 2015-03-25 23:50:55 +01:00
parent 7d1313c63d
commit 962c28aaca
No known key found for this signature in database
GPG Key ID: 78464DB0A7837F89
16 changed files with 658 additions and 233 deletions

View File

@ -89,6 +89,7 @@ use pocketmine\nbt\tag\Int;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
use pocketmine\network\protocol\AdventureSettingsPacket; use pocketmine\network\protocol\AdventureSettingsPacket;
use pocketmine\network\protocol\AnimatePacket; use pocketmine\network\protocol\AnimatePacket;
use pocketmine\network\protocol\BatchPacket;
use pocketmine\network\protocol\DataPacket; use pocketmine\network\protocol\DataPacket;
use pocketmine\network\protocol\DisconnectPacket; use pocketmine\network\protocol\DisconnectPacket;
use pocketmine\network\protocol\EntityEventPacket; use pocketmine\network\protocol\EntityEventPacket;
@ -180,7 +181,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
public $usedChunks = []; public $usedChunks = [];
protected $loadQueue = []; protected $loadQueue = [];
protected $chunkACK = [];
protected $nextChunkOrderRun = 5; protected $nextChunkOrderRun = 5;
/** @var Player[] */ /** @var Player[] */
@ -557,49 +557,24 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
} }
} }
/**
* @param int $identifier
*
* @return bool
*/
public function checkACK($identifier){
return !isset($this->needACK[$identifier]);
}
public function handleACK($identifier){
unset($this->needACK[$identifier]);
if(isset($this->chunkACK[$identifier])){
$index = $this->chunkACK[$identifier];
unset($this->chunkACK[$identifier]);
if(isset($this->usedChunks[$index])){
$this->usedChunks[$index] = true;
$X = null;
$Z = null;
Level::getXZ($index, $X, $Z);
foreach($this->level->getChunkEntities($X, $Z) as $entity){
if($entity !== $this and !$entity->closed and !$entity->dead){
$entity->spawnTo($this);
}
}
}
}
}
public function sendChunk($x, $z, $payload){ public function sendChunk($x, $z, $payload){
if($this->connected === false){ if($this->connected === false){
return; return;
} }
$this->usedChunks[Level::chunkHash($x, $z)] = true;
$pk = new FullChunkDataPacket(); $pk = new FullChunkDataPacket();
$pk->chunkX = $x; $pk->chunkX = $x;
$pk->chunkZ = $z; $pk->chunkZ = $z;
$pk->data = $payload; $pk->data = $payload;
$cnt = $this->dataPacket($pk, true); $this->dataPacket($pk);
if($cnt === false or $cnt === true){
return; foreach($this->level->getChunkEntities($x, $z) as $entity){
if($entity !== $this and !$entity->closed and !$entity->dead){
$entity->spawnTo($this);
}
} }
$this->chunkACK[$cnt] = Level::chunkHash($x, $z);
} }
protected function sendNextChunk(){ protected function sendNextChunk(){
@ -1391,6 +1366,12 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
return; return;
} }
if($packet->pid() === ProtocolInfo::BATCH_PACKET){
/** @var BatchPacket $packet */
$this->server->getNetwork()->processBatch($packet, $this);
return;
}
$this->server->getPluginManager()->callEvent($ev = new DataPacketReceiveEvent($this, $packet)); $this->server->getPluginManager()->callEvent($ev = new DataPacketReceiveEvent($this, $packet));
if($ev->isCancelled()){ if($ev->isCancelled()){
return; return;
@ -1408,10 +1389,8 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->iusername = strtolower($this->username); $this->iusername = strtolower($this->username);
$this->loginData = ["clientId" => $packet->clientId, "loginData" => $packet->loginData]; $this->loginData = ["clientId" => $packet->clientId, "loginData" => $packet->loginData];
if(count($this->server->getOnlinePlayers()) > $this->server->getMaxPlayers()){ if(count($this->server->getOnlinePlayers()) > $this->server->getMaxPlayers() and $this->kick("server full")){
if($this->kick("server full") === true){ return;
return;
}
} }
if($packet->protocol1 !== ProtocolInfo::CURRENT_PROTOCOL){ if($packet->protocol1 !== ProtocolInfo::CURRENT_PROTOCOL){
if($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL){ if($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL){

View File

@ -75,6 +75,9 @@ use pocketmine\nbt\tag\Int;
use pocketmine\nbt\tag\Long; use pocketmine\nbt\tag\Long;
use pocketmine\nbt\tag\Short; use pocketmine\nbt\tag\Short;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
use pocketmine\network\CompressBatchedTask;
use pocketmine\network\Network;
use pocketmine\network\protocol\BatchPacket;
use pocketmine\network\protocol\DataPacket; use pocketmine\network\protocol\DataPacket;
use pocketmine\network\query\QueryHandler; use pocketmine\network\query\QueryHandler;
use pocketmine\network\RakLibInterface; use pocketmine\network\RakLibInterface;
@ -191,10 +194,11 @@ class Server{
/** @var LevelMetadataStore */ /** @var LevelMetadataStore */
private $levelMetadata; private $levelMetadata;
/** @var SourceInterface[] */ /** @var Network */
private $interfaces = []; private $network;
/** @var RakLibInterface */
private $mainInterface; private $networkCompressionAsync = true;
private $networkCompressionLevel = 7;
private $serverID; private $serverID;
@ -217,6 +221,8 @@ class Server{
/** @var Player[] */ /** @var Player[] */
private $players = []; private $players = [];
private $identifiers = [];
/** @var Level[] */ /** @var Level[] */
private $levels = []; private $levels = [];
@ -595,67 +601,54 @@ class Server{
return round((array_sum($this->useAverage) / count($this->useAverage)) * 100, 2); return round((array_sum($this->useAverage) / count($this->useAverage)) * 100, 2);
} }
/** /**
* @deprecated
*
* @param $address
* @param int $timeout
*/
public function blockAddress($address, $timeout = 300){
$this->network->blockAddress($address, $timeout);
}
/**
* @deprecated
*
* @param $address
* @param $port
* @param $payload
*/
public function sendPacket($address, $port, $payload){
$this->network->sendPacket($address, $port, $payload);
}
/**
* @deprecated
*
* @return SourceInterface[] * @return SourceInterface[]
*/ */
public function getInterfaces(){ public function getInterfaces(){
return $this->interfaces; return $this->network->getInterfaces();
} }
/** /**
* @deprecated
*
* @param SourceInterface $interface * @param SourceInterface $interface
*/ */
public function addInterface(SourceInterface $interface){ public function addInterface(SourceInterface $interface){
$this->interfaces[spl_object_hash($interface)] = $interface; $this->network->registerInterface($interface);
} }
/** /**
* @deprecated
*
* @param SourceInterface $interface * @param SourceInterface $interface
*/ */
public function removeInterface(SourceInterface $interface){ public function removeInterface(SourceInterface $interface){
$interface->shutdown(); $interface->shutdown();
unset($this->interfaces[spl_object_hash($interface)]); $this->network->unregisterInterface($interface);
}
/**
* @param string $address
* @param int $port
* @param string $payload
*/
public function sendPacket($address, $port, $payload){
$this->mainInterface->putRaw($address, $port, $payload);
}
/**
* Blocks an IP address from the main interface. Setting timeout to -1 will block it forever
*
* @param string $address
* @param int $timeout
*/
public function blockAddress($address, $timeout = 300){
$this->mainInterface->blockAddress($address, $timeout);
}
/**
* @param string $address
* @param int $port
* @param string $payload
*/
public function handlePacket($address, $port, $payload){
try{
if(strlen($payload) > 2 and substr($payload, 0, 2) === "\xfe\xfd" and $this->queryHandler instanceof QueryHandler){
$this->queryHandler->handle($address, $port, $payload);
}
}catch(\Exception $e){
if(\pocketmine\DEBUG > 1){
if($this->logger instanceof MainLogger){
$this->logger->logException($e);
}
}
$this->blockAddress($address, 600);
}
//TODO: add raw packet events
} }
/** /**
@ -893,6 +886,7 @@ class Server{
foreach($this->players as $identifier => $p){ foreach($this->players as $identifier => $p){
if($player === $p){ if($player === $p){
unset($this->players[$identifier]); unset($this->players[$identifier]);
unset($this->identifiers[spl_object_hash($player)]);
break; break;
} }
} }
@ -1529,6 +1523,14 @@ class Server{
ServerScheduler::$WORKERS = $this->getProperty("settings.async-workers", ServerScheduler::$WORKERS); ServerScheduler::$WORKERS = $this->getProperty("settings.async-workers", ServerScheduler::$WORKERS);
if($this->getProperty("network.batch-threshold", 256) >= 0){
Network::$BATCH_THRESHOLD = (int) $this->getProperty("network.batch-threshold", 256);
}else{
Network::$BATCH_THRESHOLD = -1;
}
$this->networkCompressionLevel = $this->getProperty("network.compression-level", 7);
$this->networkCompressionAsync = $this->getProperty("network.async-compression", true);
$this->scheduler = new ServerScheduler(); $this->scheduler = new ServerScheduler();
if($this->getConfigBoolean("enable-rcon", false) === true){ if($this->getConfigBoolean("enable-rcon", false) === true){
@ -1588,7 +1590,10 @@ class Server{
define("BOOTUP_RANDOM", @Utils::getRandomBytes(16)); define("BOOTUP_RANDOM", @Utils::getRandomBytes(16));
$this->serverID = Binary::readLong(substr(Utils::getUniqueID(true, $this->getIp() . $this->getPort()), 0, 8)); $this->serverID = Binary::readLong(substr(Utils::getUniqueID(true, $this->getIp() . $this->getPort()), 0, 8));
$this->addInterface($this->mainInterface = new RakLibInterface($this)); $this->network = new Network($this);
$this->network->setName($this->getMotd());
$this->network->registerInterface(new RakLibInterface($this));
$this->logger->info("This server is running " . $this->getName() . " version " . ($version->isDev() ? TextFormat::YELLOW : "") . $version->get(true) . TextFormat::WHITE . " \"" . $this->getCodename() . "\" (API " . $this->getApiVersion() . ")"); $this->logger->info("This server is running " . $this->getName() . " version " . ($version->isDev() ? TextFormat::YELLOW : "") . $version->get(true) . TextFormat::WHITE . " \"" . $this->getCodename() . "\" (API " . $this->getApiVersion() . ")");
$this->logger->info($this->getName() . " is distributed under the LGPL License"); $this->logger->info($this->getName() . " is distributed under the LGPL License");
@ -1751,6 +1756,11 @@ class Server{
public static function broadcastPacket(array $players, DataPacket $packet){ public static function broadcastPacket(array $players, DataPacket $packet){
$packet->encode(); $packet->encode();
$packet->isEncoded = true; $packet->isEncoded = true;
if(Network::$BATCH_THRESHOLD >= 0 and strlen($packet->buffer) >= Network::$BATCH_THRESHOLD){
Server::getInstance()->batchPackets($players, [$packet->buffer]);
return;
}
foreach($players as $player){ foreach($players as $player){
$player->dataPacket($packet); $player->dataPacket($packet);
} }
@ -1759,6 +1769,53 @@ class Server{
} }
} }
/**
* Broadcasts a list of packets in a batch to a list of players
*
* @param Player[] $players
* @param DataPacket[]|string $packets
*/
public function batchPackets(array $players, array $packets){
$str = "";
foreach($packets as $p){
if(is_object($p)){
$p->encode();
$str .= $p->buffer;
}else{
$str .= $p;
}
}
$targets = [];
foreach($players as $p){
$targets[] = $this->identifiers[spl_object_hash($p)];
}
if($this->networkCompressionAsync){
$task = new CompressBatchedTask();
$task->targets = $targets;
$task->data = $str;
$task->level = $this->networkCompressionLevel;
$this->getScheduler()->scheduleAsyncTask($task);
}else{
$this->broadcastPacketsCallback(zlib_encode($str, ZLIB_ENCODING_DEFLATE, $this->networkCompressionLevel), $targets);
}
}
public function broadcastPacketsCallback($data, array $identifiers){
$pk = new BatchPacket();
$pk->payload = $data;
$pk->encode();
$pk->isEncoded = true;
foreach($identifiers as $i){
if(isset($this->players[$i])){
$this->players[$i]->dataPacket($pk);
}
}
}
/** /**
* @param int $type * @param int $type
@ -1902,7 +1959,7 @@ class Server{
$this->rcon->stop(); $this->rcon->stop();
} }
if($this->getProperty("settings.upnp-forwarding", false) === true){ if($this->getProperty("network.upnp-forwarding", false) === true){
$this->logger->info("[UPnP] Removing port forward..."); $this->logger->info("[UPnP] Removing port forward...");
UPnP::RemovePortForward($this->getPort()); UPnP::RemovePortForward($this->getPort());
} }
@ -1930,8 +1987,9 @@ class Server{
$this->console->kill(); $this->console->kill();
foreach($this->interfaces as $interface){ foreach($this->network->getInterfaces() as $interface){
$interface->shutdown(); $interface->shutdown();
$this->network->unregisterInterface($interface);
} }
}catch(\Exception $e){ }catch(\Exception $e){
$this->logger->emergency("Crashed while crashing, killing process"); $this->logger->emergency("Crashed while crashing, killing process");
@ -1960,7 +2018,7 @@ class Server{
} }
if($this->getProperty("settings.upnp-forwarding", false) == true){ if($this->getProperty("network.upnp-forwarding", false) == true){
$this->logger->info("[UPnP] Trying to port forward..."); $this->logger->info("[UPnP] Trying to port forward...");
UPnP::PortForward($this->getPort()); UPnP::PortForward($this->getPort());
} }
@ -2112,6 +2170,7 @@ class Server{
public function addPlayer($identifier, Player $player){ public function addPlayer($identifier, Player $player){
$this->players[$identifier] = $player; $this->players[$identifier] = $player;
$this->identifiers[spl_object_hash($player)] = $identifier;
} }
private function checkTickUpdates($currentTick){ private function checkTickUpdates($currentTick){
@ -2137,6 +2196,7 @@ class Server{
$player->save(); $player->save();
}elseif(!$player->isConnected()){ }elseif(!$player->isConnected()){
unset($this->players[$index]); unset($this->players[$index]);
unset($this->identifiers[spl_object_hash($player)]);
} }
} }
@ -2178,7 +2238,7 @@ class Server{
"version" => $version->get(true), "version" => $version->get(true),
"build" => $version->getBuild(), "build" => $version->getBuild(),
"mc_version" => \pocketmine\MINECRAFT_VERSION, "mc_version" => \pocketmine\MINECRAFT_VERSION,
"protocol" => network\protocol\Info::CURRENT_PROTOCOL, "protocol" => \pocketmine\network\protocol\Info::CURRENT_PROTOCOL,
"online" => count($this->players), "online" => count($this->players),
"max" => $this->getMaxPlayers(), "max" => $this->getMaxPlayers(),
"plugins" => $plist, "plugins" => $plist,
@ -2188,7 +2248,7 @@ class Server{
} }
public function getNetwork(){ public function getNetwork(){
return $this->mainInterface; return $this->network;
} }
private function titleTick(){ private function titleTick(){
@ -2203,10 +2263,12 @@ class Server{
$this->getPocketMineVersion() . $this->getPocketMineVersion() .
" | Online " . count($this->players) . "/" . $this->getMaxPlayers() . " | Online " . count($this->players) . "/" . $this->getMaxPlayers() .
" | Memory " . $usage . " | Memory " . $usage .
" | U " . round($this->mainInterface->getUploadUsage() / 1024, 2) . " | U " . round($this->network->getUpload() / 1024, 2) .
" D " . round($this->mainInterface->getDownloadUsage() / 1024, 2) . " D " . round($this->network->getDownload() / 1024, 2) .
" kB/s | TPS " . $this->getTicksPerSecond() . " kB/s | TPS " . $this->getTicksPerSecond() .
" | Load " . $this->getTickUsage() . "%\x07"; " | Load " . $this->getTickUsage() . "%\x07";
$this->network->resetStatistics();
} }
public function getMemoryUsage($advanced = false){ public function getMemoryUsage($advanced = false){
@ -2250,6 +2312,31 @@ class Server{
} }
/**
* @param string $address
* @param int $port
* @param string $payload
*
* TODO: move this to Network
*/
public function handlePacket($address, $port, $payload){
try{
if(strlen($payload) > 2 and substr($payload, 0, 2) === "\xfe\xfd" and $this->queryHandler instanceof QueryHandler){
$this->queryHandler->handle($address, $port, $payload);
}
}catch(\Exception $e){
if(\pocketmine\DEBUG > 1){
if($this->logger instanceof MainLogger){
$this->logger->logException($e);
}
}
$this->blockAddress($address, 600);
}
//TODO: add raw packet events
}
/** /**
* Tries to execute a server tick * Tries to execute a server tick
*/ */
@ -2266,9 +2353,7 @@ class Server{
$this->checkConsole(); $this->checkConsole();
Timings::$connectionTimer->startTiming(); Timings::$connectionTimer->startTiming();
foreach($this->interfaces as $interface){ $this->network->processInterfaces();
$interface->process();
}
Timings::$connectionTimer->stopTiming(); Timings::$connectionTimer->stopTiming();
Timings::$schedulerTimer->startTiming(); Timings::$schedulerTimer->startTiming();

View File

@ -23,6 +23,7 @@ namespace pocketmine\block;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\level\sound\DoorSound;
use pocketmine\math\AxisAlignedBB; use pocketmine\math\AxisAlignedBB;
use pocketmine\network\protocol\LevelEventPacket; use pocketmine\network\protocol\LevelEventPacket;
use pocketmine\Player; use pocketmine\Player;
@ -275,14 +276,8 @@ abstract class Door extends Transparent{
if($player instanceof Player){ if($player instanceof Player){
unset($players[$player->getId()]); unset($players[$player->getId()]);
} }
$pk = new LevelEventPacket();
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->evid = 1003;
$pk->data = 0;
Server::broadcastPacket($players, $pk);
$this->level->addSound(new DoorSound($this));
return true; return true;
} }
@ -294,13 +289,7 @@ abstract class Door extends Transparent{
if($player instanceof Player){ if($player instanceof Player){
unset($players[$player->getId()]); unset($players[$player->getId()]);
} }
$pk = new LevelEventPacket(); $this->level->addSound(new DoorSound($this));
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->evid = 1003;
$pk->data = 0;
Server::broadcastPacket($players, $pk);
} }
return true; return true;

View File

@ -380,9 +380,7 @@ class Level implements ChunkManager, Metadatable{
if(!is_array($pk)){ if(!is_array($pk)){
Server::broadcastPacket($players, $pk); Server::broadcastPacket($players, $pk);
}else{ }else{
foreach($pk as $p){ $this->server->batchPackets($players, $pk);
Server::broadcastPacket($players, $p);
}
} }
} }
} }
@ -398,9 +396,7 @@ class Level implements ChunkManager, Metadatable{
if(!is_array($pk)){ if(!is_array($pk)){
Server::broadcastPacket($players, $pk); Server::broadcastPacket($players, $pk);
}else{ }else{
foreach($pk as $p){ $this->server->batchPackets($players, $pk);
Server::broadcastPacket($players, $p);
}
} }
} }
} }

View File

@ -34,7 +34,6 @@ class ChunkRequestTask extends AsyncTask{
protected $levelId; protected $levelId;
protected $chunkX; protected $chunkX;
protected $chunkZ; protected $chunkZ;
protected $compressionLevel;
/** @var \pocketmine\level\format\ChunkSection[] */ /** @var \pocketmine\level\format\ChunkSection[] */
protected $sections; protected $sections;
@ -69,8 +68,6 @@ class ChunkRequestTask extends AsyncTask{
$this->tiles = $tiles; $this->tiles = $tiles;
$this->compressionLevel = Level::$COMPRESSION_LEVEL;
} }
public function onRun(){ public function onRun(){
@ -102,9 +99,9 @@ class ChunkRequestTask extends AsyncTask{
$biomeColors = pack("N*", ...$this->biomeColors); $biomeColors = pack("N*", ...$this->biomeColors);
$ordered = zlib_encode($orderedIds . $orderedData . $orderedSkyLight . $orderedLight . $this->biomeIds . $biomeColors . $this->tiles, ZLIB_ENCODING_DEFLATE, $this->compressionLevel); $ordered = $orderedIds . $orderedData . $orderedSkyLight . $orderedLight . $this->biomeIds . $biomeColors . $this->tiles;
$this->setResult($ordered); $this->setResult($ordered, false);
} }
public function getColumn(&$data, $x, $z){ public function getColumn(&$data, $x, $z){

View File

@ -148,15 +148,13 @@ class LevelDB extends BaseLevelProvider{
$biomeColors = pack("N*", ...$chunk->getBiomeColorArray()); $biomeColors = pack("N*", ...$chunk->getBiomeColorArray());
$ordered = zlib_encode( $ordered = $chunk->getBlockIdArray() .
$chunk->getBlockIdArray() .
$chunk->getBlockDataArray() . $chunk->getBlockDataArray() .
$chunk->getBlockSkyLightArray() . $chunk->getBlockSkyLightArray() .
$chunk->getBlockLightArray() . $chunk->getBlockLightArray() .
$chunk->getBiomeIdArray() . $chunk->getBiomeIdArray() .
$biomeColors . $biomeColors .
$tiles $tiles;
, ZLIB_ENCODING_DEFLATE, Level::$COMPRESSION_LEVEL);
$this->getLevel()->chunkRequestCallback($x, $z, $ordered); $this->getLevel()->chunkRequestCallback($x, $z, $ordered);

View File

@ -129,15 +129,13 @@ class McRegion extends BaseLevelProvider{
$biomeColors = pack("N*", ...$chunk->getBiomeColorArray()); $biomeColors = pack("N*", ...$chunk->getBiomeColorArray());
$ordered = zlib_encode( $ordered = $chunk->getBlockIdArray() .
$chunk->getBlockIdArray() .
$chunk->getBlockDataArray() . $chunk->getBlockDataArray() .
$chunk->getBlockSkyLightArray() . $chunk->getBlockSkyLightArray() .
$chunk->getBlockLightArray() . $chunk->getBlockLightArray() .
$chunk->getBiomeIdArray() . $chunk->getBiomeIdArray() .
$biomeColors . $biomeColors .
$tiles $tiles;
, ZLIB_ENCODING_DEFLATE, Level::$COMPRESSION_LEVEL);
$this->getLevel()->chunkRequestCallback($x, $z, $ordered); $this->getLevel()->chunkRequestCallback($x, $z, $ordered);

View File

@ -0,0 +1,47 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
/**
* Network-related classes
*/
namespace pocketmine\network;
interface AdvancedSourceInterface extends SourceInterface{
/**
* @param string $address
* @param int $timeout Seconds
*/
public function blockAddress($address, $timeout = 300);
/**
* @param Network $network
*/
public function setNetwork(Network $network);
/**
* @param string $address
* @param int $port
* @param string $payload
*/
public function sendRawPacket($address, $port, $payload);
}

View File

@ -0,0 +1,45 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\network;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
class CompressBatchedTask extends AsyncTask{
public $level = 7;
public $data;
public $final;
public $targets = [];
public function onRun(){
try{
$this->final = zlib_encode($this->data, ZLIB_ENCODING_DEFLATE, $this->level);
}catch(\Exception $e){
}
}
public function onCompletion(Server $server){
$server->broadcastPacketsCallback($this->final, $this->targets);
}
}

View File

@ -0,0 +1,283 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
/**
* Network-related classes
*/
namespace pocketmine\network;
use pocketmine\network\protocol\AddEntityPacket;
use pocketmine\network\protocol\AddItemEntityPacket;
use pocketmine\network\protocol\AddMobPacket;
use pocketmine\network\protocol\AddPaintingPacket;
use pocketmine\network\protocol\AddPlayerPacket;
use pocketmine\network\protocol\AdventureSettingsPacket;
use pocketmine\network\protocol\AnimatePacket;
use pocketmine\network\protocol\BatchPacket;
use pocketmine\network\protocol\ContainerClosePacket;
use pocketmine\network\protocol\ContainerOpenPacket;
use pocketmine\network\protocol\ContainerSetContentPacket;
use pocketmine\network\protocol\ContainerSetDataPacket;
use pocketmine\network\protocol\ContainerSetSlotPacket;
use pocketmine\network\protocol\DataPacket;
use pocketmine\network\protocol\DropItemPacket;
use pocketmine\network\protocol\TileEntityDataPacket;
use pocketmine\network\protocol\EntityEventPacket;
use pocketmine\network\protocol\ExplodePacket;
use pocketmine\network\protocol\HurtArmorPacket;
use pocketmine\network\protocol\Info as ProtocolInfo;
use pocketmine\network\protocol\InteractPacket;
use pocketmine\network\protocol\LevelEventPacket;
use pocketmine\network\protocol\DisconnectPacket;
use pocketmine\network\protocol\LoginPacket;
use pocketmine\network\protocol\PlayStatusPacket;
use pocketmine\network\protocol\TextPacket;
use pocketmine\network\protocol\MoveEntityPacket;
use pocketmine\network\protocol\MovePlayerPacket;
use pocketmine\network\protocol\PlayerActionPacket;
use pocketmine\network\protocol\PlayerArmorEquipmentPacket;
use pocketmine\network\protocol\PlayerEquipmentPacket;
use pocketmine\network\protocol\RemoveBlockPacket;
use pocketmine\network\protocol\RemoveEntityPacket;
use pocketmine\network\protocol\RemovePlayerPacket;
use pocketmine\network\protocol\RespawnPacket;
use pocketmine\network\protocol\SetDifficultyPacket;
use pocketmine\network\protocol\SetEntityDataPacket;
use pocketmine\network\protocol\SetEntityMotionPacket;
use pocketmine\network\protocol\SetHealthPacket;
use pocketmine\network\protocol\SetSpawnPositionPacket;
use pocketmine\network\protocol\SetTimePacket;
use pocketmine\network\protocol\StartGamePacket;
use pocketmine\network\protocol\TakeItemEntityPacket;
use pocketmine\network\protocol\TileEventPacket;
use pocketmine\network\protocol\UpdateBlockPacket;
use pocketmine\network\protocol\UseItemPacket;
use pocketmine\Player;
use pocketmine\Server;
class Network{
public static $BATCH_THRESHOLD = 512;
/** @var \SplFixedArray */
private $packetPool;
/** @var Server */
private $server;
/** @var SourceInterface[] */
private $interfaces = [];
/** @var AdvancedSourceInterface[] */
private $advancedInterfaces = [];
private $upload = 0;
private $download = 0;
private $name;
public function __construct(Server $server){
$this->registerPackets();
$this->server = $server;
}
public function addStatistics($upload, $download){
$this->upload += $upload;
$this->download += $download;
}
public function getUpload(){
return $this->upload;
}
public function getDownload(){
return $this->download;
}
public function resetStatistics(){
$this->upload = 0;
$this->download = 0;
}
/**
* @return SourceInterface[]
*/
public function getInterfaces(){
return $this->interfaces;
}
public function processInterfaces(){
foreach($this->interfaces as $interface){
$interface->process();
}
}
/**
* @param SourceInterface $interface
*/
public function registerInterface(SourceInterface $interface){
$this->interfaces[$hash = spl_object_hash($interface)] = $interface;
if($interface instanceof AdvancedSourceInterface){
$this->advancedInterfaces[$hash] = $interface;
$interface->setNetwork($this);
}
$interface->setName($this->name);
}
/**
* @param SourceInterface $interface
*/
public function unregisterInterface(SourceInterface $interface){
unset($this->interfaces[$hash = spl_object_hash($interface)],
$this->advancedInterfaces[$hash]);
}
/**
* Sets the server name shown on each interface Query
*
* @param string $name
*/
public function setName($name){
$this->name = (string) $name;
foreach($this->interfaces as $interface){
$interface->setName($this->name);
}
}
public function getName(){
return $this->name;
}
/**
* @param int $id 0-255
* @param DataPacket $class
*/
public function registerPacket($id, $class){
$this->packetPool[$id] = new $class;
}
public function getServer(){
return $this->server;
}
public function processBatch(BatchPacket $packet, Player $p){
$str = zlib_decode($packet->payload, 1024 * 1024 * 64); //Max 64MB
$len = strlen($str);
$offset = 0;
while($offset < $len){
if(($packetId = $this->getPacket(ord($str{$offset++}))) !== null){
$packetId->buffer = $str;
$packet->decode();
$p->handleDataPacket($packet);
}
}
}
/**
* @param $id
*
* @return DataPacket
*/
public function getPacket($id){
/** @var DataPacket $class */
$class = $this->packetPool[$id];
if($class !== null){
return clone $class;
}
return null;
}
/**
* @param string $address
* @param int $port
* @param string $payload
*/
public function sendPacket($address, $port, $payload){
foreach($this->advancedInterfaces as $interface){
$interface->sendRawPacket($address, $port, $payload);
}
}
/**
* Blocks an IP address from the main interface. Setting timeout to -1 will block it forever
*
* @param string $address
* @param int $timeout
*/
public function blockAddress($address, $timeout = 300){
foreach($this->advancedInterfaces as $interface){
$interface->blockAddress($address, $timeout);
}
}
private function registerPackets(){
$this->packetPool = new \SplFixedArray(256);
$this->registerPacket(ProtocolInfo::LOGIN_PACKET, LoginPacket::class);
$this->registerPacket(ProtocolInfo::PLAY_STATUS_PACKET, PlayStatusPacket::class);
$this->registerPacket(ProtocolInfo::DISCONNECT_PACKET, DisconnectPacket::class);
$this->registerPacket(ProtocolInfo::TEXT_PACKET, TextPacket::class);
$this->registerPacket(ProtocolInfo::SET_TIME_PACKET, SetTimePacket::class);
$this->registerPacket(ProtocolInfo::START_GAME_PACKET, StartGamePacket::class);
$this->registerPacket(ProtocolInfo::ADD_MOB_PACKET, AddMobPacket::class);
$this->registerPacket(ProtocolInfo::ADD_PLAYER_PACKET, AddPlayerPacket::class);
$this->registerPacket(ProtocolInfo::REMOVE_PLAYER_PACKET, RemovePlayerPacket::class);
$this->registerPacket(ProtocolInfo::ADD_ENTITY_PACKET, AddEntityPacket::class);
$this->registerPacket(ProtocolInfo::REMOVE_ENTITY_PACKET, RemoveEntityPacket::class);
$this->registerPacket(ProtocolInfo::ADD_ITEM_ENTITY_PACKET, AddItemEntityPacket::class);
$this->registerPacket(ProtocolInfo::TAKE_ITEM_ENTITY_PACKET, TakeItemEntityPacket::class);
$this->registerPacket(ProtocolInfo::MOVE_ENTITY_PACKET, MoveEntityPacket::class);
$this->registerPacket(ProtocolInfo::MOVE_PLAYER_PACKET, MovePlayerPacket::class);
$this->registerPacket(ProtocolInfo::REMOVE_BLOCK_PACKET, RemoveBlockPacket::class);
$this->registerPacket(ProtocolInfo::UPDATE_BLOCK_PACKET, UpdateBlockPacket::class);
$this->registerPacket(ProtocolInfo::ADD_PAINTING_PACKET, AddPaintingPacket::class);
$this->registerPacket(ProtocolInfo::EXPLODE_PACKET, ExplodePacket::class);
$this->registerPacket(ProtocolInfo::LEVEL_EVENT_PACKET, LevelEventPacket::class);
$this->registerPacket(ProtocolInfo::TILE_EVENT_PACKET, TileEventPacket::class);
$this->registerPacket(ProtocolInfo::ENTITY_EVENT_PACKET, EntityEventPacket::class);
$this->registerPacket(ProtocolInfo::PLAYER_EQUIPMENT_PACKET, PlayerEquipmentPacket::class);
$this->registerPacket(ProtocolInfo::PLAYER_ARMOR_EQUIPMENT_PACKET, PlayerArmorEquipmentPacket::class);
$this->registerPacket(ProtocolInfo::INTERACT_PACKET, InteractPacket::class);
$this->registerPacket(ProtocolInfo::USE_ITEM_PACKET, UseItemPacket::class);
$this->registerPacket(ProtocolInfo::PLAYER_ACTION_PACKET, PlayerActionPacket::class);
$this->registerPacket(ProtocolInfo::HURT_ARMOR_PACKET, HurtArmorPacket::class);
$this->registerPacket(ProtocolInfo::SET_ENTITY_DATA_PACKET, SetEntityDataPacket::class);
$this->registerPacket(ProtocolInfo::SET_ENTITY_MOTION_PACKET, SetEntityMotionPacket::class);
$this->registerPacket(ProtocolInfo::SET_HEALTH_PACKET, SetHealthPacket::class);
$this->registerPacket(ProtocolInfo::SET_SPAWN_POSITION_PACKET, SetSpawnPositionPacket::class);
$this->registerPacket(ProtocolInfo::ANIMATE_PACKET, AnimatePacket::class);
$this->registerPacket(ProtocolInfo::RESPAWN_PACKET, RespawnPacket::class);
$this->registerPacket(ProtocolInfo::DROP_ITEM_PACKET, DropItemPacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_OPEN_PACKET, ContainerOpenPacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_CLOSE_PACKET, ContainerClosePacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_SET_SLOT_PACKET, ContainerSetSlotPacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_SET_DATA_PACKET, ContainerSetDataPacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_SET_CONTENT_PACKET, ContainerSetContentPacket::class);
$this->registerPacket(ProtocolInfo::ADVENTURE_SETTINGS_PACKET, AdventureSettingsPacket::class);
$this->registerPacket(ProtocolInfo::TILE_ENTITY_DATA_PACKET, TileEntityDataPacket::class);
$this->registerPacket(ProtocolInfo::SET_DIFFICULTY_PACKET, SetDifficultyPacket::class);
$this->registerPacket(ProtocolInfo::BATCH_PACKET, BatchPacket::class);
}
}

View File

@ -19,9 +19,6 @@
* *
*/ */
/**
* Network-related classes
*/
namespace pocketmine\network; namespace pocketmine\network;
use pocketmine\event\player\PlayerCreationEvent; use pocketmine\event\player\PlayerCreationEvent;
@ -32,6 +29,7 @@ use pocketmine\network\protocol\AddPaintingPacket;
use pocketmine\network\protocol\AddPlayerPacket; use pocketmine\network\protocol\AddPlayerPacket;
use pocketmine\network\protocol\AdventureSettingsPacket; use pocketmine\network\protocol\AdventureSettingsPacket;
use pocketmine\network\protocol\AnimatePacket; use pocketmine\network\protocol\AnimatePacket;
use pocketmine\network\protocol\BatchPacket;
use pocketmine\network\protocol\ContainerClosePacket; use pocketmine\network\protocol\ContainerClosePacket;
use pocketmine\network\protocol\ContainerOpenPacket; use pocketmine\network\protocol\ContainerOpenPacket;
use pocketmine\network\protocol\ContainerSetContentPacket; use pocketmine\network\protocol\ContainerSetContentPacket;
@ -81,7 +79,7 @@ use raklib\server\RakLibServer;
use raklib\server\ServerHandler; use raklib\server\ServerHandler;
use raklib\server\ServerInstance; use raklib\server\ServerInstance;
class RakLibInterface implements ServerInstance, SourceInterface{ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
/** @var \SplFixedArray */ /** @var \SplFixedArray */
private $packetPool; private $packetPool;
@ -89,6 +87,9 @@ class RakLibInterface implements ServerInstance, SourceInterface{
/** @var Server */ /** @var Server */
private $server; private $server;
/** @var Network */
private $network;
/** @var RakLibServer */ /** @var RakLibServer */
private $rakLib; private $rakLib;
@ -104,31 +105,42 @@ class RakLibInterface implements ServerInstance, SourceInterface{
/** @var ServerHandler */ /** @var ServerHandler */
private $interface; private $interface;
private $upload = 0; /** @var string[][] */
private $download = 0; private $batchedPackets = [];
public function __construct(Server $server){ public function __construct(Server $server){
$this->registerPackets();
$this->server = $server; $this->server = $server;
$this->identifiers = new \SplObjectStorage(); $this->identifiers = new \SplObjectStorage();
$this->rakLib = new RakLibServer($this->server->getLogger(), $this->server->getLoader(), $this->server->getPort(), $this->server->getIp() === "" ? "0.0.0.0" : $this->server->getIp()); $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); $this->interface = new ServerHandler($this->rakLib, $this);
$this->setName($this->server->getMotd()); }
public function setNetwork(Network $network){
$this->network = $network;
} }
public function doTick(){ public function doTick(){
if(!$this->rakLib->isTerminated()){ if(!$this->rakLib->isTerminated()){
$this->sendBatchedPackets();
$this->interface->sendTick(); $this->interface->sendTick();
}else{ }else{
$info = $this->rakLib->getTerminationInfo(); $info = $this->rakLib->getTerminationInfo();
$this->server->removeInterface($this); $this->network->unregisterInterface($this);
\ExceptionHandler::handler(E_ERROR, "RakLib Thread crashed [".$info["scope"]."]: " . (isset($info["message"]) ? $info["message"] : ""), $info["file"], $info["line"]); \ExceptionHandler::handler(E_ERROR, "RakLib Thread crashed [".$info["scope"]."]: " . (isset($info["message"]) ? $info["message"] : ""), $info["file"], $info["line"]);
} }
} }
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(){ public function process(){
$work = false; $work = false;
if($this->interface->handlePacket()){ if($this->interface->handlePacket()){
@ -147,6 +159,7 @@ class RakLibInterface implements ServerInstance, SourceInterface{
$player = $this->players[$identifier]; $player = $this->players[$identifier];
$this->identifiers->detach($player); $this->identifiers->detach($player);
unset($this->players[$identifier]); unset($this->players[$identifier]);
unset($this->batchedPackets[$identifier]);
unset($this->identifiersACK[$identifier]); unset($this->identifiersACK[$identifier]);
$player->close(TextFormat::YELLOW . $player->getName() . " has left the game", $reason); $player->close(TextFormat::YELLOW . $player->getName() . " has left the game", $reason);
} }
@ -155,6 +168,7 @@ class RakLibInterface implements ServerInstance, SourceInterface{
public function close(Player $player, $reason = "unknown reason"){ public function close(Player $player, $reason = "unknown reason"){
if(isset($this->identifiers[$player])){ if(isset($this->identifiers[$player])){
unset($this->players[$this->identifiers[$player]]); unset($this->players[$this->identifiers[$player]]);
unset($this->batchedPackets[$identifier]);
unset($this->identifiersACK[$this->identifiers[$player]]); unset($this->identifiersACK[$this->identifiers[$player]]);
$this->interface->closeSession($this->identifiers[$player], $reason); $this->interface->closeSession($this->identifiers[$player], $reason);
$this->identifiers->detach($player); $this->identifiers->detach($player);
@ -177,6 +191,7 @@ class RakLibInterface implements ServerInstance, SourceInterface{
$player = new $class($this, $ev->getClientId(), $ev->getAddress(), $ev->getPort()); $player = new $class($this, $ev->getClientId(), $ev->getAddress(), $ev->getPort());
$this->players[$identifier] = $player; $this->players[$identifier] = $player;
$this->identifiersACK[$identifier] = 0; $this->identifiersACK[$identifier] = 0;
$this->batchedPackets[$identifier] = "";
$this->identifiers->attach($player, $identifier); $this->identifiers->attach($player, $identifier);
$this->server->addPlayer($identifier, $player); $this->server->addPlayer($identifier, $player);
} }
@ -209,7 +224,7 @@ class RakLibInterface implements ServerInstance, SourceInterface{
$this->server->handlePacket($address, $port, $payload); $this->server->handlePacket($address, $port, $payload);
} }
public function putRaw($address, $port, $payload){ public function sendRawPacket($address, $port, $payload){
$this->interface->sendRaw($address, $port, $payload); $this->interface->sendRaw($address, $port, $payload);
} }
@ -230,19 +245,10 @@ class RakLibInterface implements ServerInstance, SourceInterface{
public function handleOption($name, $value){ public function handleOption($name, $value){
if($name === "bandwidth"){ if($name === "bandwidth"){
$v = unserialize($value); $v = unserialize($value);
$this->upload = $v["up"]; $this->network->addStatistics($v["up"], $v["down"]);
$this->download = $v["down"];
} }
} }
public function getUploadUsage(){
return $this->upload;
}
public function getDownloadUsage(){
return $this->download;
}
public function putPacket(Player $player, DataPacket $packet, $needACK = false, $immediate = false){ public function putPacket(Player $player, DataPacket $packet, $needACK = false, $immediate = false){
if(isset($this->identifiers[$player])){ if(isset($this->identifiers[$player])){
$identifier = $this->identifiers[$player]; $identifier = $this->identifiers[$player];
@ -259,6 +265,13 @@ class RakLibInterface implements ServerInstance, SourceInterface{
$pk = $packet->__encapsulatedPacket; $pk = $packet->__encapsulatedPacket;
} }
if(!$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;
return;
}
if($pk === null){ if($pk === null){
$pk = new EncapsulatedPacket(); $pk = new EncapsulatedPacket();
$pk->buffer = $packet->buffer; $pk->buffer = $packet->buffer;
@ -276,76 +289,10 @@ class RakLibInterface implements ServerInstance, SourceInterface{
return null; return null;
} }
public function registerPacket($id, $class){
$this->packetPool[$id] = $class;
}
/**
* @param $id
*
* @return DataPacket
*/
public function getPacketFromPool($id){
/** @var DataPacket $class */
$class = $this->packetPool[$id];
if($class !== null){
return new $class;
}
return null;
}
private function registerPackets(){
$this->packetPool = new \SplFixedArray(256);
$this->registerPacket(ProtocolInfo::LOGIN_PACKET, LoginPacket::class);
$this->registerPacket(ProtocolInfo::PLAY_STATUS_PACKET, PlayStatusPacket::class);
$this->registerPacket(ProtocolInfo::DISCONNECT_PACKET, DisconnectPacket::class);
$this->registerPacket(ProtocolInfo::TEXT_PACKET, TextPacket::class);
$this->registerPacket(ProtocolInfo::SET_TIME_PACKET, SetTimePacket::class);
$this->registerPacket(ProtocolInfo::START_GAME_PACKET, StartGamePacket::class);
$this->registerPacket(ProtocolInfo::ADD_MOB_PACKET, AddMobPacket::class);
$this->registerPacket(ProtocolInfo::ADD_PLAYER_PACKET, AddPlayerPacket::class);
$this->registerPacket(ProtocolInfo::REMOVE_PLAYER_PACKET, RemovePlayerPacket::class);
$this->registerPacket(ProtocolInfo::ADD_ENTITY_PACKET, AddEntityPacket::class);
$this->registerPacket(ProtocolInfo::REMOVE_ENTITY_PACKET, RemoveEntityPacket::class);
$this->registerPacket(ProtocolInfo::ADD_ITEM_ENTITY_PACKET, AddItemEntityPacket::class);
$this->registerPacket(ProtocolInfo::TAKE_ITEM_ENTITY_PACKET, TakeItemEntityPacket::class);
$this->registerPacket(ProtocolInfo::MOVE_ENTITY_PACKET, MoveEntityPacket::class);
$this->registerPacket(ProtocolInfo::MOVE_PLAYER_PACKET, MovePlayerPacket::class);
$this->registerPacket(ProtocolInfo::REMOVE_BLOCK_PACKET, RemoveBlockPacket::class);
$this->registerPacket(ProtocolInfo::UPDATE_BLOCK_PACKET, UpdateBlockPacket::class);
$this->registerPacket(ProtocolInfo::ADD_PAINTING_PACKET, AddPaintingPacket::class);
$this->registerPacket(ProtocolInfo::EXPLODE_PACKET, ExplodePacket::class);
$this->registerPacket(ProtocolInfo::LEVEL_EVENT_PACKET, LevelEventPacket::class);
$this->registerPacket(ProtocolInfo::TILE_EVENT_PACKET, TileEventPacket::class);
$this->registerPacket(ProtocolInfo::ENTITY_EVENT_PACKET, EntityEventPacket::class);
$this->registerPacket(ProtocolInfo::PLAYER_EQUIPMENT_PACKET, PlayerEquipmentPacket::class);
$this->registerPacket(ProtocolInfo::PLAYER_ARMOR_EQUIPMENT_PACKET, PlayerArmorEquipmentPacket::class);
$this->registerPacket(ProtocolInfo::INTERACT_PACKET, InteractPacket::class);
$this->registerPacket(ProtocolInfo::USE_ITEM_PACKET, UseItemPacket::class);
$this->registerPacket(ProtocolInfo::PLAYER_ACTION_PACKET, PlayerActionPacket::class);
$this->registerPacket(ProtocolInfo::HURT_ARMOR_PACKET, HurtArmorPacket::class);
$this->registerPacket(ProtocolInfo::SET_ENTITY_DATA_PACKET, SetEntityDataPacket::class);
$this->registerPacket(ProtocolInfo::SET_ENTITY_MOTION_PACKET, SetEntityMotionPacket::class);
$this->registerPacket(ProtocolInfo::SET_HEALTH_PACKET, SetHealthPacket::class);
$this->registerPacket(ProtocolInfo::SET_SPAWN_POSITION_PACKET, SetSpawnPositionPacket::class);
$this->registerPacket(ProtocolInfo::ANIMATE_PACKET, AnimatePacket::class);
$this->registerPacket(ProtocolInfo::RESPAWN_PACKET, RespawnPacket::class);
$this->registerPacket(ProtocolInfo::DROP_ITEM_PACKET, DropItemPacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_OPEN_PACKET, ContainerOpenPacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_CLOSE_PACKET, ContainerClosePacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_SET_SLOT_PACKET, ContainerSetSlotPacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_SET_DATA_PACKET, ContainerSetDataPacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_SET_CONTENT_PACKET, ContainerSetContentPacket::class);
$this->registerPacket(ProtocolInfo::ADVENTURE_SETTINGS_PACKET, AdventureSettingsPacket::class);
$this->registerPacket(ProtocolInfo::TILE_ENTITY_DATA_PACKET, TileEntityDataPacket::class);
$this->registerPacket(ProtocolInfo::SET_DIFFICULTY_PACKET, SetDifficultyPacket::class);
}
private function getPacket($buffer){ private function getPacket($buffer){
$pid = ord($buffer{0}); $pid = ord($buffer{0});
if(($data = $this->getPacketFromPool($pid)) === null){ if(($data = $this->network->getPacket($pid)) === null){
$data = new UnknownPacket(); $data = new UnknownPacket();
$data->packetID = $pid; $data->packetID = $pid;
} }

View File

@ -0,0 +1,48 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\network\protocol;
#include <rules/DataPacket.h>
class BatchPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $payload;
public function pid(){
return Info::BATCH_PACKET;
}
public function decode(){
$size = $this->getInt();
$this->payload = $this->get($size);
}
public function encode(){
$this->reset();
$this->putInt(strlen($this->payload));
$this->put($this->payload);
}
}

View File

@ -44,6 +44,7 @@ class FullChunkDataPacket extends DataPacket{
$this->reset(); $this->reset();
$this->putInt($this->chunkX); $this->putInt($this->chunkX);
$this->putInt($this->chunkZ); $this->putInt($this->chunkZ);
$this->putInt(strlen($this->data));
$this->put($this->data); $this->put($this->data);
} }

View File

@ -91,5 +91,6 @@ interface Info{
//const PLAYER_INPUT_PACKET = 0xaf; //const PLAYER_INPUT_PACKET = 0xaf;
const FULL_CHUNK_DATA_PACKET = 0xb0; const FULL_CHUNK_DATA_PACKET = 0xb0;
const SET_DIFFICULTY_PACKET = 0xb1; const SET_DIFFICULTY_PACKET = 0xb1;
const BATCH_PACKET = 0xb2;
} }

View File

@ -1,6 +1,7 @@
# Main configuration file for PocketMine-MP # Main configuration file for PocketMine-MP
# These settings are the ones that cannot be included in server.properties # These settings are the ones that cannot be included in server.properties
# Some of these settings are safe, others can break your server if modified incorrectly # Some of these settings are safe, others can break your server if modified incorrectly
# New settings/defaults won't appear automatically on this file when upgrading.
settings: settings:
shutdown-message: "Server closed" shutdown-message: "Server closed"
@ -10,13 +11,22 @@ settings:
deprecated-verbose: true deprecated-verbose: true
#Enable plugin and core profiling by default #Enable plugin and core profiling by default
enable-profiling: false enable-profiling: false
advanced-cache: false
upnp-forwarding: false
#Sends anonymous statistics to create usage reports #Sends anonymous statistics to create usage reports
send-usage: true send-usage: true
#Number of AsyncTask workers #Number of AsyncTask workers.
#WARNING: This will increase global memory usage, but it won't be listed in the total. #Used for plugin asynchronous tasks, compression and web communication.
async-workers: 1 async-workers: 2
network:
#Threshold for batching packets, in bytes. Only these packets will be compressed
#Set to 0 to compress everything, -1 to disable.
batch-threshold: 256
#Compression level used when sending batched packets. Higher = more CPU, less bandwidth usage
compression-level: 7
#Use AsyncTasks for compression. Adds half/one tick delay, less CPU load on main thread
async-compression: true
#Experimental, only for Windows. Tries to use UPnP to automatically port forward
upnp-forwarding: false
debug: debug:
#If > 1, it will show debug messages in the console #If > 1, it will show debug messages in the console
@ -33,17 +43,15 @@ level-settings:
chunk-sending: chunk-sending:
#Amount of chunks sent to players per tick #Amount of chunks sent to players per tick
per-tick: 4 per-tick: 8
#Compression level used when sending chunks. Higher = more CPU, less bandwidth usage
compression-level: 7
#Amount of chunks sent around each player #Amount of chunks sent around each player
max-chunks: 256 max-chunks: 256
chunk-ticking: chunk-ticking:
#Max amount of chunks processed each tick #Max amount of chunks processed each tick
per-tick: 80 per-tick: 24
#Radius of chunks around a player to tick #Radius of chunks around a player to tick
tick-radius: 4 tick-radius: 3
#NOTE: This is currently not implemented #NOTE: This is currently not implemented
light-updates: false light-updates: false
clear-tick-list: false clear-tick-list: false
@ -54,7 +62,7 @@ chunk-generation:
#Using this with fast generators is recommended #Using this with fast generators is recommended
use-async: true use-async: true
#Max. amount of chunks to generate per tick, only for use-async: false #Max. amount of chunks to generate per tick, only for use-async: false
per-tick: 1 per-tick: 3
#Max. amount of chunks to populate per tick #Max. amount of chunks to populate per tick
populations-per-tick: 1 populations-per-tick: 1
@ -78,7 +86,7 @@ auto-report:
enabled: true enabled: true
send-code: true send-code: true
send-settings: true send-settings: true
send-phpinfo: true send-phpinfo: false
host: crash.pocketmine.net host: crash.pocketmine.net
auto-updater: auto-updater:

View File

@ -31,6 +31,7 @@ use pocketmine\Server;
abstract class AsyncTask extends \Collectable{ abstract class AsyncTask extends \Collectable{
private $result = null; private $result = null;
private $serialized = false;
/** @var int */ /** @var int */
private $taskId = null; private $taskId = null;
@ -55,7 +56,7 @@ abstract class AsyncTask extends \Collectable{
* @return mixed * @return mixed
*/ */
public function getResult(){ public function getResult(){
return unserialize($this->result); return $this->serialized ? unserialize($this->result) : $this->result;
} }
/** /**
@ -67,9 +68,11 @@ abstract class AsyncTask extends \Collectable{
/** /**
* @param mixed $result * @param mixed $result
* @param bool $serialize
*/ */
public function setResult($result){ public function setResult($result, $serialize = true){
$this->result = serialize($result); $this->result = $serialize ? serialize($result) : $result;
$this->serialized = $serialize;
} }
public function setTaskId($taskId){ public function setTaskId($taskId){