This commit is contained in:
xl
2024-09-04 10:07:59 +08:00
parent 80b9ae8f1e
commit a756532738
+27 -73
View File
@@ -3,6 +3,7 @@
namespace Kiri\Server\Abstracts; namespace Kiri\Server\Abstracts;
use Exception; use Exception;
use Kiri\Abstracts\Component;
use Kiri\Exception\NotFindClassException; use Kiri\Exception\NotFindClassException;
use Kiri\Server\Config as SConfig; use Kiri\Server\Config as SConfig;
use Kiri\Server\Constant; use Kiri\Server\Constant;
@@ -12,13 +13,13 @@ use Kiri\Server\Handler\OnServer;
use Kiri\Server\Processes\TraitProcess; use Kiri\Server\Processes\TraitProcess;
use Kiri\Server\ServerInterface; use Kiri\Server\ServerInterface;
use Kiri\Server\Task\Task; use Kiri\Server\Task\Task;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Swoole\Server; use Swoole\Server;
use Symfony\Component\Console\Output\OutputInterface;
/** /**
*
*/ */
class AsyncServer implements ServerInterface class AsyncServer extends Component implements ServerInterface
{ {
use TraitServer; use TraitServer;
@@ -39,15 +40,15 @@ class AsyncServer implements ServerInterface
*/ */
public function initCoreServers(array $service, int $daemon = 0): void public function initCoreServers(array $service, int $daemon = 0): void
{ {
$service = $this->genConfigService($service); $service = $this->createBaseServer($this->genConfigService($service), $daemon);
$this->createBaseServer(array_pop($service), $daemon); if (isset($this->server->setting[Constant::OPTION_TASK_WORKER_NUM])) {
foreach ($service as $value) { $this->container->get(Task::class)->initTaskWorker($this->server);
$this->addListener($value);
} }
foreach ($this->_process as $process) {
$this->server->addProcess($process); foreach ($this->_process as $process) $this->server->addProcess($process);
} foreach ($service as $value) $this->addListener($value);
on(OnServerBeforeStart::class, [$this, 'onSignal']);
$this->provider->on(OnServerBeforeStart::class, [$this, 'onSignal']);
} }
@@ -59,60 +60,35 @@ class AsyncServer implements ServerInterface
{ {
$this->server->shutdown(); $this->server->shutdown();
event(new OnShutdown()); $this->dispatch->dispatch(new OnShutdown());
return true; return true;
} }
/** /**
* @param SConfig $config * @param array $service
* @param int $daemon * @param int $daemon
* @return void * @return array
* @throws * @throws NotFindClassException
*/ */
private function createBaseServer(SConfig $config, int $daemon = 0): void private function createBaseServer(array $service, int $daemon = 0): array
{ {
$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->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
*/
private function initServer($match, $config, $daemon): void
{
$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->_listenDump($config); $this->event($this->server, array_merge(\config('server.events', []), $config->events));
\Kiri::getDi()->bind(ServerInterface::class, $this->server); $this->container->bind(ServerInterface::class, $this->server);
} return $service;
/**
* @return void
* @throws
*/
private function onTaskListen(): void
{
if (!isset($this->server->setting[Constant::OPTION_TASK_WORKER_NUM])) {
return;
}
\Kiri::getDi()->get(Task::class)->initTaskWorker($this->server);
} }
@@ -146,29 +122,8 @@ class AsyncServer implements ServerInterface
if ($port === false) { if ($port === false) {
throw new Exception('Listen port fail.' . swoole_last_error()); throw new Exception('Listen port fail.' . swoole_last_error());
} }
$this->_listenDump($config);
$port->set($this->resetSettings($config->type, $config->settings)); $port->set($this->resetSettings($config->type, $config->settings));
$this->onEventListen($port, $config->getEvents()); $this->event($port, $config->getEvents());
}
/**
* @param SConfig $config
* @return void
* @throws
*/
protected function _listenDump(SConfig $config): void
{
$writeln = \Kiri::getDi()->get(OutputInterface::class);
if ($config->type == Constant::SERVER_TYPE_HTTP) {
$writeln->writeln('Add http port listen ' . $config->host . '::' . $config->port);
} else if ($config->type == Constant::SERVER_TYPE_WEBSOCKET) {
$writeln->writeln('Add wss port listen ' . $config->host . '::' . $config->port);
} else if ($config->type == Constant::SERVER_TYPE_UDP) {
$writeln->writeln('Add udp port listen ' . $config->host . '::' . $config->port);
} else {
$writeln->writeln('Add tcp port listen ' . $config->host . '::' . $config->port);
}
} }
@@ -199,11 +154,12 @@ class AsyncServer implements ServerInterface
* @return void * @return void
* @throws * @throws
*/ */
private function onEventListen(Server\Port|Server $base, array $events): void private function event(Server\Port|Server $base, array $events): void
{ {
$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] = \Kiri::getDi()->get($event[0]); $event[0] = $container->get($event[0]);
} }
$base->on($name, $event); $base->on($name, $event);
} }
@@ -216,9 +172,7 @@ class AsyncServer implements ServerInterface
*/ */
public function start(): void public function start(): void
{ {
event(new OnServerBeforeStart()); $this->dispatch->dispatch(new OnServerBeforeStart());
$this->server->start(); $this->server->start();
} }
} }