MadelineProto/src/danog/MadelineProto/Ipc/Server.php

145 lines
4.3 KiB
PHP
Raw Normal View History

2020-07-11 20:01:54 +02:00
<?php
/**
* API wrapper module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\Ipc;
use Amp\Ipc\IpcServer;
use Amp\Ipc\Sync\ChannelledSocket;
2020-07-28 20:39:32 +02:00
use danog\Loop\SignalLoop;
2020-07-11 20:01:54 +02:00
use danog\MadelineProto\Ipc\Runner\ProcessRunner;
use danog\MadelineProto\Ipc\Runner\WebRunner;
use danog\MadelineProto\Logger;
use danog\MadelineProto\Tools;
/**
* IPC server.
*/
class Server extends SignalLoop
{
2020-07-12 00:17:47 +02:00
/**
2020-07-12 01:27:26 +02:00
* Session not initialized, should initialize.
2020-07-12 00:17:47 +02:00
*/
const NOT_INITED = 'not inited';
/**
2020-07-12 01:27:26 +02:00
* Session uses event handler, should start from main event handler file.
2020-07-12 00:17:47 +02:00
*/
const EVENT_HANDLER = 'event';
2020-07-11 20:01:54 +02:00
/**
* IPC server.
*/
private IpcServer $server;
/**
* Set IPC path.
*
* @param string $path IPC path
*
* @return void
*/
public function setIpcPath(string $path): void
{
$this->server = new IpcServer($path);
}
/**
* Start IPC server in background.
*
* @param string $session Session path
*
2020-07-12 01:12:20 +02:00
* @return void
2020-07-11 20:01:54 +02:00
*/
2020-07-12 01:12:20 +02:00
public static function startMe(string $session): void
2020-07-11 20:01:54 +02:00
{
try {
Logger::log("Starting IPC server $session (process)");
2020-07-12 01:12:20 +02:00
ProcessRunner::start($session);
2020-07-11 20:01:54 +02:00
return;
} catch (\Throwable $e) {
Logger::log($e);
}
Logger::log("Starting IPC server $session (web)");
2020-07-12 01:12:20 +02:00
WebRunner::start($session);
2020-07-11 20:01:54 +02:00
}
/**
* Main loop.
*
* @return \Generator
*/
public function loop(): \Generator
{
while ($socket = yield $this->waitSignal($this->server->accept())) {
Tools::callFork($this->clientLoop($socket));
}
$this->server->close();
}
/**
* Client handler loop.
*
* @param ChannelledSocket $socket Client
*
* @return \Generator
*/
private function clientLoop(ChannelledSocket $socket): \Generator
{
$this->API->logger("Accepted IPC client connection!");
$id = 0;
2020-07-12 01:12:20 +02:00
try {
while ($payload = yield $socket->receive()) {
2020-07-12 01:27:26 +02:00
Tools::callFork($this->clientRequest($socket, $id++, $payload));
2020-07-12 01:12:20 +02:00
}
2020-07-12 01:27:26 +02:00
} catch (\Throwable $e) {
}
2020-07-11 20:01:54 +02:00
}
/**
* Handle client request.
*
* @param ChannelledSocket $socket Socket
* @param integer $id Request ID
* @param array $payload Payload
*
* @return \Generator
*/
public function clientRequest(ChannelledSocket $socket, int $id, $payload): \Generator
{
try {
$result = $this->API->{$payload[0]}(...$payload[1]);
$result = $result instanceof \Generator ? yield from $result : yield $result;
} catch (\Throwable $e) {
$result = new ExitFailure($e);
}
try {
yield $socket->send([$id, $result]);
} catch (\Throwable $e) {
$this->API->logger("Got error while trying to send result of ${payload[0]}: $e", Logger::ERROR);
try {
yield $socket->send([$id, new ExitFailure($e)]);
} catch (\Throwable $e) {
$this->API->logger("Got error while trying to send error of error of ${payload[0]}: $e", Logger::ERROR);
}
}
}
/**
* Get the name of the loop.
*
* @return string
*/
public function __toString(): string
{
return "IPC server";
}
}