Files
kiri-http-server/Abstracts/AsyncServer.php
T

233 lines
5.7 KiB
PHP
Raw Normal View History

2022-06-16 17:38:22 +08:00
<?php
2022-06-16 17:49:06 +08:00
namespace Kiri\Server\Abstracts;
2022-06-16 17:38:22 +08:00
use Exception;
use Kiri;
use Kiri\Abstracts\Config;
2023-04-16 02:01:26 +08:00
use Psr\Container\ContainerInterface;
2022-06-16 17:38:22 +08:00
use Kiri\Exception\ConfigException;
2022-06-20 18:14:25 +08:00
use Kiri\Server\Events\OnShutdown;
2022-06-16 17:38:22 +08:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2022-06-17 12:27:33 +08:00
use Psr\Log\LoggerInterface;
2022-06-16 17:38:22 +08:00
use ReflectionException;
2022-06-22 16:29:41 +08:00
use Kiri\Server\Config as SConfig;
use Kiri\Di\LocalService;
2022-06-16 17:38:22 +08:00
use Swoole\Server;
2022-06-16 17:58:29 +08:00
use Kiri\Server\ServerInterface;
use Kiri\Server\Constant;
2022-06-16 17:38:22 +08:00
use Kiri\Events\EventDispatch;
use Kiri\Exception\NotFindClassException;
use Kiri\Server\Events\OnServerBeforeStart;
2023-04-16 02:15:51 +08:00
use Kiri\Di\Inject\Container;
2022-06-16 17:38:22 +08:00
/**
*
*/
2022-06-20 17:25:01 +08:00
class AsyncServer implements ServerInterface
2022-06-16 17:38:22 +08:00
{
use TraitServer;
/**
* @var Server|null
*/
private Server|null $server = null;
/**
* @param array $service
* @param int $daemon
* @return void
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFindClassException
* @throws NotFoundExceptionInterface
2022-10-11 15:15:04 +08:00
* @throws Exception
2022-06-16 17:38:22 +08:00
*/
public function initCoreServers(array $service, int $daemon = 0): void
{
$service = $this->genConfigService($service);
$this->createBaseServer(array_shift($service), $daemon);
foreach ($service as $value) {
$this->addListener($value);
}
2022-06-22 16:29:41 +08:00
$rpcService = Config::get('rpc', []);
if (!empty($rpcService)) {
$this->addListener(instance(SConfig::class, [], $rpcService));
}
2023-04-17 01:07:14 +08:00
$processManager = Kiri::getDi()->get(ProcessManager::class);
$processManager->batch(Config::get('processes', []));
2023-04-02 00:32:35 +08:00
$this->onSignal(Config::get('signal', []));
2022-06-16 17:38:22 +08:00
}
/**
* @param string $name
* @return Server|null
*/
public function getServer(string $name = ''): Server|null
{
return $this->server;
}
2022-06-20 18:14:25 +08:00
/**
2022-09-23 18:55:45 +08:00
* @return bool
2022-06-20 18:14:25 +08:00
* @throws ContainerExceptionInterface
2023-02-07 16:55:21 +08:00
* @throws NotFoundExceptionInterface|ReflectionException
2022-06-20 18:14:25 +08:00
*/
2022-09-23 18:55:45 +08:00
public function shutdown(): bool
2022-06-20 18:14:25 +08:00
{
$this->server->shutdown();
2023-04-17 01:07:14 +08:00
$processManager = Kiri::getDi()->get(EventDispatch::class);
$processManager->dispatch(new OnShutdown());
2022-09-23 18:55:45 +08:00
return true;
2022-06-20 18:14:25 +08:00
}
2022-06-16 17:38:22 +08:00
/**
2022-06-22 16:29:41 +08:00
* @param SConfig $config
2022-06-16 17:38:22 +08:00
* @param int $daemon
* @return void
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFindClassException
* @throws NotFoundExceptionInterface
2023-04-17 01:07:14 +08:00
* @throws ReflectionException
2022-06-16 17:38:22 +08:00
*/
2022-06-22 16:29:41 +08:00
private function createBaseServer(SConfig $config, int $daemon = 0): void
2022-06-16 17:38:22 +08:00
{
$match = $this->getServerClass($config->type);
if (is_null($match)) {
throw new NotFindClassException('Unknown server type ' . $config->type);
}
2022-10-25 14:58:30 +08:00
$this->server = new $match($config->host, $config->port, $config->mode, $config->socket);
2022-06-16 17:38:22 +08:00
$this->server->set($this->systemConfig($config, $daemon));
2023-04-16 02:46:54 +08:00
\Kiri::getLogger()->alert('Listen ' . $config->type . ' address ' . $config->host . '::' . $config->port);
2022-06-17 12:27:33 +08:00
2022-06-16 17:38:22 +08:00
$this->onEventListen($this->server, Config::get('server.events', []));
2022-06-16 18:17:17 +08:00
$this->onEventListen($this->server, $config->events);
2022-06-16 17:38:22 +08:00
}
/**
2022-06-22 16:29:41 +08:00
* @param SConfig $config
2022-06-16 17:38:22 +08:00
* @param int $daemon
* @return array
* @throws Exception
* @throws ConfigException
*/
2022-06-22 16:29:41 +08:00
protected function systemConfig(SConfig $config, int $daemon): array
2022-06-16 17:38:22 +08:00
{
$settings = array_merge(Config::get('server.settings', []), $config->settings);
$settings[Constant::OPTION_DAEMONIZE] = (bool)$daemon;
$settings[Constant::OPTION_ENABLE_REUSE_PORT] = true;
$settings[Constant::OPTION_PID_FILE] = storage('.swoole.pid');
if (!isset($settings[Constant::OPTION_PID_FILE])) {
$settings[Constant::OPTION_LOG_FILE] = storage('system.log');
}
return $settings;
}
/**
2022-06-22 16:29:41 +08:00
* @param SConfig $config
2022-06-16 17:38:22 +08:00
* @return void
* @throws Exception
*/
2022-06-22 16:29:41 +08:00
public function addListener(SConfig $config): void
2022-06-16 17:38:22 +08:00
{
$port = $this->server->addlistener($config->host, $config->port, $config->mode);
if ($port === false) {
throw new Exception('Listen port fail.' . swoole_last_error());
}
2023-04-16 02:46:54 +08:00
\Kiri::getLogger()->alert('Listen ' . $config->type . ' address ' . $config->host . '::' . $config->port);
2022-06-17 12:27:33 +08:00
2022-06-16 17:38:22 +08:00
$port->set($this->resetSettings($config->type, $config->settings));
$this->onEventListen($port, $config->getEvents());
2023-04-17 01:07:14 +08:00
Kiri::getDi()->get(LocalService::class)->set($config->getName(), $port);
2022-06-22 16:29:41 +08:00
}
/**
2022-06-22 18:58:26 +08:00
* @param $no
* @param array $signInfo
2022-06-22 16:29:41 +08:00
* @return void
2023-04-17 01:07:14 +08:00
* @throws ReflectionException
2022-06-22 16:29:41 +08:00
*/
2022-06-22 18:58:26 +08:00
public function onSigint($no, array $signInfo): void
2022-06-22 16:29:41 +08:00
{
try {
2023-04-16 02:46:54 +08:00
\Kiri::getLogger()->alert('Pid ' . getmypid() . ' get signo ' . $no);
2022-06-22 17:16:17 +08:00
$this->shutdown();
2022-06-22 16:29:41 +08:00
} catch (\Throwable $exception) {
2023-04-18 23:47:30 +08:00
error($exception);
2022-06-22 16:29:41 +08:00
}
2022-06-16 17:38:22 +08:00
}
/**
* @param string $type
* @param array $settings
* @return array
*/
private function resetSettings(string $type, array $settings): array
{
if ($type == Constant::SERVER_TYPE_HTTP && !isset($settings['open_http_protocol'])) {
$settings['open_http_protocol'] = true;
if (in_array($this->server->setting['dispatch_mode'], [2, 4])) {
$settings['open_http2_protocol'] = true;
}
}
if ($type == Constant::SERVER_TYPE_WEBSOCKET && !isset($settings['open_websocket_protocol'])) {
$settings['open_websocket_protocol'] = true;
}
return $settings;
}
/**
* @param Server\Port|Server $base
* @param array $events
* @return void
2023-04-17 01:07:14 +08:00
* @throws ReflectionException
2022-06-16 17:38:22 +08:00
*/
private function onEventListen(Server\Port|Server $base, array $events): void
{
foreach ($events as $name => $event) {
if (is_array($event) && is_string($event[0])) {
2023-04-17 01:07:14 +08:00
$event[0] = Kiri::getDi()->get($event[0]);
2022-06-16 17:38:22 +08:00
}
$base->on($name, $event);
}
}
/**
* @return void
2023-04-19 13:15:21 +08:00
* @throws
2022-06-16 17:38:22 +08:00
*/
public function start(): void
{
2023-04-19 13:15:21 +08:00
$pid = (int)file_get_contents(storage('.swoole.pid'));
if (posix_kill($pid, 0)) {
posix_kill($pid, SIGTERM);
}
2023-04-17 01:07:14 +08:00
$processManager = Kiri::getDi()->get(EventDispatch::class);
$processManager->dispatch(new OnServerBeforeStart());
2022-06-16 17:38:22 +08:00
$this->server->start();
}
}