From c73c5b04150ff315e441dc370295d7b88b34dfbf Mon Sep 17 00:00:00 2001 From: programarivm Date: Mon, 7 Oct 2024 22:59:03 +0200 Subject: [PATCH] Implemented ImageAsyncTask --- cli/ratchet/binary.php | 8 +++--- cli/workerman/binary.php | 5 +++- src/Command/Binary/Cli.php | 5 ++-- src/Command/Binary/ImageAsyncTask.php | 37 +++++++++++++++++++++++++++ src/Command/Binary/ImageCommand.php | 21 +++++---------- 5 files changed, 55 insertions(+), 21 deletions(-) create mode 100644 src/Command/Binary/ImageAsyncTask.php diff --git a/cli/ratchet/binary.php b/cli/ratchet/binary.php index 90fffd7b..88ede104 100644 --- a/cli/ratchet/binary.php +++ b/cli/ratchet/binary.php @@ -12,22 +12,24 @@ use Ratchet\Http\HttpServer; use Ratchet\Server\IoServer; use Ratchet\WebSocket\WsServer; -use React\EventLoop\Factory; use React\Socket\LimitingServer; use React\Socket\Server; use React\Socket\SecureServer; +use Spatie\Async\Pool; require __DIR__ . '/../../vendor/autoload.php'; $dotenv = Dotenv::createImmutable(__DIR__.'/../../'); $dotenv->load(); +$pool = Pool::create(); + $logger = new Logger('log'); $logger->pushHandler(new StreamHandler(__DIR__.'/../../storage' . '/binary.log', Logger::INFO)); -$clientStorage = new ClientStorage($logger); +$parser = new Parser(new Cli($pool)); -$parser = new Parser(new Cli()); +$clientStorage = new ClientStorage($logger); $webSocket = (new BinaryWebSocket($parser))->init($clientStorage); diff --git a/cli/workerman/binary.php b/cli/workerman/binary.php index 5e3ac410..06e2c263 100644 --- a/cli/workerman/binary.php +++ b/cli/workerman/binary.php @@ -9,16 +9,19 @@ use Dotenv\Dotenv; use Monolog\Logger; use Monolog\Handler\StreamHandler; +use Spatie\Async\Pool; require __DIR__ . '/../../vendor/autoload.php'; $dotenv = Dotenv::createImmutable(__DIR__.'/../../'); $dotenv->load(); +$pool = Pool::create(); + $logger = new Logger('binary'); $logger->pushHandler(new StreamHandler(BinaryWebSocket::STORAGE_FOLDER . '/binary.log', Logger::INFO)); -$parser = new Parser(new Cli()); +$parser = new Parser(new Cli($pool)); $clientStorage = new ClientStorage($logger); diff --git a/src/Command/Binary/Cli.php b/src/Command/Binary/Cli.php index 052679b8..9db60803 100644 --- a/src/Command/Binary/Cli.php +++ b/src/Command/Binary/Cli.php @@ -3,13 +3,14 @@ namespace ChessServer\Command\Binary; use ChessServer\Command\AbstractCli; +use Spatie\Async\Pool; class Cli extends AbstractCli { - public function __construct() + public function __construct(Pool $pool) { parent::__construct(); - $this->commands->attach(new ImageCommand()); + $this->commands->attach((new ImageCommand())->setPool($pool)); } } diff --git a/src/Command/Binary/ImageAsyncTask.php b/src/Command/Binary/ImageAsyncTask.php new file mode 100644 index 00000000..7a1ebe10 --- /dev/null +++ b/src/Command/Binary/ImageAsyncTask.php @@ -0,0 +1,37 @@ +params = $params; + } + + public function configure() + { + } + + public function run() + { + $board = (new ClassicalStrToBoard($this->params['fen']))->create(); + $filename = (new BoardToPng($board, $this->params['flip'] === Color::B)) + ->output(AbstractSocket::TMP_FOLDER); + $contents = file_get_contents(AbstractSocket::TMP_FOLDER . "/$filename"); + $base64 = base64_encode($contents); + if (is_file(AbstractSocket::TMP_FOLDER . "/$filename")) { + unlink(AbstractSocket::TMP_FOLDER . "/$filename"); + } + + return $base64; + } +} diff --git a/src/Command/Binary/ImageCommand.php b/src/Command/Binary/ImageCommand.php index e7484be0..6879085a 100644 --- a/src/Command/Binary/ImageCommand.php +++ b/src/Command/Binary/ImageCommand.php @@ -2,9 +2,6 @@ namespace ChessServer\Command\Binary; -use Chess\Media\BoardToPng; -use Chess\Variant\Classical\FEN\StrToBoard as ClassicalStrToBoard; -use Chess\Variant\Classical\PGN\AN\Color; use ChessServer\Command\AbstractCommand; use ChessServer\Socket\AbstractSocket; @@ -28,17 +25,11 @@ public function run(AbstractSocket $socket, array $argv, int $id) { $params = json_decode(stripslashes($argv[1]), true); - $board = (new ClassicalStrToBoard($params['fen']))->create(); - $filename = (new BoardToPng($board, $params['flip'] === Color::B)) - ->output(AbstractSocket::TMP_FOLDER); - $contents = file_get_contents(AbstractSocket::TMP_FOLDER . "/$filename"); - $base64 = base64_encode($contents); - if (is_file(AbstractSocket::TMP_FOLDER . "/$filename")) { - unlink(AbstractSocket::TMP_FOLDER . "/$filename"); - } - - return $socket->getClientStorage()->send([$id], [ - $this->name => $base64, - ]); + $this->pool->add(new ImageAsyncTask($params), 81920) + ->then(function ($result) use ($socket, $id) { + return $socket->getClientStorage()->send([$id], [ + $this->name => $result, + ]); + }); } }