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

273 lines
6.0 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;
2023-04-22 02:04:31 +08:00
use Kiri\Abstracts\Logger;
2022-06-16 17:38:22 +08:00
use Kiri\Exception\ConfigException;
2023-04-22 02:04:31 +08:00
use Kiri\Exception\NotFindClassException;
use Kiri\Server\Config as SConfig;
use Kiri\Server\Constant;
use Kiri\Server\Events\OnServerBeforeStart;
2022-06-20 18:14:25 +08:00
use Kiri\Server\Events\OnShutdown;
2023-04-22 02:04:31 +08:00
use Kiri\Server\Handler\OnServer;
use Kiri\Server\ServerInterface;
use Kiri\Server\Task\TaskInterface;
use Kiri\Server\Task\Task;
2022-06-16 17:38:22 +08:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Swoole\Server;
/**
*
*/
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
*/
2023-04-22 02:04:31 +08:00
private ?Server $server = null;
2022-06-16 17:38:22 +08:00
/**
* @param array $service
* @param int $daemon
* @return void
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
{
2023-04-22 02:04:31 +08:00
$this->listener($service, $daemon);
$this->initRpcListen();
$this->initProcess();
$this->onSignal();
}
2023-04-19 13:38:08 +08:00
2023-04-22 02:04:31 +08:00
/**
* @param array $service
* @param $daemon
* @return void
* @throws
*/
private function listener(array $service, $daemon): void
{
$service = $this->genConfigService($service);
2022-06-16 17:38:22 +08:00
foreach ($service as $value) {
2023-04-22 02:04:31 +08:00
if (is_null($this->server)) {
$this->createBaseServer($value, $daemon);
} else {
$this->addListener($value);
}
2022-06-16 17:38:22 +08:00
}
2023-04-22 02:04:31 +08:00
}
/**
* @return void
* @throws Exception
*/
private function initRpcListen(): void
{
2023-05-25 16:59:17 +08:00
$rpcService = \config('rpc', []);
2022-06-22 16:29:41 +08:00
if (!empty($rpcService)) {
$this->addListener(instance(SConfig::class, [], $rpcService));
}
2022-06-16 17:38:22 +08:00
}
/**
2023-04-22 02:04:31 +08:00
* @return void
2022-06-16 17:38:22 +08:00
*/
2023-04-22 02:04:31 +08:00
private function initProcess(): void
2022-06-16 17:38:22 +08:00
{
2023-04-22 02:04:31 +08:00
foreach ($this->_process as $process) {
$this->server->addProcess($process);
}
2022-06-16 17:38:22 +08:00
}
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-22 02:04:31 +08:00
event(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 NotFindClassException
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);
}
2023-04-22 02:04:31 +08:00
$this->initServer($match, $config, $daemon);
2023-05-25 16:59:17 +08:00
$this->onEventListen($this->server, \config('server.events', []));
2023-04-22 02:04:31 +08:00
$this->onEventListen($this->server, $config->events);
$this->onTaskListen();
}
/**
* @param $match
* @param $config
* @param $daemon
* @return void
* @throws ConfigException
* @throws ReflectionException
*/
private function initServer($match, $config, $daemon): void
{
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-19 15:35:57 +08:00
Logger::_alert('Listen ' . $config->type . ' address ' . $config->host . '::' . $config->port);
2023-04-19 15:23:38 +08:00
if (!isset($config->events[Constant::SHUTDOWN])) {
$config->events[Constant::SHUTDOWN] = [OnServer::class, 'onShutdown'];
}
2023-04-22 02:04:31 +08:00
Kiri::service()->set('server', $this->server);
}
/**
* @return void
* @throws
*/
private function onTaskListen(): void
{
if (!isset($this->server->setting[Constant::OPTION_TASK_WORKER_NUM])) {
return;
}
$container = Kiri::getDi();
$task = $container->get(Task::class);
2023-04-22 02:11:07 +08:00
$container->bind(TaskInterface::class, $task);
2023-04-22 02:04:31 +08:00
$task->initTaskWorker($this->server);
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
{
2023-05-25 16:59:17 +08:00
$settings = array_merge(\config('server.settings', []), $config->settings);
2022-06-16 17:38:22 +08:00
$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
{
2023-04-22 02:30:02 +08:00
$port = $this->server->addlistener($config->host, $config->port, $config->socket);
2022-06-16 17:38:22 +08:00
if ($port === false) {
throw new Exception('Listen port fail.' . swoole_last_error());
}
2023-04-22 02:30:02 +08:00
Logger::_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());
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
*/
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-22 02:04:31 +08:00
Logger::_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
2023-04-22 02:04:31 +08:00
* @throws
2022-06-16 17:38:22 +08:00
*/
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-21 22:26:43 +08:00
event(new OnServerBeforeStart());
2022-06-16 17:38:22 +08:00
$this->server->start();
}
}