Updated RakLib dependency

This commit is contained in:
Dylan K. Taylor 2020-03-29 18:18:39 +01:00
parent f779881b6a
commit 4e54e54421
4 changed files with 206 additions and 8 deletions

8
composer.lock generated
View File

@ -529,12 +529,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/pmmp/RakLib.git", "url": "https://github.com/pmmp/RakLib.git",
"reference": "76e8d9f15c5ecc0d08d39968023380a2086f0d31" "reference": "b049d56a3ef5c87e545882737f8e1a8e965fec1d"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/pmmp/RakLib/zipball/76e8d9f15c5ecc0d08d39968023380a2086f0d31", "url": "https://api.github.com/repos/pmmp/RakLib/zipball/b049d56a3ef5c87e545882737f8e1a8e965fec1d",
"reference": "76e8d9f15c5ecc0d08d39968023380a2086f0d31", "reference": "b049d56a3ef5c87e545882737f8e1a8e965fec1d",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -563,7 +563,7 @@
"GPL-3.0" "GPL-3.0"
], ],
"description": "A RakNet server implementation written in PHP", "description": "A RakNet server implementation written in PHP",
"time": "2020-03-29T10:00:16+00:00" "time": "2020-03-29T17:01:59+00:00"
}, },
{ {
"name": "pocketmine/snooze", "name": "pocketmine/snooze",

View File

@ -35,7 +35,8 @@ use pocketmine\utils\Utils;
use raklib\protocol\EncapsulatedPacket; use raklib\protocol\EncapsulatedPacket;
use raklib\protocol\PacketReliability; use raklib\protocol\PacketReliability;
use raklib\RakLib; use raklib\RakLib;
use raklib\server\RakLibServer; use raklib\server\InterThreadChannelReader;
use raklib\server\InterThreadChannelWriter;
use raklib\server\ServerHandler; use raklib\server\ServerHandler;
use raklib\server\ServerInstance; use raklib\server\ServerInstance;
use raklib\utils\InternetAddress; use raklib\utils\InternetAddress;
@ -82,13 +83,16 @@ class RakLibInterface implements ServerInstance, AdvancedNetworkInterface{
$this->rakLib = new RakLibServer( $this->rakLib = new RakLibServer(
$this->server->getLogger(), $this->server->getLogger(),
\pocketmine\COMPOSER_AUTOLOADER_PATH,
new InternetAddress($this->server->getIp(), $this->server->getPort(), 4), new InternetAddress($this->server->getIp(), $this->server->getPort(), 4),
(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->rakLib, $this); $this->interface = new ServerHandler(
$this,
new InterThreadChannelReader($this->rakLib->getExternalQueue()),
new InterThreadChannelWriter($this->rakLib->getInternalQueue())
);
} }
public function start() : void{ public function start() : void{
@ -132,6 +136,7 @@ class RakLibInterface implements ServerInstance, AdvancedNetworkInterface{
public function shutdown() : void{ public function shutdown() : void{
$this->server->getTickSleeper()->removeNotifier($this->sleeper); $this->server->getTickSleeper()->removeNotifier($this->sleeper);
$this->interface->shutdown(); $this->interface->shutdown();
$this->rakLib->quit();
} }
public function openSession(int $sessionId, string $address, int $port, int $clientID) : void{ public function openSession(int $sessionId, string $address, int $port, int $clientID) : void{

View File

@ -0,0 +1,193 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\network\mcpe\raklib;
use pocketmine\snooze\SleeperNotifier;
use pocketmine\thread\Thread;
use raklib\generic\Socket;
use raklib\RakLib;
use raklib\server\InterThreadChannelReader;
use raklib\server\InterThreadChannelWriter;
use raklib\server\SessionManager;
use raklib\utils\ExceptionTraceCleaner;
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{
/** @var InternetAddress */
private $address;
/** @var \ThreadedLogger */
protected $logger;
/** @var bool */
protected $cleanShutdown = false;
/** @var bool */
protected $ready = false;
/** @var \Threaded */
protected $externalQueue;
/** @var \Threaded */
protected $internalQueue;
/** @var string */
protected $mainPath;
/** @var int */
protected $serverId = 0;
/** @var int */
protected $maxMtuSize;
/** @var int */
private $protocolVersion;
/** @var SleeperNotifier */
protected $mainThreadNotifier;
/** @var \Throwable|null */
public $crashInfo = null;
/**
* @param \ThreadedLogger $logger
* @param InternetAddress $address
* @param int $maxMtuSize
* @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){
$this->address = $address;
$this->serverId = mt_rand(0, PHP_INT_MAX);
$this->maxMtuSize = $maxMtuSize;
$this->logger = $logger;
$this->externalQueue = new \Threaded;
$this->internalQueue = new \Threaded;
$this->mainPath = \pocketmine\PATH;
$this->protocolVersion = $overrideProtocolVersion ?? RakLib::DEFAULT_PROTOCOL_VERSION;
$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
*/
public function shutdownHandler(){
if($this->cleanShutdown !== true){
$error = error_get_last();
if($error !== null){
$this->logger->emergency("Fatal error: " . $error["message"] . " in " . $error["file"] . " on line " . $error["line"]);
$this->setCrashInfo(new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']));
}else{
$this->logger->emergency("RakLib shutdown unexpectedly");
}
}
}
public function getCrashInfo() : ?\Throwable{
return $this->crashInfo;
}
private function setCrashInfo(\Throwable $e) : void{
$this->synchronized(function(\Throwable $e) : void{
$this->crashInfo = $e;
$this->notify();
}, $e);
}
public function startAndWait(int $options = PTHREADS_INHERIT_NONE) : void{
$this->start($options);
$this->synchronized(function() : void{
while(!$this->ready and $this->crashInfo === null){
$this->wait();
}
if($this->crashInfo !== null){
throw $this->crashInfo;
}
});
}
public function onRun() : void{
try{
gc_enable();
ini_set("display_errors", '1');
ini_set("display_startup_errors", '1');
register_shutdown_function([$this, "shutdownHandler"]);
$socket = new Socket($this->address);
$manager = new SessionManager(
$this->serverId,
$this->logger,
$socket,
$this->maxMtuSize,
$this->protocolVersion,
new InterThreadChannelReader($this->internalQueue),
new InterThreadChannelWriter($this->externalQueue, $this->mainThreadNotifier),
new ExceptionTraceCleaner($this->mainPath)
);
$this->synchronized(function() : void{
$this->ready = true;
$this->notify();
});
$manager->run();
$this->cleanShutdown = true;
}catch(\Throwable $e){
$this->setCrashInfo($e);
$this->logger->logException($e);
}
}
}

View File

@ -114,7 +114,7 @@ final class Process{
//TODO: more OS //TODO: more OS
return count(ThreadManager::getInstance()->getAll()) + 3; //RakLib + MainLogger + Main Thread return count(ThreadManager::getInstance()->getAll()) + 2; //MainLogger + Main Thread
} }
/** /**