Updated to new RakLib

This commit is contained in:
Shoghi Cervantes 2014-06-05 17:52:42 +02:00
parent c434961dfc
commit fdc2edc421
5 changed files with 121 additions and 19 deletions

View File

@ -146,6 +146,8 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
private $chunkScheduled = 0;
private $inAction = false;
private $needACK = [];
/**
* @var \pocketmine\scheduler\TaskHandler[]
*/
@ -467,6 +469,19 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
return $this->spawnPosition;
}
/**
* @param int $identifier
*
* @return bool
*/
public function checkACK($identifier){
return !isset($this->needACK[$identifier]);
}
public function handleACK($identifier){
unset($this->needACK[$identifier]);
}
/**
* Sends, if available, the next ordered chunk to the client
*
@ -491,15 +506,28 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
}
if(is_array($this->lastChunk)){
foreach($this->getLevel()->getChunkEntities($this->lastChunk[0], $this->lastChunk[1]) as $entity){
if($entity !== $this){
$entity->spawnTo($this);
$identifier = $this->lastChunk[2];
if(!$this->checkACK($identifier)){
if((microtime(true) - $this->lastChunk[3]) < 1.5){
$this->server->getScheduler()->scheduleDelayedTask(new CallbackTask(array($this, "getNextChunk"), array(false, true)), MAX_CHUNK_RATE);
return false;
}else{
$index = null;
LevelFormat::getXZ($index, $this->lastChunk[0], $this->lastChunk[1]);
unset($this->chunksLoaded[$index]);
}
}
foreach($this->getLevel()->getChunkTiles($this->lastChunk[0], $this->lastChunk[1]) as $tile){
if($tile instanceof Spawnable){
$tile->spawnTo($this);
}else{
foreach($this->getLevel()->getChunkEntities($this->lastChunk[0], $this->lastChunk[1]) as $entity){
if($entity !== $this){
$entity->spawnTo($this);
}
}
foreach($this->getLevel()->getChunkTiles($this->lastChunk[0], $this->lastChunk[1]) as $tile){
if($tile instanceof Spawnable){
$tile->spawnTo($this);
}
}
}
$this->lastChunk = false;
}
@ -536,12 +564,12 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->chunkX = $X;
$pk->chunkZ = $Z;
$pk->data = $this->getLevel()->getOrderedChunk($X, $Z, $Yndex);
$cnt = $this->dataPacket($pk);
$cnt = $this->dataPacket($pk, true);
if($cnt === false){
return false;
}
$this->lastChunk = array($X, $Z);
$this->lastChunk = array($X, $Z, $cnt, microtime(true));
if($this->chunkScheduled === 0 or $force === true){
$this->server->getScheduler()->scheduleDelayedTask(new CallbackTask(array($this, "getNextChunk"), array(false, true)), MAX_CHUNK_RATE);
@ -615,10 +643,11 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
* Sends an ordered DataPacket to the send buffer
*
* @param DataPacket $packet
* @param bool $needACK
*
* @return array|bool
* @return int|bool
*/
public function dataPacket(DataPacket $packet){
public function dataPacket(DataPacket $packet, $needACK = false){
if($this->connected === false){
return false;
}
@ -627,7 +656,39 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
return false;
}
$this->interface->putPacket($this, $packet);
$identifier = $this->interface->putPacket($this, $packet, $needACK);
if($needACK and $identifier !== null){
$this->needACK[$identifier] = false;
return $identifier;
}
return true;
}
/**
* @param DataPacket $packet
* @param bool $needACK
*
* @return bool|int
*/
public function directDataPacket(DataPacket $packet, $needACK = false){
if($this->connected === false){
return false;
}
$this->server->getPluginManager()->callEvent($ev = new DataPacketSendEvent($this, $packet));
if($ev->isCancelled()){
return false;
}
$identifier = $this->interface->putPacket($this, $packet, $needACK, true);
if($needACK and $identifier !== null){
$this->needACK[$identifier] = false;
return $identifier;
}
return true;
}
/**

View File

@ -1282,7 +1282,7 @@ class Server{
$this->logger->setLogDebug(\pocketmine\DEBUG > 1);
}
define("ADVANCED_CACHE", $this->getConfigBoolean("enable-advanced-cache", false));
define("MAX_CHUNK_RATE", 20 / $this->getConfigInt("max-chunks-per-second", 3)); //Default rate ~144 kB/s, TODO: increment this, add backwards notification of packets
define("MAX_CHUNK_RATE", 20 / $this->getConfigInt("max-chunks-per-second", 7)); //Default rate ~336 kB/s
if(ADVANCED_CACHE == true){
$this->logger->info("Advanced cache enabled");
}
@ -1571,6 +1571,9 @@ class Server{
$this->tickScheduler->kill();
$this->console->kill();
foreach($this->interfaces as $interface){
$interface->shutdown();
}
}
/**

View File

@ -92,6 +92,9 @@ class RakLibInterface implements ServerInstance, SourceInterface{
/** @var \SplObjectStorage */
private $identifers;
/** @var int[] */
private $identifiersACK = [];
/** @var ServerHandler */
private $interface;
@ -101,6 +104,7 @@ class RakLibInterface implements ServerInstance, SourceInterface{
$server = 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($server, $this);
$this->setName($this->server->getMotd());
}
@ -113,6 +117,7 @@ class RakLibInterface implements ServerInstance, SourceInterface{
$player = $this->players[$identifier];
$this->identifers->detach($player);
unset($this->players[$identifier]);
unset($this->identifiersACK[$identifier]);
$player->close($player->getName() . " has left the game", $reason);
}
}
@ -120,6 +125,7 @@ class RakLibInterface implements ServerInstance, SourceInterface{
public function close(Player $player, $reason = "unknown reason"){
if(isset($this->identifers[$player])){
unset($this->players[$this->identifers[$player]]);
unset($this->identifiersACK[$this->identifers[$player]]);
$this->interface->closeSession($this->identifers[$player], $reason);
$this->identifers->detach($player);
}
@ -136,24 +142,47 @@ class RakLibInterface implements ServerInstance, SourceInterface{
public function openSession($identifier, $address, $port, $clientID){
$player = new Player($this, $clientID, $address, $port);
$this->players[$identifier] = $player;
$this->identifiersACK[$identifier] = 0;
$this->identifers->attach($player, $identifier);
$this->server->addPlayer($identifier, $player);
}
public function handleEncapsulated($identifier, EncapsulatedPacket $packet){
public function handleEncapsulated($identifier, EncapsulatedPacket $packet, $flags){
if(isset($this->players[$identifier])){
$this->players[$identifier]->handleDataPacket($this->getPacket($packet->buffer));
}
}
public function putPacket(Player $player, DataPacket $packet){
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 handleOption($name, $value){
//TODO
}
public function putPacket(Player $player, DataPacket $packet, $needACK = false, $immediate = false){
if(isset($this->identifers[$player])){
$identifier = $this->identifers[$player];
$packet->encode();
$pk = new EncapsulatedPacket();
$pk->buffer = $packet->buffer;
$pk->reliability = 2;
$this->interface->sendEncapsulated($this->identifers[$player], $pk);
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;
}
private function getPacket($buffer){

View File

@ -33,12 +33,16 @@ use pocketmine\Player;
interface SourceInterface{
/**
* Sends a DataPacket to the interface
* Sends a DataPacket to the interface, returns an unique identifier for the packet if $needACK is true
*
* @param Player $player
* @param DataPacket $packet
* @param bool $needACK
* @param bool $immediate
*
* @return int
*/
public function putPacket(Player $player, DataPacket $packet);
public function putPacket(Player $player, DataPacket $packet, $needACK = false, $immediate = true);
/**
* Terminates the connection
@ -49,6 +53,11 @@ interface SourceInterface{
*/
public function close(Player $player, $reason = "unknown reason");
/**
* @param string $name
*/
public function setName($name);
/**
* @return bool
*/

@ -1 +1 @@
Subproject commit 19519aedcff033843de7144c4abb2ec474080fd3
Subproject commit 0a7c9134230d921f9e3b97b2dd3af965eaf61281