Relieve RakLibServer of more responsibilities

at this point it's really not much more than just a thread-safe way to transmit parameters from main to thread. Maybe we can ditch it in favour of a generic closure-based thread implementation.
This commit is contained in:
Dylan K. Taylor 2020-03-29 18:42:15 +01:00
parent 4e54e54421
commit d89cdfc18e
2 changed files with 32 additions and 36 deletions

View File

@ -43,6 +43,7 @@ use raklib\utils\InternetAddress;
use function addcslashes;
use function bin2hex;
use function implode;
use function mt_rand;
use function random_bytes;
use function rtrim;
use function substr;
@ -64,6 +65,9 @@ class RakLibInterface implements ServerInstance, AdvancedNetworkInterface{
/** @var Network */
private $network;
/** @var int */
private $rakServerId;
/** @var RakLibServer */
private $rakLib;
@ -78,20 +82,27 @@ class RakLibInterface implements ServerInstance, AdvancedNetworkInterface{
public function __construct(Server $server){
$this->server = $server;
$this->rakServerId = mt_rand(0, PHP_INT_MAX);
$this->sleeper = new SleeperNotifier();
$mainToThreadBuffer = new \Threaded;
$threadToMainBuffer = new \Threaded;
$this->rakLib = new RakLibServer(
$this->server->getLogger(),
$mainToThreadBuffer,
$threadToMainBuffer,
new InternetAddress($this->server->getIp(), $this->server->getPort(), 4),
$this->rakServerId,
(int) $this->server->getProperty("network.max-mtu-size", 1492),
self::MCPE_RAKNET_PROTOCOL_VERSION,
$this->sleeper
);
$this->interface = new ServerHandler(
$this,
new InterThreadChannelReader($this->rakLib->getExternalQueue()),
new InterThreadChannelWriter($this->rakLib->getInternalQueue())
new InterThreadChannelReader($threadToMainBuffer),
new InterThreadChannelWriter($mainToThreadBuffer)
);
}
@ -207,7 +218,7 @@ class RakLibInterface implements ServerInstance, AdvancedNetworkInterface{
ProtocolInfo::MINECRAFT_VERSION_NETWORK,
$info->getPlayerCount(),
$info->getMaxPlayerCount(),
$this->rakLib->getServerId(),
$this->rakServerId,
$this->server->getName(),
$this->server->getGamemode()->getEnglishName()
]) . ";"

View File

@ -35,9 +35,7 @@ use raklib\utils\InternetAddress;
use function error_get_last;
use function gc_enable;
use function ini_set;
use function mt_rand;
use function register_shutdown_function;
use const PHP_INT_MAX;
use const PTHREADS_INHERIT_NONE;
class RakLibServer extends Thread{
@ -53,15 +51,15 @@ class RakLibServer extends Thread{
protected $ready = false;
/** @var \Threaded */
protected $externalQueue;
protected $mainToThreadBuffer;
/** @var \Threaded */
protected $internalQueue;
protected $threadToMainBuffer;
/** @var string */
protected $mainPath;
/** @var int */
protected $serverId = 0;
protected $serverId;
/** @var int */
protected $maxMtuSize;
/** @var int */
@ -80,16 +78,25 @@ class RakLibServer extends Thread{
* @param int|null $overrideProtocolVersion Optional custom protocol version to use, defaults to current RakLib's protocol
* @param SleeperNotifier|null $sleeper
*/
public function __construct(\ThreadedLogger $logger, InternetAddress $address, int $maxMtuSize = 1492, ?int $overrideProtocolVersion = null, ?SleeperNotifier $sleeper = null){
public function __construct(
\ThreadedLogger $logger,
\Threaded $mainToThreadBuffer,
\Threaded $threadToMainBuffer,
InternetAddress $address,
int $serverId,
int $maxMtuSize = 1492,
?int $overrideProtocolVersion = null,
?SleeperNotifier $sleeper = null
){
$this->address = $address;
$this->serverId = mt_rand(0, PHP_INT_MAX);
$this->serverId = $serverId;
$this->maxMtuSize = $maxMtuSize;
$this->logger = $logger;
$this->externalQueue = new \Threaded;
$this->internalQueue = new \Threaded;
$this->mainToThreadBuffer = $mainToThreadBuffer;
$this->threadToMainBuffer = $threadToMainBuffer;
$this->mainPath = \pocketmine\PATH;
@ -98,28 +105,6 @@ class RakLibServer extends Thread{
$this->mainThreadNotifier = $sleeper;
}
/**
* Returns the RakNet server ID
* @return int
*/
public function getServerId() : int{
return $this->serverId;
}
/**
* @return \Threaded
*/
public function getExternalQueue() : \Threaded{
return $this->externalQueue;
}
/**
* @return \Threaded
*/
public function getInternalQueue() : \Threaded{
return $this->internalQueue;
}
/**
* @return void
*/
@ -174,8 +159,8 @@ class RakLibServer extends Thread{
$socket,
$this->maxMtuSize,
$this->protocolVersion,
new InterThreadChannelReader($this->internalQueue),
new InterThreadChannelWriter($this->externalQueue, $this->mainThreadNotifier),
new InterThreadChannelReader($this->mainToThreadBuffer),
new InterThreadChannelWriter($this->threadToMainBuffer, $this->mainThreadNotifier),
new ExceptionTraceCleaner($this->mainPath)
);
$this->synchronized(function() : void{