1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-12-11 23:19:38 +01:00
MadelineProto/src/Ipc/ServerCallback.php

91 lines
2.8 KiB
PHP
Raw Normal View History

2022-12-30 21:54:44 +01:00
<?php
declare(strict_types=1);
2020-09-25 19:17:16 +02:00
/**
* IPC callback server.
*
* 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>
2023-01-04 12:43:01 +01:00
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
2020-09-25 19:17:16 +02:00
* @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;
use danog\MadelineProto\Exception;
use danog\MadelineProto\SessionPaths;
2022-12-30 20:24:13 +01:00
use Revolt\EventLoop;
2020-09-25 19:17:16 +02:00
/**
* IPC callback server.
*/
2023-01-15 12:05:38 +01:00
final class ServerCallback extends Server
2020-09-25 19:17:16 +02:00
{
/**
* Timeout watcher list, indexed by socket ID.
*
* @var array<int, string>
*/
2023-01-03 22:07:58 +01:00
private array $watcherList = [];
2020-09-25 19:17:16 +02:00
/**
* Timeout watcher list, indexed by socket ID.
*
* @var array<int, ChannelledSocket>
*/
2023-01-03 22:07:58 +01:00
private array $socketList = [];
2020-09-25 19:17:16 +02:00
/**
* Counter.
*/
private int $id = 0;
/**
* Set IPC path.
*
* @param SessionPaths $session Session
*/
public function setIpcPath(SessionPaths $session): void
{
$this->server = new IpcServer($session->getIpcCallbackPath());
}
/**
* Client handler loop.
*
* @param ChannelledSocket $socket Client
*/
2023-01-03 22:07:58 +01:00
protected function clientLoop(ChannelledSocket $socket): void
2020-09-25 19:17:16 +02:00
{
$id = $this->id++;
$this->API->logger("Accepted IPC callback connection, assigning ID $id!");
2020-09-25 19:17:16 +02:00
$this->socketList[$id] = $socket;
2022-12-30 20:24:13 +01:00
$this->watcherList[$id] = EventLoop::delay(30, function () use ($id): void {
2020-09-25 19:17:16 +02:00
unset($this->watcherList[$id], $this->socketList[$id]);
});
2023-01-03 22:07:58 +01:00
$socket->send($id);
2020-09-25 19:17:16 +02:00
}
/**
* Unwrap value.
2020-09-25 19:17:16 +02:00
*/
protected function unwrap(Wrapper $wrapper)
{
$id = $wrapper->getRemoteId();
if (!isset($this->socketList[$id])) {
2023-01-04 15:13:55 +01:00
throw new Exception('IPC timeout, could not find callback socket!');
2020-09-25 19:17:16 +02:00
}
$socket = $this->socketList[$id];
2022-12-30 21:43:58 +01:00
EventLoop::cancel($this->watcherList[$id]);
2020-09-25 19:17:16 +02:00
unset($this->watcherList[$id], $this->socketList[$id]);
return $wrapper->unwrap($socket);
}
}