qqq
This commit is contained in:
+146
-151
@@ -17,175 +17,170 @@ trait TraitServer
|
||||
{
|
||||
|
||||
|
||||
private array $_process = [];
|
||||
private array $_process = [];
|
||||
|
||||
|
||||
/**
|
||||
* @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 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());
|
||||
}
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $signal
|
||||
* @param $callback
|
||||
* @return void
|
||||
*/
|
||||
private function onPcntlSignal($signal, $callback): void
|
||||
{
|
||||
// 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]);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
}
|
||||
/**
|
||||
* @param $signal
|
||||
* @param $callback
|
||||
* @return void
|
||||
*/
|
||||
private function onPcntlSignal($signal, $callback): void
|
||||
{
|
||||
pcntl_signal(SIGINT, [$this, 'onSigint']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getProcess(): array
|
||||
{
|
||||
return $this->_process;
|
||||
}
|
||||
/**
|
||||
* @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
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function sortService(array $ports): array
|
||||
{
|
||||
$array = [];
|
||||
foreach ($ports as $port) {
|
||||
$array = $this->sort($array, $port);
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $ports
|
||||
* @return array<Config>
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function genConfigService(array $ports): array
|
||||
{
|
||||
$array = [];
|
||||
$ports = $ports['ports'] ?? [];
|
||||
foreach ($ports as $port) {
|
||||
$config = \Kiri::getDi()->make(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 array $ports
|
||||
* @return array<Config>
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function genConfigService(array $ports): array
|
||||
{
|
||||
$array = [];
|
||||
$ports = $ports['ports'] ?? [];
|
||||
foreach ($ports as $port) {
|
||||
$array = $this->sort($array, $port);
|
||||
}
|
||||
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 array $array
|
||||
* @param $port
|
||||
* @return array
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function sort(array $array, $port): array
|
||||
{
|
||||
$config = created(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 getCoroutineServerClass($type): ?string
|
||||
{
|
||||
return match ($type) {
|
||||
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,
|
||||
default => null
|
||||
};
|
||||
}
|
||||
/**
|
||||
* @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) {
|
||||
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,
|
||||
default => null
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Kiri\Di\Inject\Service;
|
||||
use const Kiri\Router\ROUTER_TYPE_HTTP;
|
||||
|
||||
class OnRequest implements OnRequestInterface
|
||||
|
||||
Reference in New Issue
Block a user