mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-07 10:22:56 +00:00
BatchPacket, compress any packet depending on the size, really fast threaded chunk sending
This commit is contained in:
47
src/pocketmine/network/AdvancedSourceInterface.php
Normal file
47
src/pocketmine/network/AdvancedSourceInterface.php
Normal 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);
|
||||
|
||||
}
|
45
src/pocketmine/network/CompressBatchedTask.php
Normal file
45
src/pocketmine/network/CompressBatchedTask.php
Normal 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);
|
||||
}
|
||||
}
|
283
src/pocketmine/network/Network.php
Normal file
283
src/pocketmine/network/Network.php
Normal 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);
|
||||
}
|
||||
}
|
@ -19,9 +19,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Network-related classes
|
||||
*/
|
||||
namespace pocketmine\network;
|
||||
|
||||
use pocketmine\event\player\PlayerCreationEvent;
|
||||
@ -32,6 +29,7 @@ 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;
|
||||
@ -81,7 +79,7 @@ use raklib\server\RakLibServer;
|
||||
use raklib\server\ServerHandler;
|
||||
use raklib\server\ServerInstance;
|
||||
|
||||
class RakLibInterface implements ServerInstance, SourceInterface{
|
||||
class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
|
||||
|
||||
/** @var \SplFixedArray */
|
||||
private $packetPool;
|
||||
@ -89,6 +87,9 @@ class RakLibInterface implements ServerInstance, SourceInterface{
|
||||
/** @var Server */
|
||||
private $server;
|
||||
|
||||
/** @var Network */
|
||||
private $network;
|
||||
|
||||
/** @var RakLibServer */
|
||||
private $rakLib;
|
||||
|
||||
@ -104,31 +105,42 @@ class RakLibInterface implements ServerInstance, SourceInterface{
|
||||
/** @var ServerHandler */
|
||||
private $interface;
|
||||
|
||||
private $upload = 0;
|
||||
private $download = 0;
|
||||
/** @var string[][] */
|
||||
private $batchedPackets = [];
|
||||
|
||||
public function __construct(Server $server){
|
||||
|
||||
$this->registerPackets();
|
||||
|
||||
$this->server = $server;
|
||||
$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->interface = new ServerHandler($this->rakLib, $this);
|
||||
$this->setName($this->server->getMotd());
|
||||
}
|
||||
|
||||
public function setNetwork(Network $network){
|
||||
$this->network = $network;
|
||||
}
|
||||
|
||||
public function doTick(){
|
||||
if(!$this->rakLib->isTerminated()){
|
||||
$this->sendBatchedPackets();
|
||||
$this->interface->sendTick();
|
||||
}else{
|
||||
$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"]);
|
||||
}
|
||||
}
|
||||
|
||||
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()){
|
||||
@ -147,6 +159,7 @@ class RakLibInterface implements ServerInstance, SourceInterface{
|
||||
$player = $this->players[$identifier];
|
||||
$this->identifiers->detach($player);
|
||||
unset($this->players[$identifier]);
|
||||
unset($this->batchedPackets[$identifier]);
|
||||
unset($this->identifiersACK[$identifier]);
|
||||
$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"){
|
||||
if(isset($this->identifiers[$player])){
|
||||
unset($this->players[$this->identifiers[$player]]);
|
||||
unset($this->batchedPackets[$identifier]);
|
||||
unset($this->identifiersACK[$this->identifiers[$player]]);
|
||||
$this->interface->closeSession($this->identifiers[$player], $reason);
|
||||
$this->identifiers->detach($player);
|
||||
@ -177,6 +191,7 @@ class RakLibInterface implements ServerInstance, SourceInterface{
|
||||
$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);
|
||||
}
|
||||
@ -209,7 +224,7 @@ class RakLibInterface implements ServerInstance, SourceInterface{
|
||||
$this->server->handlePacket($address, $port, $payload);
|
||||
}
|
||||
|
||||
public function putRaw($address, $port, $payload){
|
||||
public function sendRawPacket($address, $port, $payload){
|
||||
$this->interface->sendRaw($address, $port, $payload);
|
||||
}
|
||||
|
||||
@ -230,19 +245,10 @@ class RakLibInterface implements ServerInstance, SourceInterface{
|
||||
public function handleOption($name, $value){
|
||||
if($name === "bandwidth"){
|
||||
$v = unserialize($value);
|
||||
$this->upload = $v["up"];
|
||||
$this->download = $v["down"];
|
||||
$this->network->addStatistics($v["up"], $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){
|
||||
if(isset($this->identifiers[$player])){
|
||||
$identifier = $this->identifiers[$player];
|
||||
@ -259,6 +265,13 @@ class RakLibInterface implements ServerInstance, SourceInterface{
|
||||
$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){
|
||||
$pk = new EncapsulatedPacket();
|
||||
$pk->buffer = $packet->buffer;
|
||||
@ -276,76 +289,10 @@ class RakLibInterface implements ServerInstance, SourceInterface{
|
||||
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){
|
||||
$pid = ord($buffer{0});
|
||||
|
||||
if(($data = $this->getPacketFromPool($pid)) === null){
|
||||
if(($data = $this->network->getPacket($pid)) === null){
|
||||
$data = new UnknownPacket();
|
||||
$data->packetID = $pid;
|
||||
}
|
||||
|
48
src/pocketmine/network/protocol/BatchPacket.php
Normal file
48
src/pocketmine/network/protocol/BatchPacket.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
@ -44,6 +44,7 @@ class FullChunkDataPacket extends DataPacket{
|
||||
$this->reset();
|
||||
$this->putInt($this->chunkX);
|
||||
$this->putInt($this->chunkZ);
|
||||
$this->putInt(strlen($this->data));
|
||||
$this->put($this->data);
|
||||
}
|
||||
|
||||
|
@ -91,5 +91,6 @@ interface Info{
|
||||
//const PLAYER_INPUT_PACKET = 0xaf;
|
||||
const FULL_CHUNK_DATA_PACKET = 0xb0;
|
||||
const SET_DIFFICULTY_PACKET = 0xb1;
|
||||
const BATCH_PACKET = 0xb2;
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user