Files
kiri-http-server/Abstracts/TraitServer.php
T

194 lines
4.5 KiB
PHP
Raw Normal View History

2022-06-16 17:49:06 +08:00
<?php
namespace Kiri\Server\Abstracts;
2022-10-11 15:15:04 +08:00
use Exception;
2023-04-22 02:04:31 +08:00
use Kiri;
2023-08-14 21:09:44 +08:00
use Kiri\Di\Inject\Container;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
2022-06-16 17:49:06 +08:00
use Swoole\Http\Server as HServer;
2023-04-22 02:04:31 +08:00
use Swoole\Process;
2022-06-16 17:49:06 +08:00
use Swoole\Server;
2022-06-16 17:51:22 +08:00
use Kiri\Server\Constant;
use Kiri\Server\Config;
2022-06-16 17:49:06 +08:00
use Swoole\WebSocket\Server as WServer;
trait TraitServer
{
2023-04-22 02:04:31 +08:00
2023-05-26 11:16:43 +08:00
private array $_process = [];
2023-08-14 21:09:44 +08:00
/**
* @var Logger
*/
#[Container(LoggerInterface::class)]
public Logger $logger;
2023-05-26 11:16:43 +08:00
/**
* @param string|array|BaseProcess $class
* @return void
* @throws Exception
*/
public function addProcess(string|array|BaseProcess $class): void
{
if (!is_array($class)) {
$class = [$class];
}
foreach ($class as $name) {
if (is_string($name)) {
$name = Kiri::getDi()->get($name);
}
if (isset($this->_process[$name->getName()])) {
throw new Exception('Process(' . $name->getName() . ') is exists.');
}
$this->_process[$name->getName()] = $this->genProcess($name);
}
}
/**
* @param BaseProcess $name
* @return Process
*/
private function genProcess(BaseProcess $name): Process
{
return new Process(function (Process $process) use ($name) {
$process->name($name->getName());
$name->onSigterm()->process($process);
},
$name->getRedirectStdinAndStdout(),
$name->getPipeType(),
$name->isEnableCoroutine());
}
/**
* @return void
* @throws Exception
*/
public function onSignal(): void
{
$signal = \config('signal', []);
$this->onPcntlSignal(SIGINT, [$this, 'onSigint']);
foreach ($signal as $sig => $value) {
if (is_array($value) && is_string($value[0])) {
$value[0] = \Kiri::getDi()->get($value[0]);
}
if (!is_callable($value, true)) {
throw new Exception('Register signal callback must can exec.');
}
$this->onPcntlSignal($sig, $value);
}
}
2023-08-11 00:12:32 +08:00
/**
* @param $no
* @param array $signInfo
* @return void
*/
public function onSigint($no, array $signInfo): void
{
try {
2023-08-14 21:09:44 +08:00
$this->logger->alert('Pid ' . getmypid() . ' get signo ' . $no);
2023-08-11 00:12:32 +08:00
$this->shutdown();
} catch (\Throwable $exception) {
error($exception);
}
}
2023-05-26 11:16:43 +08:00
/**
* @param $signal
* @param $callback
* @return void
*/
private function onPcntlSignal($signal, $callback): void
{
2023-07-26 17:48:34 +08:00
pcntl_signal($signal, $callback);
2023-05-26 11:16:43 +08:00
}
/**
* @return array
*/
public function getProcess(): array
{
return $this->_process;
}
/**
* @param array $ports
* @return array
*/
public function sortService(array $ports): array
{
$array = [];
foreach ($ports as $port) {
$array = $this->sort($array, $port);
}
return $array;
}
/**
* @param array $ports
* @return array<Config>
*/
public function genConfigService(array $ports): array
{
$array = [];
$ports = $ports['ports'] ?? [];
foreach ($ports as $port) {
$array = $this->sort($array, $port);
}
return $array;
}
/**
* @param array $array
* @param $port
* @return array
*/
private function sort(array $array, $port): array
{
2023-05-26 11:21:36 +08:00
$config = instance(Config::class, [], $port);
2023-05-26 11:16:43 +08:00
if ($port['type'] == Constant::SERVER_TYPE_WEBSOCKET) {
array_unshift($array, $config);
} else if ($port['type'] == Constant::SERVER_TYPE_HTTP) {
if (!empty($array) && $array[0]['type'] == Constant::SERVER_TYPE_WEBSOCKET) {
$array[] = $config;
} else {
array_unshift($array, $config);
}
} else {
$array[] = $config;
}
return $array;
}
/**
* @param $type
* @return string|null
*/
public function getServerClass($type): ?string
{
return match ($type) {
Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP,
Constant::SERVER_TYPE_UDP => Server::class,
Constant::SERVER_TYPE_HTTP => HServer::class,
Constant::SERVER_TYPE_WEBSOCKET => WServer::class,
default => null
};
}
2022-06-16 17:49:06 +08:00
}