Skip to content

Commit

Permalink
Updated for PM5
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed May 31, 2023
1 parent 5a0eb87 commit 6d04d58
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
11 changes: 9 additions & 2 deletions phpstan/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "pocketmine/phpstan-plugin-config",
"require": {
"phpstan/phpstan": "^1.10.0",
"pocketmine/pocketmine-mp": "4.21.0",
"pocketmine/pocketmine-mp": "dev-major-next",
"netresearch/jsonmapper": "dev-array-in-string-property-error as 4.2.0",
"phpstan/phpstan-strict-rules": "^1.0",
"phpstan/extension-installer": "^1.0",
"ext-sockets": "*",
Expand All @@ -17,5 +18,11 @@
"allow-plugins": {
"phpstan/extension-installer": true
}
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/dktapps/JsonMapper.git"
}
]
}
4 changes: 2 additions & 2 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: RconServer
main: pmmp\RconServer\Main
src-namespace-prefix: pmmp\RconServer
version: 1.0.0
api: 4.21.0
version: 1.2.0
api: 5.0.0
load: POSTWORLD
author: PMMP Team
description: Allow RCON clients to control your server
Expand Down
2 changes: 1 addition & 1 deletion src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use pocketmine\plugin\PluginBase;
use pocketmine\plugin\PluginException;
use pocketmine\utils\Filesystem;
use Webmozart\PathUtil\Path;
use Symfony\Component\Filesystem\Path;
use function base64_encode;
use function file_exists;
use function file_put_contents;
Expand Down
13 changes: 5 additions & 8 deletions src/Rcon.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

use pocketmine\network\NetworkInterface;
use pocketmine\snooze\SleeperHandler;
use pocketmine\snooze\SleeperNotifier;
use pocketmine\thread\log\ThreadSafeLogger;
use pocketmine\utils\TextFormat;
use function socket_bind;
use function socket_close;
Expand All @@ -45,7 +45,6 @@
use function trim;
use const AF_INET;
use const AF_UNIX;
use const PTHREADS_INHERIT_NONE;
use const SO_REUSEADDR;
use const SOCK_STREAM;
use const SOCKET_ENOPROTOOPT;
Expand All @@ -65,7 +64,7 @@ class Rcon implements NetworkInterface{
* @phpstan-param callable(string $command) : string $onCommandCallback
* @throws RconException
*/
public function __construct(RconConfig $config, callable $onCommandCallback, \ThreadedLogger $logger, SleeperHandler $sleeper){
public function __construct(RconConfig $config, callable $onCommandCallback, ThreadSafeLogger $logger, SleeperHandler $sleeper){
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if($socket === false){
throw new RconException("Failed to create socket: " . socket_strerror(socket_last_error()));
Expand All @@ -92,9 +91,7 @@ public function __construct(RconConfig $config, callable $onCommandCallback, \Th

[$this->ipcMainSocket, $this->ipcThreadSocket] = $ipc;

$notifier = new SleeperNotifier();

$sleeper->addNotifier($notifier, function() use ($onCommandCallback) : void{
$sleeperEntry = $sleeper->addNotifier(function() use ($onCommandCallback) : void{
$response = $onCommandCallback($this->thread->cmd);

$this->thread->response = TextFormat::clean($response);
Expand All @@ -103,11 +100,11 @@ public function __construct(RconConfig $config, callable $onCommandCallback, \Th
}, $this->thread);
});

$this->thread = new RconThread($this->socket, $config->password, $config->maxConnections, $logger, $this->ipcThreadSocket, $notifier);
$this->thread = new RconThread($this->socket, $config->password, $config->maxConnections, $logger, $this->ipcThreadSocket, $sleeperEntry);
}

public function start() : void{
$this->thread->start(PTHREADS_INHERIT_NONE);
$this->thread->start();
}

public function tick() : void{
Expand Down
13 changes: 8 additions & 5 deletions src/RconThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

namespace pmmp\RconServer;

use pocketmine\snooze\SleeperNotifier;
use pocketmine\snooze\SleeperHandlerEntry;
use pocketmine\thread\log\ThreadSafeLogger;
use pocketmine\thread\Thread;
use pocketmine\utils\Binary;
use function count;
Expand Down Expand Up @@ -61,9 +62,9 @@ public function __construct(
private \Socket $socket,
private string $password,
private int $maxClients,
private \ThreadedLogger $logger,
private ThreadSafeLogger $logger,
private \Socket $ipcSocket,
private SleeperNotifier $notifier
private SleeperHandlerEntry $sleeperEntry
){}

private function writePacket(\Socket $client, int $requestID, int $packetType, string $payload) : void{
Expand Down Expand Up @@ -129,6 +130,8 @@ protected function onRun() : void{
/** @var int $nextClientId */
$nextClientId = 0;

$notifier = $this->sleeperEntry->createNotifier();

while(!$this->stop){
$r = $clients;
$r["main"] = $this->socket; //this is ugly, but we need to be able to mass-select()
Expand Down Expand Up @@ -188,8 +191,8 @@ protected function onRun() : void{
}
if($payload !== ""){
$this->cmd = ltrim($payload);
$this->synchronized(function() : void{
$this->notifier->wakeupSleeper();
$this->synchronized(function() use ($notifier) : void{
$notifier->wakeupSleeper();
$this->wait();
});
$this->writePacket($sock, $requestID, 0, str_replace("\n", "\r\n", trim($this->response)));
Expand Down

0 comments on commit 6d04d58

Please sign in to comment.