registerPackets(); $this->server = $server; $this->identifiers = new \SplObjectStorage(); $this->internalThreaded = new \Threaded(); $this->externalThreaded = new \Threaded(); $this->rakLib = new RakLibServer($this->internalThreaded, $this->externalThreaded, $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 doTick(){ if(!$this->rakLib->isTerminated()){ $this->interface->sendTick(); }else{ $info = $this->rakLib->getTerminationInfo(); $this->server->removeInterface($this); \ExceptionHandler::handler(E_ERROR, "RakLib Thread crashed [".$info["scope"]."]: " . (isset($info["message"]) ? $info["message"] : ""), $info["file"], $info["line"]); } } public function process(){ $work = false; if($this->interface->handlePacket()){ $work = true; while($this->interface->handlePacket()){ } } $this->doTick(); return $work; } public function closeSession($identifier, $reason){ if(isset($this->players[$identifier])){ $player = $this->players[$identifier]; $this->identifiers->detach($player); unset($this->players[$identifier]); unset($this->identifiersACK[$identifier]); $player->close(TextFormat::YELLOW . $player->getName() . " has left the game", $reason); } } public function close(Player $player, $reason = "unknown reason"){ if(isset($this->identifiers[$player])){ unset($this->players[$this->identifiers[$player]]); unset($this->identifiersACK[$this->identifiers[$player]]); $this->interface->closeSession($this->identifiers[$player], $reason); $this->identifiers->detach($player); } } public function shutdown(){ $this->interface->shutdown(); } public function emergencyShutdown(){ $this->interface->emergencyShutdown(); } public function openSession($identifier, $address, $port, $clientID){ $ev = new PlayerCreationEvent($this, Player::class, Player::class, null, $address, $port); $this->server->getPluginManager()->callEvent($ev); $class = $ev->getPlayerClass(); $player = new $class($this, $ev->getClientId(), $ev->getAddress(), $ev->getPort()); $this->players[$identifier] = $player; $this->identifiersACK[$identifier] = 0; $this->identifiers->attach($player, $identifier); $this->server->addPlayer($identifier, $player); } public function handleEncapsulated($identifier, EncapsulatedPacket $packet, $flags){ if(isset($this->players[$identifier])){ try{ $pk = $this->getPacket($packet->buffer); $pk->decode(); $this->players[$identifier]->handleDataPacket($pk); }catch(\Exception $e){ if(\pocketmine\DEBUG > 1){ $logger = $this->server->getLogger(); if($logger instanceof MainLogger){ $logger->debug("Packet " . get_class($pk) . " 0x" . bin2hex($packet->buffer)); $logger->logException($e); } } $this->interface->blockAddress($this->players[$identifier]->getAddress(), 5); } } } public function blockAddress($address, $timeout = 300){ $this->interface->blockAddress($address, $timeout); } public function handleRaw($address, $port, $payload){ $this->server->handlePacket($address, $port, $payload); } public function putRaw($address, $port, $payload){ $this->interface->sendRaw($address, $port, $payload); } public function notifyACK($identifier, $identifierACK){ if(isset($this->players[$identifier])){ $this->players[$identifier]->handleACK($identifierACK); } } public function setName($name){ $this->interface->sendOption("name", "MCCPP;Demo;$name"); } public function setPortCheck($name){ $this->interface->sendOption("portChecking", (bool) $name); } public function handleOption($name, $value){ if($name === "bandwidth"){ $v = unserialize($value); $this->upload = $v["up"]; $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){ if(isset($this->identifiers[$player])){ $identifier = $this->identifiers[$player]; $pk = null; if(!$packet->isEncoded){ $packet->encode(); }elseif(!$needACK){ if(!isset($packet->__encapsulatedPacket)){ $packet->__encapsulatedPacket = new CachedEncapsulatedPacket; $packet->__encapsulatedPacket->identifierACK = null; $packet->__encapsulatedPacket->buffer = $packet->buffer; $packet->__encapsulatedPacket->reliability = 2; } $pk = $packet->__encapsulatedPacket; } if($pk === null){ $pk = new EncapsulatedPacket(); $pk->buffer = $packet->buffer; $pk->reliability = 2; if($needACK === true){ $pk->identifierACK = $this->identifiersACK[$identifier]++; } } $this->interface->sendEncapsulated($identifier, $pk, ($needACK === true ? RakLib::FLAG_NEED_ACK : 0) | ($immediate === true ? RakLib::PRIORITY_IMMEDIATE : RakLib::PRIORITY_NORMAL)); return $pk->identifierACK; } 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::DISCONNECT_PACKET, DisconnectPacket::class); $this->registerPacket(ProtocolInfo::LOGIN_PACKET, LoginPacket::class); $this->registerPacket(ProtocolInfo::LOGIN_STATUS_PACKET, LoginStatusPacket::class); $this->registerPacket(ProtocolInfo::MESSAGE_PACKET, MessagePacket::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::ROTATE_HEAD_PACKET, RotateHeadPacket::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::SEND_INVENTORY_PACKET, SendInventoryPacket::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::CHAT_PACKET, ChatPacket::class); $this->registerPacket(ProtocolInfo::ADVENTURE_SETTINGS_PACKET, AdventureSettingsPacket::class); $this->registerPacket(ProtocolInfo::ENTITY_DATA_PACKET, EntityDataPacket::class); $this->registerPacket(ProtocolInfo::SET_DIFFICULTY_PACKET, SetDifficultyPacket::class); } private function getPacket($buffer){ $pid = ord($buffer{0}); if(($data = $this->getPacketFromPool($pid)) === null){ $data = new UnknownPacket(); $data->packetID = $pid; } $data->setBuffer(substr($buffer, 1)); return $data; } }