Server: Implemented an signal/sleep interrupt mechanism for ticking (#2171)

This allows other threads to notify the main thread to wake it up while it's sleeping between ticks, allowing reduction of processing latency.

Currently only RakLib and the CommandReader threads utilize this, but it's planned to extend it to more things in the near future.

CommandReader is now event-driven instead of poll-based - the server will not poll the CommandReader thread for messages each tick anymore.

RakLib utilizes this mechanism to get packets processed without delays to lower latency.

This now adds an extra dependency - `pocketmine/snooze` library contains the meat of the code used for this. See the Snooze repository for details.
This commit is contained in:
Dylan K. Taylor
2018-05-09 14:18:13 +01:00
committed by GitHub
parent e70af362d0
commit 2a0a2134d1
6 changed files with 108 additions and 34 deletions

View File

@ -32,6 +32,7 @@ use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\network\Network;
use pocketmine\Player;
use pocketmine\Server;
use pocketmine\snooze\SleeperNotifier;
use raklib\protocol\EncapsulatedPacket;
use raklib\protocol\PacketReliability;
use raklib\RakLib;
@ -68,15 +69,24 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
/** @var ServerHandler */
private $interface;
/** @var SleeperNotifier */
private $sleeper;
public function __construct(Server $server){
$this->server = $server;
$this->sleeper = new SleeperNotifier();
$server->getTickSleeper()->addNotifier($this->sleeper, function() : void{
$this->server->getNetwork()->processInterface($this);
});
$this->rakLib = new RakLibServer(
$this->server->getLogger(),
\pocketmine\COMPOSER_AUTOLOADER_PATH,
new InternetAddress($this->server->getIp() === "" ? "0.0.0.0" : $this->server->getIp(), $this->server->getPort(), 4),
(int) $this->server->getProperty("network.max-mtu-size", 1492),
self::MCPE_RAKNET_PROTOCOL_VERSION
self::MCPE_RAKNET_PROTOCOL_VERSION,
$this->sleeper
);
$this->interface = new ServerHandler($this->rakLib, $this);
}
@ -117,10 +127,12 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
}
public function shutdown(){
$this->server->getTickSleeper()->removeNotifier($this->sleeper);
$this->interface->shutdown();
}
public function emergencyShutdown(){
$this->server->getTickSleeper()->removeNotifier($this->sleeper);
$this->interface->emergencyShutdown();
}