2022-06-16 17:49:06 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Server\Abstracts;
|
|
|
|
|
|
|
|
|
|
use Swoole\Http\Server as HServer;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private array $_process = [];
|
2022-09-25 04:19:00 +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-09-25 04:19:00 +08:00
|
|
|
public function addProcess(string|array|BaseProcess $class): void
|
2022-06-16 17:49:06 +08:00
|
|
|
{
|
2022-09-25 17:02:49 +08:00
|
|
|
if (is_object($class)) {
|
|
|
|
|
$this->_process[] = $class;
|
|
|
|
|
} else if (is_string($class)) {
|
2022-06-23 10:44:06 +08:00
|
|
|
$this->_process[] = $class;
|
|
|
|
|
} else {
|
|
|
|
|
foreach ($class as $name) {
|
|
|
|
|
$this->_process[] = $name;
|
|
|
|
|
}
|
2022-06-22 16:50:37 +08:00
|
|
|
}
|
2022-06-16 17:49:06 +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) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $ports
|
|
|
|
|
* @return array<Config>
|
|
|
|
|
*/
|
|
|
|
|
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) {
|
|
|
|
|
$config = \Kiri::getDi()->create(Config::class, [], $port);
|
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $type
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function getCoroutineServerClass($type): ?string
|
|
|
|
|
{
|
|
|
|
|
return match ($type) {
|
2022-06-20 17:33:27 +08:00
|
|
|
Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP, Constant::SERVER_TYPE_UDP => \Swoole\Coroutine\Server::class,
|
2022-06-16 17:49:06 +08:00
|
|
|
Constant::SERVER_TYPE_HTTP, Constant::SERVER_TYPE_WEBSOCKET => \Swoole\Coroutine\Http\Server::class,
|
|
|
|
|
default => null
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|