Files
kiri-http-server/Server.php
T

158 lines
3.4 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;
2022-01-12 11:20:33 +08:00
use Kiri\Annotation\Inject;
2022-01-09 03:49:02 +08:00
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
2022-01-12 11:20:33 +08:00
use Kiri\Message\Handler\Abstracts\HttpService;
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;
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
{
private array $process = [
];
private mixed $daemon = 0;
/**
* @var State
*/
#[Inject(State::class)]
public State $state;
2022-02-14 10:42:27 +08:00
public Kiri\Server\Coroutine\Http $manager;
2022-01-12 14:54:51 +08:00
2022-01-09 03:49:02 +08:00
/**
*
*/
public function init()
{
2022-02-14 10:42:27 +08:00
$this->manager = Kiri::getContainer()->get(Kiri\Server\Coroutine\Http::class);
2022-01-12 14:54:51 +08:00
$enable_coroutine = Config::get('servers.settings.enable_coroutine', false);
Config::set('servers.settings.enable_coroutine', true);
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;
}
/**
* @return string
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2022-01-17 18:45:00 +08:00
* @throws ReflectionException
2022-01-09 03:49:02 +08:00
* @throws Exception
*/
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-01-12 14:54:51 +08:00
$this->manager->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'],
2022-01-09 03:49:02 +08:00
$rpcService['mode'], $rpcService);
}
$processes = array_merge($this->process, Config::get('processes', []));
2022-01-12 11:20:33 +08:00
2022-01-20 19:04:15 +08:00
$this->getContainer()->get(ProcessManager::class)->batch($processes);
2022-01-09 03:49:02 +08:00
2022-01-20 19:04:15 +08:00
$this->getEventDispatch()->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-01-17 18:45:00 +08:00
* @throws ReflectionException
2022-01-09 03:49:02 +08:00
* @throws Exception
*/
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-01-20 19:04:15 +08:00
$this->getContainer()->get(EventDispatch::class)->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
}
}