eee
This commit is contained in:
+42
-11
@@ -13,6 +13,10 @@ use Kiri\Server\Events\OnShutdown;
|
|||||||
use Kiri\Server\Handler\OnServer;
|
use Kiri\Server\Handler\OnServer;
|
||||||
use Kiri\Server\ServerInterface;
|
use Kiri\Server\ServerInterface;
|
||||||
use Kiri\Server\Task\Task;
|
use Kiri\Server\Task\Task;
|
||||||
|
use Kiri\Di\Inject\Container;
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
@@ -32,11 +36,21 @@ class AsyncServer implements ServerInterface
|
|||||||
private ?Server $server = null;
|
private ?Server $server = null;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ContainerInterface
|
||||||
|
*/
|
||||||
|
#[Container(ContainerInterface::class)]
|
||||||
|
public ContainerInterface $container;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $service
|
* @param array $service
|
||||||
* @param int $daemon
|
* @param int $daemon
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws ConfigException
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFindClassException
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
public function initCoreServers(array $service, int $daemon = 0): void
|
public function initCoreServers(array $service, int $daemon = 0): void
|
||||||
{
|
{
|
||||||
@@ -71,8 +85,9 @@ class AsyncServer implements ServerInterface
|
|||||||
* @param int $daemon
|
* @param int $daemon
|
||||||
* @return void
|
* @return void
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
private function createBaseServer(SConfig $config, int $daemon = 0): void
|
private function createBaseServer(SConfig $config, int $daemon = 0): void
|
||||||
{
|
{
|
||||||
@@ -93,6 +108,8 @@ class AsyncServer implements ServerInterface
|
|||||||
* @param $daemon
|
* @param $daemon
|
||||||
* @return void
|
* @return void
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
private function initServer($match, $config, $daemon): void
|
private function initServer($match, $config, $daemon): void
|
||||||
{
|
{
|
||||||
@@ -101,7 +118,8 @@ class AsyncServer implements ServerInterface
|
|||||||
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'];
|
||||||
}
|
}
|
||||||
Kiri::getDi()->bind(ServerInterface::class, $this->server);
|
$this->_listenDump($config);
|
||||||
|
$this->container->bind(ServerInterface::class, $this->server);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -114,8 +132,7 @@ class AsyncServer implements ServerInterface
|
|||||||
if (!isset($this->server->setting[Constant::OPTION_TASK_WORKER_NUM])) {
|
if (!isset($this->server->setting[Constant::OPTION_TASK_WORKER_NUM])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$container = Kiri::getDi();
|
$this->container->get(Task::class)->initTaskWorker($this->server);
|
||||||
$container->get(Task::class)->initTaskWorker($this->server);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -142,6 +159,8 @@ class AsyncServer implements ServerInterface
|
|||||||
/**
|
/**
|
||||||
* @param SConfig $config
|
* @param SConfig $config
|
||||||
* @return void
|
* @return void
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function addListener(SConfig $config): void
|
public function addListener(SConfig $config): void
|
||||||
@@ -150,7 +169,21 @@ 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());
|
||||||
}
|
}
|
||||||
$writeln = Kiri::getDi()->get(OutputInterface::class);
|
$this->_listenDump($config);
|
||||||
|
$port->set($this->resetSettings($config->type, $config->settings));
|
||||||
|
$this->onEventListen($port, $config->getEvents());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param SConfig $config
|
||||||
|
* @return void
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
|
protected function _listenDump(SConfig $config): void
|
||||||
|
{
|
||||||
|
$writeln = $this->container->get(OutputInterface::class);
|
||||||
if ($config->type == Constant::SERVER_TYPE_HTTP) {
|
if ($config->type == Constant::SERVER_TYPE_HTTP) {
|
||||||
$writeln->writeln('Add http port listen ' . $config->host . '::' . $config->port . PHP_EOL);
|
$writeln->writeln('Add http port listen ' . $config->host . '::' . $config->port . PHP_EOL);
|
||||||
} else if ($config->type == Constant::SERVER_TYPE_WEBSOCKET) {
|
} else if ($config->type == Constant::SERVER_TYPE_WEBSOCKET) {
|
||||||
@@ -160,9 +193,6 @@ class AsyncServer implements ServerInterface
|
|||||||
} else {
|
} else {
|
||||||
$writeln->writeln('Add tcp port listen ' . $config->host . '::' . $config->port . PHP_EOL);
|
$writeln->writeln('Add tcp port listen ' . $config->host . '::' . $config->port . PHP_EOL);
|
||||||
}
|
}
|
||||||
$port->set($this->resetSettings($config->type, $config->settings));
|
|
||||||
|
|
||||||
$this->onEventListen($port, $config->getEvents());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -191,13 +221,14 @@ class AsyncServer implements ServerInterface
|
|||||||
* @param Server\Port|Server $base
|
* @param Server\Port|Server $base
|
||||||
* @param array $events
|
* @param array $events
|
||||||
* @return void
|
* @return void
|
||||||
* @throws ReflectionException
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
private function onEventListen(Server\Port|Server $base, array $events): void
|
private function onEventListen(Server\Port|Server $base, array $events): void
|
||||||
{
|
{
|
||||||
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] = $this->container->get($event[0]);
|
||||||
}
|
}
|
||||||
$base->on($name, $event);
|
$base->on($name, $event);
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-9
@@ -36,16 +36,16 @@ class ServerCommand extends Command
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|null $name
|
* @param string|null $name
|
||||||
* @throws ReflectionException
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(string $name = null)
|
public function __construct(string $name = null)
|
||||||
{
|
{
|
||||||
parent::__construct($name);
|
parent::__construct($name);
|
||||||
$container = Kiri::getDi();
|
$container = Kiri::getDi();
|
||||||
$this->manager = $container->get(AsyncServer::class);
|
$this->manager = $container->get(AsyncServer::class);
|
||||||
$this->state = $container->get(State::class);
|
$this->state = $container->get(State::class);
|
||||||
$this->dispatch = $container->get(EventDispatch::class);
|
$this->dispatch = $container->get(EventDispatch::class);
|
||||||
$this->router = $container->get(Router::class);
|
$this->router = $container->get(Router::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -55,9 +55,9 @@ class ServerCommand extends Command
|
|||||||
protected function configure(): void
|
protected function configure(): void
|
||||||
{
|
{
|
||||||
$this->setName('sw:server')
|
$this->setName('sw:server')
|
||||||
->setDescription('server start|stop|reload|restart')
|
->setDescription('server start|stop|reload|restart')
|
||||||
->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start')
|
->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start')
|
||||||
->addOption('daemon', 'd', InputOption::VALUE_NONE, 'is run daemonize');
|
->addOption('daemon', 'd', InputOption::VALUE_NONE, 'is run daemonize');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -99,7 +99,7 @@ class ServerCommand extends Command
|
|||||||
*/
|
*/
|
||||||
protected function stop(): int
|
protected function stop(): int
|
||||||
{
|
{
|
||||||
$configs = \config('server', []);
|
$configs = \config('server', []);
|
||||||
$instances = $this->manager->sortService($configs['ports'] ?? []);
|
$instances = $this->manager->sortService($configs['ports'] ?? []);
|
||||||
foreach ($instances as $config) {
|
foreach ($instances as $config) {
|
||||||
$this->state->exit($config->port);
|
$this->state->exit($config->port);
|
||||||
|
|||||||
Reference in New Issue
Block a user