Finalize IPC API

This commit is contained in:
Daniil Gentili 2020-09-24 21:13:58 +02:00
parent 164000b591
commit f90159c8bd
4 changed files with 17 additions and 14 deletions

View File

@ -201,7 +201,7 @@ class API extends InternalDoc
return yield from $this->connectToMadelineProto($settings, true); return yield from $this->connectToMadelineProto($settings, true);
} elseif ($unserialized instanceof ChannelledSocket) { } elseif ($unserialized instanceof ChannelledSocket) {
// Success, IPC client // Success, IPC client
$this->API = new Client($unserialized, $this->session->getIpcPath(), Logger::$default); $this->API = new Client($unserialized, $this->session, Logger::$default);
$this->APIFactory(); $this->APIFactory();
return true; return true;
} elseif ($unserialized) { } elseif ($unserialized) {

View File

@ -24,6 +24,7 @@ use Amp\Promise;
use danog\MadelineProto\API; use danog\MadelineProto\API;
use danog\MadelineProto\Exception; use danog\MadelineProto\Exception;
use danog\MadelineProto\Logger; use danog\MadelineProto\Logger;
use danog\MadelineProto\SessionPaths;
use danog\MadelineProto\Tools; use danog\MadelineProto\Tools;
use function Amp\Ipc\connect; use function Amp\Ipc\connect;
@ -49,9 +50,9 @@ class Client
*/ */
private bool $run = true; private bool $run = true;
/** /**
* IPC path. * Session.
*/ */
private string $ipcPath; private SessionPaths $session;
/** /**
* Logger instance. * Logger instance.
*/ */
@ -60,14 +61,14 @@ class Client
* Constructor function. * Constructor function.
* *
* @param ChannelledSocket $socket IPC client socket * @param ChannelledSocket $socket IPC client socket
* @param string $ipcPath IPC socket path * @param SessionPaths $session Session paths
* @param Logger $logger Logger * @param Logger $logger Logger
*/ */
public function __construct(ChannelledSocket $server, string $ipcPath, Logger $logger) public function __construct(ChannelledSocket $server, SessionPaths $session, Logger $logger)
{ {
$this->logger = $logger; $this->logger = $logger;
$this->server = $server; $this->server = $server;
$this->ipcPath = $ipcPath; $this->session = $session;
Tools::callFork($this->loopInternal()); Tools::callFork($this->loopInternal());
} }
/** /**
@ -112,7 +113,8 @@ class Client
if ($this->run) { if ($this->run) {
$this->logger("Reconnecting to IPC server!"); $this->logger("Reconnecting to IPC server!");
yield $this->server->disconnect(); yield $this->server->disconnect();
$this->server = yield connect($this->ipcPath); Server::startMe($this->session);
$this->server = yield connect($this->session->getIpcPath());
} }
} }
} }
@ -152,9 +154,10 @@ class Client
* *
* @internal * @internal
*/ */
public function stopIpcServer(): \Generator public function stopIpcServer(): Promise
{ {
yield $this->server->send(Server::SHUTDOWN); $this->run = false;
return $this->server->send(Server::SHUTDOWN);
} }
/** /**
* Call function. * Call function.

View File

@ -97,12 +97,12 @@ use danog\MadelineProto\Tools;
Magic::classExists(); Magic::classExists();
Magic::$script_cwd = $_GET['cwd'] ?? Magic::getcwd(); Magic::$script_cwd = $_GET['cwd'] ?? Magic::getcwd();
$API = new API($ipcPath, (new Settings)->getSerialization()->setForceFull(true)); $API = new API($ipcPath, (new Settings)->getSerialization()->setForceFull(true));
$API->init();
$API->initSelfRestart();
Tools::wait($session->storeIpcState(new IpcState($runnerId)));
while (true) { while (true) {
try { try {
$API->init();
$API->initSelfRestart();
Tools::wait($session->storeIpcState(new IpcState($runnerId)));
Tools::wait(Server::waitShutdown()); Tools::wait(Server::waitShutdown());
return; return;
} catch (\Throwable $e) { } catch (\Throwable $e) {

View File

@ -154,12 +154,12 @@ class Server extends SignalLoop
} }
} finally { } finally {
yield $socket->disconnect(); yield $socket->disconnect();
/*if ($payload === self::SHUTDOWN) { if ($payload === self::SHUTDOWN) {
$this->signal(null); $this->signal(null);
if (self::$shutdownDeferred) { if (self::$shutdownDeferred) {
self::$shutdownDeferred->resolve(); self::$shutdownDeferred->resolve();
} }
}*/ }
} }
} }
/** /**