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-04-16 02:32:36 +08:00
|
|
|
use ReflectionException;
|
2023-04-01 23:12:58 +08:00
|
|
|
use Swoole\Coroutine;
|
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
|
|
|
|
|
|
|
|
|
2022-06-16 17:49:06 +08:00
|
|
|
private array $_process = [];
|
2023-04-22 02:04:31 +08:00
|
|
|
|
|
|
|
|
|
2022-06-16 17:49:06 +08:00
|
|
|
/**
|
2022-09-25 04:19:00 +08:00
|
|
|
* @param string|array|BaseProcess $class
|
2022-06-16 17:49:06 +08:00
|
|
|
* @return void
|
2022-10-11 15:15:04 +08:00
|
|
|
* @throws Exception
|
2022-06-16 17:49:06 +08:00
|
|
|
*/
|
2022-09-25 04:19:00 +08:00
|
|
|
public function addProcess(string|array|BaseProcess $class): void
|
2022-06-16 17:49:06 +08:00
|
|
|
{
|
2022-10-11 15:15:04 +08:00
|
|
|
if (!is_array($class)) {
|
|
|
|
|
$class = [$class];
|
|
|
|
|
}
|
|
|
|
|
foreach ($class as $name) {
|
2023-04-22 02:04:31 +08:00
|
|
|
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);
|
2022-06-22 16:50:37 +08:00
|
|
|
}
|
2022-06-16 17:49:06 +08:00
|
|
|
}
|
2023-04-22 02:04:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-01 22:36:17 +08:00
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-22 02:04:31 +08:00
|
|
|
public function onSignal(): void
|
2023-04-01 22:36:17 +08:00
|
|
|
{
|
2023-04-22 02:04:31 +08:00
|
|
|
$signal = \Kiri\Abstracts\Config::get('signal', []);
|
2023-04-01 23:12:58 +08:00
|
|
|
$this->onPcntlSignal(SIGINT, [$this, 'onSigint']);
|
2023-04-01 22:36:17 +08:00
|
|
|
foreach ($signal as $sig => $value) {
|
|
|
|
|
if (is_array($value) && is_string($value[0])) {
|
2023-04-22 02:04:31 +08:00
|
|
|
$value[0] = \Kiri::getDi()->get($value[0]);
|
2023-04-01 22:36:17 +08:00
|
|
|
}
|
|
|
|
|
if (!is_callable($value, true)) {
|
|
|
|
|
throw new Exception('Register signal callback must can exec.');
|
|
|
|
|
}
|
2023-04-01 23:12:58 +08:00
|
|
|
$this->onPcntlSignal($sig, $value);
|
2023-04-01 22:36:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-22 02:04:31 +08:00
|
|
|
|
|
|
|
|
|
2023-04-01 23:12:58 +08:00
|
|
|
/**
|
|
|
|
|
* @param $signal
|
|
|
|
|
* @param $callback
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function onPcntlSignal($signal, $callback): void
|
|
|
|
|
{
|
2023-04-22 02:04:31 +08:00
|
|
|
// if (get_called_class() != CoroutineServer::class) {
|
|
|
|
|
pcntl_signal(SIGINT, [$this, 'onSigint']);
|
|
|
|
|
// } else {
|
|
|
|
|
// Coroutine::create(static function () use ($signal, $callback) {
|
|
|
|
|
// $data = Coroutine::waitSignal($signal);
|
|
|
|
|
// if ($data) {
|
|
|
|
|
// $callback($signal, [true]);
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// }
|
2023-04-01 23:12:58 +08:00
|
|
|
}
|
2023-04-22 02:04:31 +08:00
|
|
|
|
|
|
|
|
|
2022-06-16 17:49:06 +08:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getProcess(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->_process;
|
|
|
|
|
}
|
2023-04-22 02:04:31 +08:00
|
|
|
|
|
|
|
|
|
2022-06-16 17:49:06 +08:00
|
|
|
/**
|
|
|
|
|
* @param array $ports
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function sortService(array $ports): array
|
|
|
|
|
{
|
|
|
|
|
$array = [];
|
|
|
|
|
foreach ($ports as $port) {
|
|
|
|
|
if ($port['type'] == Constant::SERVER_TYPE_WEBSOCKET) {
|
|
|
|
|
array_unshift($array, $port);
|
|
|
|
|
} else if ($port['type'] == Constant::SERVER_TYPE_HTTP) {
|
|
|
|
|
if (!empty($array) && $array[0]['type'] == Constant::SERVER_TYPE_WEBSOCKET) {
|
|
|
|
|
$array[] = $port;
|
|
|
|
|
} else {
|
|
|
|
|
array_unshift($array, $port);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$array[] = $port;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
2023-04-16 02:32:36 +08:00
|
|
|
|
|
|
|
|
|
2022-06-16 17:49:06 +08:00
|
|
|
/**
|
|
|
|
|
* @param array $ports
|
|
|
|
|
* @return array<Config>
|
2023-04-16 02:32:36 +08:00
|
|
|
* @throws ReflectionException
|
2022-06-16 17:49:06 +08:00
|
|
|
*/
|
|
|
|
|
public function genConfigService(array $ports): array
|
|
|
|
|
{
|
|
|
|
|
$array = [];
|
2022-06-16 18:00:11 +08:00
|
|
|
$ports = $ports['ports'] ?? [];
|
2022-06-16 17:49:06 +08:00
|
|
|
foreach ($ports as $port) {
|
2023-04-16 02:32:36 +08:00
|
|
|
$config = \Kiri::getDi()->make(Config::class, [], $port);
|
2022-06-16 17:49:06 +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;
|
|
|
|
|
}
|
2023-04-22 02:04:31 +08:00
|
|
|
|
|
|
|
|
|
2022-06-16 17:49:06 +08:00
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-04-22 02:04:31 +08:00
|
|
|
|
|
|
|
|
|
2022-06-16 17:49:06 +08:00
|
|
|
/**
|
|
|
|
|
* @param $type
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function getCoroutineServerClass($type): ?string
|
|
|
|
|
{
|
|
|
|
|
return match ($type) {
|
2023-04-21 22:26:43 +08:00
|
|
|
Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP, Constant::SERVER_TYPE_UDP => Coroutine\Server::class,
|
|
|
|
|
Constant::SERVER_TYPE_HTTP, Constant::SERVER_TYPE_WEBSOCKET => Coroutine\Http\Server::class,
|
2022-06-16 17:49:06 +08:00
|
|
|
default => null
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-04-22 02:04:31 +08:00
|
|
|
|
|
|
|
|
|
2022-06-16 17:49:06 +08:00
|
|
|
}
|