Files
kiri-http-server/Server.php
T

177 lines
4.3 KiB
PHP
Raw Normal View History

2022-01-09 03:49:02 +08:00
<?php
2022-01-10 11:39:55 +08:00
namespace Kiri\Server;
2022-01-09 03:49:02 +08:00
use Exception;
2022-01-13 18:41:15 +08:00
use JetBrains\PhpStorm\Pure;
2022-01-12 14:54:51 +08:00
use Kiri;
2022-01-09 03:49:02 +08:00
use Kiri\Abstracts\Config;
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
2022-02-23 16:32:07 +08:00
use Kiri\Message\Constrict\Request;
use Kiri\Message\Constrict\RequestInterface;
use Kiri\Message\Constrict\Response;
use Kiri\Message\Constrict\ResponseInterface;
2022-01-12 11:20:33 +08:00
use Kiri\Message\Handler\Abstracts\HttpService;
2022-02-18 15:28:28 +08:00
use Kiri\Message\Handler\Router;
2022-02-14 10:55:56 +08:00
use Kiri\Server\Events\OnServerBeforeStart;
2022-01-12 11:20:33 +08:00
use Kiri\Server\Events\OnShutdown;
2022-01-09 03:49:02 +08:00
use Psr\Container\ContainerExceptionInterface;
2022-03-03 18:11:24 +08:00
use Psr\Container\ContainerInterface;
2022-01-09 03:49:02 +08:00
use Psr\Container\NotFoundExceptionInterface;
2022-01-17 18:45:00 +08:00
use ReflectionException;
2022-01-09 17:56:47 +08:00
use Swoole\Coroutine;
2022-01-09 03:49:02 +08:00
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
/**
* Class Server
* @package Http
*/
class Server extends HttpService
{
2022-03-03 18:11:24 +08:00
private array $process = [];
2022-01-09 03:49:02 +08:00
private mixed $daemon = 0;
2022-04-10 03:37:58 +08:00
/**
* @param State $state
* @param ServerManager $manager
* @param ContainerInterface $container
* @param ProcessManager $processManager
* @param EventDispatch $eventDispatch
* @param Router $router
* @param array $config
* @throws Exception
*/
2022-03-03 18:11:24 +08:00
public function __construct(public State $state,
public ServerManager $manager,
public ContainerInterface $container,
public ProcessManager $processManager,
public EventDispatch $eventDispatch,
public Router $router,
array $config = [])
{
parent::__construct($config);
}
2022-01-12 14:54:51 +08:00
2022-01-09 03:49:02 +08:00
/**
2022-02-23 16:32:07 +08:00
* @return void
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2022-01-09 03:49:02 +08:00
*/
public function init()
{
2022-03-03 18:11:24 +08:00
$this->container->mapping(ResponseInterface::class, Response::class);
$this->container->mapping(RequestInterface::class, Request::class);
2022-02-23 16:32:07 +08:00
2022-01-12 14:54:51 +08:00
$enable_coroutine = Config::get('servers.settings.enable_coroutine', false);
if ($enable_coroutine != true) {
return;
}
Coroutine::set([
'hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION,
'enable_deadlock_check' => FALSE,
'exit_condition' => function () {
return Coroutine::stats()['coroutine_num'] === 0;
}
]);
2022-01-09 03:49:02 +08:00
}
/**
* @param $process
*/
public function addProcess($process)
{
$this->process[] = $process;
}
/**
2022-02-14 16:04:09 +08:00
* @return void
2022-01-09 03:49:02 +08:00
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2022-04-10 03:37:58 +08:00
* @throws Exception
2022-01-09 03:49:02 +08:00
*/
2022-02-14 10:55:56 +08:00
public function start(): void
2022-01-09 03:49:02 +08:00
{
2022-01-12 14:54:51 +08:00
$this->manager->initBaseServer(Config::get('server', [], true), $this->daemon);
2022-01-09 03:49:02 +08:00
$rpcService = Config::get('rpc', []);
if (!empty($rpcService)) {
2022-02-14 16:04:09 +08:00
$this->manager->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'], $rpcService['mode'], $rpcService);
2022-01-09 03:49:02 +08:00
}
2022-04-10 15:13:03 +08:00
// $this->process[] = Inotify::class;
2022-04-10 03:37:58 +08:00
2022-01-09 03:49:02 +08:00
$processes = array_merge($this->process, Config::get('processes', []));
2022-01-12 11:20:33 +08:00
2022-03-03 18:11:24 +08:00
$this->processManager->batch($processes);
2022-01-09 03:49:02 +08:00
2022-02-23 16:32:07 +08:00
$this->eventDispatch->dispatch(new OnServerBeforeStart());
2022-01-13 18:36:25 +08:00
2022-02-14 10:55:56 +08:00
$this->manager->start();
2022-01-12 11:20:33 +08:00
}
/**
2022-01-09 03:49:02 +08:00
* @return void
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2022-04-10 03:37:58 +08:00
* @throws Exception
2022-01-09 03:49:02 +08:00
*/
public function shutdown()
{
$configs = Config::get('server', [], true);
2022-01-12 14:54:51 +08:00
foreach ($this->manager->sortService($configs['ports'] ?? []) as $config) {
2022-01-09 03:49:02 +08:00
$this->state->exit($config['port']);
}
2022-03-03 18:11:24 +08:00
$this->eventDispatch->dispatch(new OnShutdown());
2022-01-09 03:49:02 +08:00
}
/**
* @return bool
* @throws ConfigException
* @throws Exception
*/
public function isRunner(): bool
{
return $this->state->isRunner();
}
/**
* @param $daemon
* @return Server
*/
public function setDaemon($daemon): static
{
if (!in_array($daemon, [0, 1])) {
return $this;
}
$this->daemon = $daemon;
return $this;
}
/**
* @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
*/
2022-01-13 18:41:15 +08:00
#[Pure] public function getServer(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
2022-01-09 03:49:02 +08:00
{
2022-01-12 14:54:51 +08:00
return $this->manager->getServer();
2022-01-09 03:49:02 +08:00
}
}