This commit is contained in:
2023-11-29 14:49:18 +08:00
parent bf930a375a
commit a0d0b6a597
2 changed files with 51 additions and 20 deletions
+42 -11
View File
@@ -13,6 +13,10 @@ use Kiri\Server\Events\OnShutdown;
use Kiri\Server\Handler\OnServer;
use Kiri\Server\ServerInterface;
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 Swoole\Server;
use Symfony\Component\Console\Output\OutputInterface;
@@ -32,11 +36,21 @@ class AsyncServer implements ServerInterface
private ?Server $server = null;
/**
* @var ContainerInterface
*/
#[Container(ContainerInterface::class)]
public ContainerInterface $container;
/**
* @param array $service
* @param int $daemon
* @return void
* @throws Exception
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFindClassException
* @throws NotFoundExceptionInterface
*/
public function initCoreServers(array $service, int $daemon = 0): void
{
@@ -71,8 +85,9 @@ class AsyncServer implements ServerInterface
* @param int $daemon
* @return void
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFindClassException
* @throws ReflectionException
* @throws NotFoundExceptionInterface
*/
private function createBaseServer(SConfig $config, int $daemon = 0): void
{
@@ -93,6 +108,8 @@ class AsyncServer implements ServerInterface
* @param $daemon
* @return void
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function initServer($match, $config, $daemon): void
{
@@ -101,7 +118,8 @@ class AsyncServer implements ServerInterface
if (!isset($config->events[Constant::SHUTDOWN])) {
$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])) {
return;
}
$container = Kiri::getDi();
$container->get(Task::class)->initTaskWorker($this->server);
$this->container->get(Task::class)->initTaskWorker($this->server);
}
@@ -142,6 +159,8 @@ class AsyncServer implements ServerInterface
/**
* @param SConfig $config
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function addListener(SConfig $config): void
@@ -150,7 +169,21 @@ class AsyncServer implements ServerInterface
if ($port === false) {
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) {
$writeln->writeln('Add http port listen ' . $config->host . '::' . $config->port . PHP_EOL);
} else if ($config->type == Constant::SERVER_TYPE_WEBSOCKET) {
@@ -160,9 +193,6 @@ class AsyncServer implements ServerInterface
} else {
$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 array $events
* @return void
* @throws ReflectionException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
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]);
$event[0] = $this->container->get($event[0]);
}
$base->on($name, $event);
}
+9 -9
View File
@@ -36,16 +36,16 @@ class ServerCommand extends Command
/**
* @param string|null $name
* @throws ReflectionException
* @throws Exception
*/
public function __construct(string $name = null)
{
parent::__construct($name);
$container = Kiri::getDi();
$this->manager = $container->get(AsyncServer::class);
$this->state = $container->get(State::class);
$container = Kiri::getDi();
$this->manager = $container->get(AsyncServer::class);
$this->state = $container->get(State::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
{
$this->setName('sw:server')
->setDescription('server start|stop|reload|restart')
->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start')
->addOption('daemon', 'd', InputOption::VALUE_NONE, 'is run daemonize');
->setDescription('server start|stop|reload|restart')
->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start')
->addOption('daemon', 'd', InputOption::VALUE_NONE, 'is run daemonize');
}
@@ -99,7 +99,7 @@ class ServerCommand extends Command
*/
protected function stop(): int
{
$configs = \config('server', []);
$configs = \config('server', []);
$instances = $this->manager->sortService($configs['ports'] ?? []);
foreach ($instances as $config) {
$this->state->exit($config->port);