mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-14 17:59:41 +00:00
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:
parent
4e54e54421
commit
d89cdfc18e
@ -43,6 +43,7 @@ use raklib\utils\InternetAddress;
|
|||||||
use function addcslashes;
|
use function addcslashes;
|
||||||
use function bin2hex;
|
use function bin2hex;
|
||||||
use function implode;
|
use function implode;
|
||||||
|
use function mt_rand;
|
||||||
use function random_bytes;
|
use function random_bytes;
|
||||||
use function rtrim;
|
use function rtrim;
|
||||||
use function substr;
|
use function substr;
|
||||||
@ -64,6 +65,9 @@ class RakLibInterface implements ServerInstance, AdvancedNetworkInterface{
|
|||||||
/** @var Network */
|
/** @var Network */
|
||||||
private $network;
|
private $network;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
private $rakServerId;
|
||||||
|
|
||||||
/** @var RakLibServer */
|
/** @var RakLibServer */
|
||||||
private $rakLib;
|
private $rakLib;
|
||||||
|
|
||||||
@ -78,20 +82,27 @@ class RakLibInterface implements ServerInstance, AdvancedNetworkInterface{
|
|||||||
|
|
||||||
public function __construct(Server $server){
|
public function __construct(Server $server){
|
||||||
$this->server = $server;
|
$this->server = $server;
|
||||||
|
$this->rakServerId = mt_rand(0, PHP_INT_MAX);
|
||||||
|
|
||||||
$this->sleeper = new SleeperNotifier();
|
$this->sleeper = new SleeperNotifier();
|
||||||
|
|
||||||
|
$mainToThreadBuffer = new \Threaded;
|
||||||
|
$threadToMainBuffer = new \Threaded;
|
||||||
|
|
||||||
$this->rakLib = new RakLibServer(
|
$this->rakLib = new RakLibServer(
|
||||||
$this->server->getLogger(),
|
$this->server->getLogger(),
|
||||||
|
$mainToThreadBuffer,
|
||||||
|
$threadToMainBuffer,
|
||||||
new InternetAddress($this->server->getIp(), $this->server->getPort(), 4),
|
new InternetAddress($this->server->getIp(), $this->server->getPort(), 4),
|
||||||
|
$this->rakServerId,
|
||||||
(int) $this->server->getProperty("network.max-mtu-size", 1492),
|
(int) $this->server->getProperty("network.max-mtu-size", 1492),
|
||||||
self::MCPE_RAKNET_PROTOCOL_VERSION,
|
self::MCPE_RAKNET_PROTOCOL_VERSION,
|
||||||
$this->sleeper
|
$this->sleeper
|
||||||
);
|
);
|
||||||
$this->interface = new ServerHandler(
|
$this->interface = new ServerHandler(
|
||||||
$this,
|
$this,
|
||||||
new InterThreadChannelReader($this->rakLib->getExternalQueue()),
|
new InterThreadChannelReader($threadToMainBuffer),
|
||||||
new InterThreadChannelWriter($this->rakLib->getInternalQueue())
|
new InterThreadChannelWriter($mainToThreadBuffer)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,7 +218,7 @@ class RakLibInterface implements ServerInstance, AdvancedNetworkInterface{
|
|||||||
ProtocolInfo::MINECRAFT_VERSION_NETWORK,
|
ProtocolInfo::MINECRAFT_VERSION_NETWORK,
|
||||||
$info->getPlayerCount(),
|
$info->getPlayerCount(),
|
||||||
$info->getMaxPlayerCount(),
|
$info->getMaxPlayerCount(),
|
||||||
$this->rakLib->getServerId(),
|
$this->rakServerId,
|
||||||
$this->server->getName(),
|
$this->server->getName(),
|
||||||
$this->server->getGamemode()->getEnglishName()
|
$this->server->getGamemode()->getEnglishName()
|
||||||
]) . ";"
|
]) . ";"
|
||||||
|
@ -35,9 +35,7 @@ use raklib\utils\InternetAddress;
|
|||||||
use function error_get_last;
|
use function error_get_last;
|
||||||
use function gc_enable;
|
use function gc_enable;
|
||||||
use function ini_set;
|
use function ini_set;
|
||||||
use function mt_rand;
|
|
||||||
use function register_shutdown_function;
|
use function register_shutdown_function;
|
||||||
use const PHP_INT_MAX;
|
|
||||||
use const PTHREADS_INHERIT_NONE;
|
use const PTHREADS_INHERIT_NONE;
|
||||||
|
|
||||||
class RakLibServer extends Thread{
|
class RakLibServer extends Thread{
|
||||||
@ -53,15 +51,15 @@ class RakLibServer extends Thread{
|
|||||||
protected $ready = false;
|
protected $ready = false;
|
||||||
|
|
||||||
/** @var \Threaded */
|
/** @var \Threaded */
|
||||||
protected $externalQueue;
|
protected $mainToThreadBuffer;
|
||||||
/** @var \Threaded */
|
/** @var \Threaded */
|
||||||
protected $internalQueue;
|
protected $threadToMainBuffer;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $mainPath;
|
protected $mainPath;
|
||||||
|
|
||||||
/** @var int */
|
/** @var int */
|
||||||
protected $serverId = 0;
|
protected $serverId;
|
||||||
/** @var int */
|
/** @var int */
|
||||||
protected $maxMtuSize;
|
protected $maxMtuSize;
|
||||||
/** @var int */
|
/** @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 int|null $overrideProtocolVersion Optional custom protocol version to use, defaults to current RakLib's protocol
|
||||||
* @param SleeperNotifier|null $sleeper
|
* @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->address = $address;
|
||||||
|
|
||||||
$this->serverId = mt_rand(0, PHP_INT_MAX);
|
$this->serverId = $serverId;
|
||||||
$this->maxMtuSize = $maxMtuSize;
|
$this->maxMtuSize = $maxMtuSize;
|
||||||
|
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
|
||||||
$this->externalQueue = new \Threaded;
|
$this->mainToThreadBuffer = $mainToThreadBuffer;
|
||||||
$this->internalQueue = new \Threaded;
|
$this->threadToMainBuffer = $threadToMainBuffer;
|
||||||
|
|
||||||
$this->mainPath = \pocketmine\PATH;
|
$this->mainPath = \pocketmine\PATH;
|
||||||
|
|
||||||
@ -98,28 +105,6 @@ class RakLibServer extends Thread{
|
|||||||
$this->mainThreadNotifier = $sleeper;
|
$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
|
* @return void
|
||||||
*/
|
*/
|
||||||
@ -174,8 +159,8 @@ class RakLibServer extends Thread{
|
|||||||
$socket,
|
$socket,
|
||||||
$this->maxMtuSize,
|
$this->maxMtuSize,
|
||||||
$this->protocolVersion,
|
$this->protocolVersion,
|
||||||
new InterThreadChannelReader($this->internalQueue),
|
new InterThreadChannelReader($this->mainToThreadBuffer),
|
||||||
new InterThreadChannelWriter($this->externalQueue, $this->mainThreadNotifier),
|
new InterThreadChannelWriter($this->threadToMainBuffer, $this->mainThreadNotifier),
|
||||||
new ExceptionTraceCleaner($this->mainPath)
|
new ExceptionTraceCleaner($this->mainPath)
|
||||||
);
|
);
|
||||||
$this->synchronized(function() : void{
|
$this->synchronized(function() : void{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user