qqq
This commit is contained in:
+173
-211
@@ -26,232 +26,194 @@ use Swoole\Server;
|
|||||||
class AsyncServer implements ServerInterface
|
class AsyncServer implements ServerInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
use TraitServer;
|
use TraitServer;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Server|null
|
* @var Server|null
|
||||||
*/
|
|
||||||
private ?Server $server = null;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $service
|
|
||||||
* @param int $daemon
|
|
||||||
* @return void
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function initCoreServers(array $service, int $daemon = 0): void
|
|
||||||
{
|
|
||||||
$this->listener($service, $daemon);
|
|
||||||
$this->initProcess();
|
|
||||||
$this->onSignal();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $service
|
|
||||||
* @param $daemon
|
|
||||||
* @return void
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
private function listener(array $service, $daemon): void
|
|
||||||
{
|
|
||||||
$service = $this->genConfigService($service);
|
|
||||||
foreach ($service as $value) {
|
|
||||||
if (is_null($this->server)) {
|
|
||||||
$this->createBaseServer($value, $daemon);
|
|
||||||
} else {
|
|
||||||
$this->addListener($value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function initProcess(): void
|
|
||||||
{
|
|
||||||
foreach ($this->_process as $process) {
|
|
||||||
$this->server->addProcess($process);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface|ReflectionException
|
|
||||||
*/
|
|
||||||
public function shutdown(): bool
|
|
||||||
{
|
|
||||||
$this->server->shutdown();
|
|
||||||
|
|
||||||
event(new OnShutdown());
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param SConfig $config
|
|
||||||
* @param int $daemon
|
|
||||||
* @return void
|
|
||||||
* @throws ConfigException
|
|
||||||
* @throws NotFindClassException
|
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
|
||||||
private function createBaseServer(SConfig $config, int $daemon = 0): void
|
|
||||||
{
|
|
||||||
$match = $this->getServerClass($config->type);
|
|
||||||
if (is_null($match)) {
|
|
||||||
throw new NotFindClassException('Unknown server type ' . $config->type);
|
|
||||||
}
|
|
||||||
$this->initServer($match, $config, $daemon);
|
|
||||||
$this->onEventListen($this->server, \config('server.events', []));
|
|
||||||
$this->onEventListen($this->server, $config->events);
|
|
||||||
$this->onTaskListen();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $match
|
|
||||||
* @param $config
|
|
||||||
* @param $daemon
|
|
||||||
* @return void
|
|
||||||
* @throws ConfigException
|
|
||||||
*/
|
*/
|
||||||
private function initServer($match, $config, $daemon): void
|
private ?Server $server = null;
|
||||||
{
|
|
||||||
$this->server = new $match($config->host, $config->port, $config->mode, $config->socket);
|
|
||||||
$this->server->set($this->systemConfig($config, $daemon));
|
|
||||||
|
|
||||||
Logger::_alert('Listen ' . $config->type . ' address ' . $config->host . '::' . $config->port);
|
|
||||||
if (!isset($config->events[Constant::SHUTDOWN])) {
|
|
||||||
$config->events[Constant::SHUTDOWN] = [OnServer::class, 'onShutdown'];
|
|
||||||
}
|
|
||||||
Kiri::getDi()->bind(ServerInterface::class, $this->server);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @param array $service
|
||||||
* @throws
|
* @param int $daemon
|
||||||
*/
|
* @return void
|
||||||
private function onTaskListen(): void
|
* @throws Exception
|
||||||
{
|
*/
|
||||||
if (!isset($this->server->setting[Constant::OPTION_TASK_WORKER_NUM])) {
|
public function initCoreServers(array $service, int $daemon = 0): void
|
||||||
return;
|
{
|
||||||
}
|
$service = $this->genConfigService($service);
|
||||||
$container = Kiri::getDi();
|
$this->createBaseServer(array_pop($service), $daemon);
|
||||||
$task = $container->get(Task::class);
|
foreach ($service as $value) {
|
||||||
$container->bind(TaskInterface::class, $task);
|
$this->addListener($value);
|
||||||
$task->initTaskWorker($this->server);
|
}
|
||||||
}
|
foreach ($this->_process as $process) {
|
||||||
|
$this->server->addProcess($process);
|
||||||
|
}
|
||||||
|
on(OnServerBeforeStart::class, [$this, 'onSignal']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param SConfig $config
|
* @return bool
|
||||||
* @param int $daemon
|
* @throws ContainerExceptionInterface
|
||||||
* @return array
|
* @throws NotFoundExceptionInterface|ReflectionException
|
||||||
* @throws Exception
|
*/
|
||||||
* @throws ConfigException
|
public function shutdown(): bool
|
||||||
*/
|
{
|
||||||
protected function systemConfig(SConfig $config, int $daemon): array
|
$this->server->shutdown();
|
||||||
{
|
|
||||||
$settings = array_merge(\config('server.settings', []), $config->settings);
|
event(new OnShutdown());
|
||||||
$settings[Constant::OPTION_DAEMONIZE] = (bool)$daemon;
|
|
||||||
$settings[Constant::OPTION_ENABLE_REUSE_PORT] = true;
|
return 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param SConfig $config
|
* @param SConfig $config
|
||||||
* @return void
|
* @param int $daemon
|
||||||
* @throws Exception
|
* @return void
|
||||||
*/
|
* @throws ConfigException
|
||||||
public function addListener(SConfig $config): void
|
* @throws NotFindClassException
|
||||||
{
|
* @throws ReflectionException
|
||||||
$port = $this->server->addlistener($config->host, $config->port, $config->socket);
|
*/
|
||||||
if ($port === false) {
|
private function createBaseServer(SConfig $config, int $daemon = 0): void
|
||||||
throw new Exception('Listen port fail.' . swoole_last_error());
|
{
|
||||||
}
|
$match = $this->getServerClass($config->type);
|
||||||
|
if (is_null($match)) {
|
||||||
Logger::_alert('Listen ' . $config->type . ' address ' . $config->host . '::' . $config->port);
|
throw new NotFindClassException('Unknown server type ' . $config->type);
|
||||||
|
}
|
||||||
$port->set($this->resetSettings($config->type, $config->settings));
|
$this->initServer($match, $config, $daemon);
|
||||||
|
$this->onEventListen($this->server, \config('server.events', []));
|
||||||
$this->onEventListen($port, $config->getEvents());
|
$this->onEventListen($this->server, $config->events);
|
||||||
}
|
$this->onTaskListen();
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @param $no
|
|
||||||
* @param array $signInfo
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function onSigint($no, array $signInfo): void
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
Logger::_alert('Pid ' . getmypid() . ' get signo ' . $no);
|
|
||||||
$this->shutdown();
|
|
||||||
} catch (\Throwable $exception) {
|
|
||||||
error($exception);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $type
|
* @param $match
|
||||||
* @param array $settings
|
* @param $config
|
||||||
* @return array
|
* @param $daemon
|
||||||
* @throws
|
* @return void
|
||||||
*/
|
* @throws ConfigException
|
||||||
private function resetSettings(string $type, array $settings): array
|
*/
|
||||||
{
|
private function initServer($match, $config, $daemon): void
|
||||||
if ($type == Constant::SERVER_TYPE_HTTP && !isset($settings['open_http_protocol'])) {
|
{
|
||||||
$settings['open_http_protocol'] = true;
|
$this->server = new $match($config->host, $config->port, $config->mode, $config->socket);
|
||||||
if (in_array($this->server->setting['dispatch_mode'], [2, 4])) {
|
$this->server->set($this->systemConfig($config, $daemon));
|
||||||
$settings['open_http2_protocol'] = true;
|
|
||||||
}
|
Logger::_alert('Listen ' . $config->type . ' address ' . $config->host . '::' . $config->port);
|
||||||
}
|
if (!isset($config->events[Constant::SHUTDOWN])) {
|
||||||
if ($type == Constant::SERVER_TYPE_WEBSOCKET && !isset($settings['open_websocket_protocol'])) {
|
$config->events[Constant::SHUTDOWN] = [OnServer::class, 'onShutdown'];
|
||||||
$settings['open_websocket_protocol'] = true;
|
}
|
||||||
}
|
Kiri::getDi()->bind(ServerInterface::class, $this->server);
|
||||||
return $settings;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server\Port|Server $base
|
* @return void
|
||||||
* @param array $events
|
* @throws
|
||||||
* @return void
|
*/
|
||||||
* @throws ReflectionException
|
private function onTaskListen(): void
|
||||||
*/
|
{
|
||||||
private function onEventListen(Server\Port|Server $base, array $events): void
|
if (!isset($this->server->setting[Constant::OPTION_TASK_WORKER_NUM])) {
|
||||||
{
|
return;
|
||||||
foreach ($events as $name => $event) {
|
}
|
||||||
if (is_array($event) && is_string($event[0])) {
|
$container = Kiri::getDi();
|
||||||
$event[0] = Kiri::getDi()->get($event[0]);
|
$task = $container->get(Task::class);
|
||||||
}
|
$container->bind(TaskInterface::class, $task);
|
||||||
$base->on($name, $event);
|
$task->initTaskWorker($this->server);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @param SConfig $config
|
||||||
* @throws
|
* @param int $daemon
|
||||||
*/
|
* @return array
|
||||||
public function start(): void
|
* @throws Exception
|
||||||
{
|
* @throws ConfigException
|
||||||
event(new OnServerBeforeStart());
|
*/
|
||||||
$this->server->start();
|
protected function systemConfig(SConfig $config, int $daemon): array
|
||||||
}
|
{
|
||||||
|
$settings = array_merge(\config('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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param SConfig $config
|
||||||
|
* @return void
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function addListener(SConfig $config): void
|
||||||
|
{
|
||||||
|
$port = $this->server->addlistener($config->host, $config->port, $config->socket);
|
||||||
|
if ($port === false) {
|
||||||
|
throw new Exception('Listen port fail.' . swoole_last_error());
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::_alert('Listen ' . $config->type . ' address ' . $config->host . '::' . $config->port);
|
||||||
|
|
||||||
|
$port->set($this->resetSettings($config->type, $config->settings));
|
||||||
|
|
||||||
|
$this->onEventListen($port, $config->getEvents());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $type
|
||||||
|
* @param array $settings
|
||||||
|
* @return array
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
private function onEventListen(Server\Port|Server $base, array $events): void
|
||||||
|
{
|
||||||
|
foreach ($events as $name => $event) {
|
||||||
|
if (is_array($event) && is_string($event[0])) {
|
||||||
|
$event[0] = Kiri::getDi()->get($event[0]);
|
||||||
|
}
|
||||||
|
$base->on($name, $event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function start(): void
|
||||||
|
{
|
||||||
|
event(new OnServerBeforeStart());
|
||||||
|
$this->server->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace Kiri\Server\Abstracts;
|
|||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri;
|
use Kiri;
|
||||||
|
use Kiri\Abstracts\Logger;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Swoole\Coroutine;
|
use Swoole\Coroutine;
|
||||||
use Swoole\Http\Server as HServer;
|
use Swoole\Http\Server as HServer;
|
||||||
@@ -78,6 +79,22 @@ trait TraitServer
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $no
|
||||||
|
* @param array $signInfo
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function onSigint($no, array $signInfo): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
Logger::_alert('Pid ' . getmypid() . ' get signo ' . $no);
|
||||||
|
$this->shutdown();
|
||||||
|
} catch (\Throwable $exception) {
|
||||||
|
error($exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $signal
|
* @param $signal
|
||||||
* @param $callback
|
* @param $callback
|
||||||
|
|||||||
@@ -68,7 +68,6 @@ class Server
|
|||||||
on(OnWorkerStop::class, [Timer::class, 'clearAll'], 9999);
|
on(OnWorkerStop::class, [Timer::class, 'clearAll'], 9999);
|
||||||
on(OnWorkerStart::class, [$this, 'setWorkerName']);
|
on(OnWorkerStart::class, [$this, 'setWorkerName']);
|
||||||
on(OnTaskerStart::class, [$this, 'setTaskerName']);
|
on(OnTaskerStart::class, [$this, 'setTaskerName']);
|
||||||
|
|
||||||
if (\config('reload.hot') === false) {
|
if (\config('reload.hot') === false) {
|
||||||
$this->router->scan_build_route();
|
$this->router->scan_build_route();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user