This commit is contained in:
2026-04-17 11:56:44 +08:00
parent 853a9661bc
commit a9f1afb866
+135 -133
View File
@@ -23,164 +23,166 @@ use Swoole\Server;
class AsyncServer extends Component implements ServerInterface class AsyncServer extends Component implements ServerInterface
{ {
use TraitServer; use TraitServer;
use TraitProcess; use TraitProcess;
/** /**
* @var Server|null * @var Server|null
*/ */
private ?Server $server = null; private ?Server $server = null;
/** /**
* @param array $service * @param array $service
* @param int $daemon * @param int $daemon
* @return void * @return void
* @throws * @throws
*/ */
public function initCoreServers(array $service, int $daemon = 0): void public function initCoreServers(array $service, int $daemon = 0): void
{ {
$service = $this->createBaseServer($this->genConfigService($service), $daemon); $service = $this->createBaseServer($this->genConfigService($service), $daemon);
if (isset($this->server->setting[Constant::OPTION_TASK_WORKER_NUM])) { if (isset($this->server->setting[Constant::OPTION_TASK_WORKER_NUM])) {
$this->container->get(Task::class)->initTaskWorker($this->server); $this->container->get(Task::class)->initTaskWorker($this->server);
} }
foreach ($this->_process as $process) $this->server->addProcess($process); foreach ($this->_process as $process)
foreach ($service as $value) $this->addListener($value); $this->server->addProcess($process);
foreach ($service as $value)
$this->addListener($value);
$this->provider->on(OnServerBeforeStart::class, [$this, 'onSignal']); $this->provider->on(OnServerBeforeStart::class, [$this, 'onSignal']);
} }
/** /**
* @return bool * @return bool
* @throws * @throws
*/ */
public function shutdown(): bool public function shutdown(): bool
{ {
$this->server->shutdown(); $this->server->shutdown();
$this->dispatch->dispatch(new OnShutdown()); $this->dispatch->dispatch(new OnShutdown);
return true; return true;
} }
/** /**
* @param array $service * @param array $service
* @param int $daemon * @param int $daemon
* @return array * @return array
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFindClassException * @throws NotFindClassException
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
private function createBaseServer(array $service, int $daemon = 0): array private function createBaseServer(array $service, int $daemon = 0): array
{ {
$config = array_pop($service); $config = array_pop($service);
$match = $this->getServerClass($config->type); $match = $this->getServerClass($config->type);
if (is_null($match)) { if (is_null($match)) {
throw new NotFindClassException('Unknown server type ' . $config->type); throw new NotFindClassException('Unknown server type ' . $config->type);
} }
$this->container->get(StdoutLogger::class)->println('Listen address ' . $config->host . '::' . $config->port); $this->container->get(StdoutLogger::class)->println('Listen address ' . $config->type . ' ' . $config->host . '::' . $config->port);
$this->server = new $match($config->host, $config->port, $config->mode, $config->socket); $this->server = new $match($config->host, $config->port, $config->mode, $config->socket);
$this->server->set($this->systemConfig($config, $daemon)); $this->server->set($this->systemConfig($config, $daemon));
if (!isset($config->events[Constant::SHUTDOWN])) { if (!isset($config->events[Constant::SHUTDOWN])) {
$config->events[Constant::SHUTDOWN] = [OnServer::class, 'onShutdown']; $config->events[Constant::SHUTDOWN] = [OnServer::class, 'onShutdown'];
} }
$this->event($this->server, array_merge(\config('servers.server.events', []), $config->events)); $this->event($this->server, array_merge(\config('servers.server.events', []), $config->events));
$this->container->bind(ServerInterface::class, $this->server); $this->container->bind(ServerInterface::class, $this->server);
return $service; return $service;
} }
/** /**
* @param SConfig $config * @param SConfig $config
* @param int $daemon * @param int $daemon
* @return array * @return array
* @throws * @throws
*/ */
protected function systemConfig(SConfig $config, int $daemon): array protected function systemConfig(SConfig $config, int $daemon): array
{ {
$settings = array_merge(\config('servers.server.settings', []), $config->settings); $settings = array_merge(\config('servers.server.settings', []), $config->settings);
$settings[Constant::OPTION_DAEMONIZE] = (bool)$daemon; $settings[Constant::OPTION_DAEMONIZE] = (bool)$daemon;
$settings[Constant::OPTION_ENABLE_REUSE_PORT] = true; $settings[Constant::OPTION_ENABLE_REUSE_PORT] = true;
$settings[Constant::OPTION_PID_FILE] = storage('.swoole.pid'); $settings[Constant::OPTION_PID_FILE] = storage('.swoole.pid');
if (!isset($settings[Constant::OPTION_PID_FILE])) { if (!isset($settings[Constant::OPTION_PID_FILE])) {
$settings[Constant::OPTION_LOG_FILE] = storage('system.log'); $settings[Constant::OPTION_LOG_FILE] = storage('system.log');
} }
return $settings; return $settings;
} }
/** /**
* @param SConfig $config * @param SConfig $config
* @return void * @return void
* @throws * @throws
*/ */
public function addListener(SConfig $config): void public function addListener(SConfig $config): void
{ {
$port = $this->server->addlistener($config->host, $config->port, $config->socket); $port = $this->server->addlistener($config->host, $config->port, $config->socket);
if ($port === false) { if ($port === false) {
throw new Exception('Listen port fail.' . swoole_last_error()); throw new Exception('Listen port fail.' . swoole_last_error());
} }
$port->set($this->resetSettings($config->type, $config->settings)); $port->set($this->resetSettings($config->type, $config->settings));
$this->event($port, $config->getEvents()); $this->event($port, $config->getEvents());
$this->container->get(StdoutLogger::class)->println('Listen address ' . $config->host . '::' . $config->port); $this->container->get(StdoutLogger::class)->println('Listen address ' . $config->host . '::' . $config->port);
} }
/** /**
* @param string $type * @param string $type
* @param array $settings * @param array $settings
* @return array * @return array
* @throws * @throws
*/ */
private function resetSettings(string $type, array $settings): array private function resetSettings(string $type, array $settings): array
{ {
if ($type == Constant::SERVER_TYPE_HTTP && !isset($settings['open_http_protocol'])) { if ($type == Constant::SERVER_TYPE_HTTP && !isset($settings['open_http_protocol'])) {
$settings['open_http_protocol'] = true; $settings['open_http_protocol'] = true;
if (in_array($this->server->setting['dispatch_mode'], [2, 4])) { if (in_array($this->server->setting['dispatch_mode'], [2, 4])) {
$settings['open_http2_protocol'] = true; $settings['open_http2_protocol'] = true;
} }
} }
if ($type == Constant::SERVER_TYPE_WEBSOCKET && !isset($settings['open_websocket_protocol'])) { if ($type == Constant::SERVER_TYPE_WEBSOCKET && !isset($settings['open_websocket_protocol'])) {
$settings['open_websocket_protocol'] = true; $settings['open_websocket_protocol'] = true;
} }
return $settings; return $settings;
} }
/** /**
* @param Server\Port|Server $base * @param Server\Port|Server $base
* @param array $events * @param array $events
* @return void * @return void
* @throws * @throws
*/ */
private function event(Server\Port|Server $base, array $events): void private function event(Server\Port|Server $base, array $events): void
{ {
$container = $this->container; $container = $this->container;
foreach ($events as $name => $event) { foreach ($events as $name => $event) {
if (is_array($event) && is_string($event[0])) { if (is_array($event) && is_string($event[0])) {
$event[0] = $container->get($event[0]); $event[0] = $container->get($event[0]);
} }
$base->on($name, $event); $base->on($name, $event);
} }
} }
/** /**
* @return void * @return void
* @throws * @throws
*/ */
public function start(): void public function start(): void
{ {
$this->dispatch->dispatch(new OnServerBeforeStart()); $this->dispatch->dispatch(new OnServerBeforeStart);
$this->server->start(); $this->server->start();
} }
} }