eee
This commit is contained in:
+27
-73
@@ -3,6 +3,7 @@
|
||||
namespace Kiri\Server\Abstracts;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Server\Config as SConfig;
|
||||
use Kiri\Server\Constant;
|
||||
@@ -12,13 +13,13 @@ use Kiri\Server\Handler\OnServer;
|
||||
use Kiri\Server\Processes\TraitProcess;
|
||||
use Kiri\Server\ServerInterface;
|
||||
use Kiri\Server\Task\Task;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Swoole\Server;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class AsyncServer implements ServerInterface
|
||||
class AsyncServer extends Component implements ServerInterface
|
||||
{
|
||||
|
||||
use TraitServer;
|
||||
@@ -39,15 +40,15 @@ class AsyncServer implements ServerInterface
|
||||
*/
|
||||
public function initCoreServers(array $service, int $daemon = 0): void
|
||||
{
|
||||
$service = $this->genConfigService($service);
|
||||
$this->createBaseServer(array_pop($service), $daemon);
|
||||
foreach ($service as $value) {
|
||||
$this->addListener($value);
|
||||
$service = $this->createBaseServer($this->genConfigService($service), $daemon);
|
||||
if (isset($this->server->setting[Constant::OPTION_TASK_WORKER_NUM])) {
|
||||
$this->container->get(Task::class)->initTaskWorker($this->server);
|
||||
}
|
||||
foreach ($this->_process as $process) {
|
||||
$this->server->addProcess($process);
|
||||
}
|
||||
on(OnServerBeforeStart::class, [$this, 'onSignal']);
|
||||
|
||||
foreach ($this->_process as $process) $this->server->addProcess($process);
|
||||
foreach ($service as $value) $this->addListener($value);
|
||||
|
||||
$this->provider->on(OnServerBeforeStart::class, [$this, 'onSignal']);
|
||||
}
|
||||
|
||||
|
||||
@@ -59,60 +60,35 @@ class AsyncServer implements ServerInterface
|
||||
{
|
||||
$this->server->shutdown();
|
||||
|
||||
event(new OnShutdown());
|
||||
$this->dispatch->dispatch(new OnShutdown());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param SConfig $config
|
||||
* @param array $service
|
||||
* @param int $daemon
|
||||
* @return void
|
||||
* @throws
|
||||
* @return array
|
||||
* @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);
|
||||
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
|
||||
*/
|
||||
private function initServer($match, $config, $daemon): void
|
||||
{
|
||||
$this->server = new $match($config->host, $config->port, $config->mode, $config->socket);
|
||||
$this->server->set($this->systemConfig($config, $daemon));
|
||||
if (!isset($config->events[Constant::SHUTDOWN])) {
|
||||
$config->events[Constant::SHUTDOWN] = [OnServer::class, 'onShutdown'];
|
||||
}
|
||||
$this->_listenDump($config);
|
||||
\Kiri::getDi()->bind(ServerInterface::class, $this->server);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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);
|
||||
$this->event($this->server, array_merge(\config('server.events', []), $config->events));
|
||||
$this->container->bind(ServerInterface::class, $this->server);
|
||||
return $service;
|
||||
}
|
||||
|
||||
|
||||
@@ -146,29 +122,8 @@ class AsyncServer implements ServerInterface
|
||||
if ($port === false) {
|
||||
throw new Exception('Listen port fail.' . swoole_last_error());
|
||||
}
|
||||
$this->_listenDump($config);
|
||||
$port->set($this->resetSettings($config->type, $config->settings));
|
||||
$this->onEventListen($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);
|
||||
}
|
||||
$this->event($port, $config->getEvents());
|
||||
}
|
||||
|
||||
|
||||
@@ -199,11 +154,12 @@ class AsyncServer implements ServerInterface
|
||||
* @return void
|
||||
* @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) {
|
||||
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);
|
||||
}
|
||||
@@ -216,9 +172,7 @@ class AsyncServer implements ServerInterface
|
||||
*/
|
||||
public function start(): void
|
||||
{
|
||||
event(new OnServerBeforeStart());
|
||||
$this->dispatch->dispatch(new OnServerBeforeStart());
|
||||
$this->server->start();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user