2022-01-09 03:49:02 +08:00
|
|
|
<?php
|
|
|
|
|
|
2022-01-10 11:39:55 +08:00
|
|
|
namespace Kiri\Server\Handler;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
2023-04-16 02:41:03 +08:00
|
|
|
use Kiri\Di\Inject\Container;
|
2023-08-14 22:14:35 +08:00
|
|
|
use Kiri\Error\StdoutLogger;
|
2022-06-16 17:38:22 +08:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2023-08-14 21:09:44 +08:00
|
|
|
use Psr\Log\LoggerInterface;
|
2022-01-09 03:49:02 +08:00
|
|
|
use ReflectionException;
|
2022-01-10 11:39:55 +08:00
|
|
|
use Kiri\Server\Abstracts\Server;
|
|
|
|
|
use Kiri\Server\Events\OnBeforeShutdown;
|
|
|
|
|
use Kiri\Server\Events\OnShutdown;
|
|
|
|
|
use Kiri\Server\Events\OnStart;
|
2022-06-16 17:38:22 +08:00
|
|
|
use Swoole\Server as SServer;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class OnServerDefault
|
|
|
|
|
* @package Server\Manager
|
|
|
|
|
*/
|
|
|
|
|
class OnServer extends Server
|
|
|
|
|
{
|
2022-06-16 17:38:22 +08:00
|
|
|
|
2022-01-09 03:49:02 +08:00
|
|
|
|
2023-08-14 21:09:44 +08:00
|
|
|
/**
|
2023-08-14 22:14:35 +08:00
|
|
|
* @var StdoutLogger
|
2023-08-14 21:09:44 +08:00
|
|
|
*/
|
|
|
|
|
#[Container(LoggerInterface::class)]
|
2023-08-14 22:14:35 +08:00
|
|
|
public StdoutLogger $logger;
|
2023-08-14 21:09:44 +08:00
|
|
|
|
|
|
|
|
|
2023-08-11 02:35:46 +08:00
|
|
|
/**
|
|
|
|
|
* @param SServer $server
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
|
*/
|
|
|
|
|
public function onStart(SServer $server): void
|
|
|
|
|
{
|
|
|
|
|
\Kiri::setProcessName(sprintf('start[%d].server', $server->master_pid));
|
|
|
|
|
foreach (config('server.ports') as $value) {
|
2023-08-14 21:09:44 +08:00
|
|
|
$this->logger->alert('Listen ' . $value['type'] . ' address ' . $value['host'] . '::' . $value['port']);
|
2023-08-11 02:35:46 +08:00
|
|
|
}
|
|
|
|
|
event(new OnStart($server));
|
|
|
|
|
}
|
2022-06-16 17:38:22 +08:00
|
|
|
|
|
|
|
|
|
2023-08-11 02:35:46 +08:00
|
|
|
/**
|
|
|
|
|
* @param SServer $server
|
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public function onBeforeShutdown(SServer $server): void
|
|
|
|
|
{
|
|
|
|
|
event(new OnBeforeShutdown($server));
|
|
|
|
|
}
|
2022-06-16 17:38:22 +08:00
|
|
|
|
|
|
|
|
|
2023-08-11 02:35:46 +08:00
|
|
|
/**
|
|
|
|
|
* @param SServer $server
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
public function onShutdown(SServer $server): void
|
|
|
|
|
{
|
|
|
|
|
@unlink(storage('.swoole.pid'));
|
|
|
|
|
event(new OnShutdown($server));
|
|
|
|
|
}
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
}
|