Files
kiri-core/HttpServer/Server.php
T

153 lines
3.1 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
namespace HttpServer;
2021-04-27 16:42:44 +08:00
use Exception;
2021-02-20 17:30:45 +08:00
use HttpServer\Abstracts\HttpService;
2021-07-20 11:24:15 +08:00
use JetBrains\PhpStorm\Pure;
use ReflectionException;
2021-07-20 01:37:16 +08:00
use Rpc\Service;
use Server\Constant;
use Server\ServerManager;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\Config;
use Kiri\Error\LoggerProcess;
use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Process\Biomonitoring;
use Kiri\Kiri;
2020-09-02 18:53:55 +08:00
use Swoole\Runtime;
2020-08-31 01:27:08 +08:00
2021-03-01 15:09:36 +08:00
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
2020-08-31 01:27:08 +08:00
/**
* Class Server
* @package HttpServer
*/
2021-02-20 17:30:45 +08:00
class Server extends HttpService
2020-08-31 01:27:08 +08:00
{
2021-03-26 01:05:07 +08:00
2021-07-21 15:37:33 +08:00
private array $process = [
Biomonitoring::class,
LoggerProcess::class
];
2021-07-03 21:12:21 +08:00
2021-07-21 15:37:33 +08:00
private ServerManager $manager;
2021-08-12 15:39:33 +08:00
private mixed $daemon = 0;
2021-07-03 21:12:21 +08:00
2021-07-21 15:37:33 +08:00
/**
*
*/
public function init()
{
$this->manager = ServerManager::getContext();
}
2021-07-03 21:12:21 +08:00
2021-07-20 11:29:18 +08:00
/**
* @param $process
*/
2021-07-21 15:37:33 +08:00
public function addProcess($process)
{
$this->process[] = $process;
}
/**
* @return string start server
*
* start server
* @throws ConfigException
* @throws Exception
*/
public function start(): string
{
$this->manager->initBaseServer(Config::get('server', [], true));
$rpcService = Config::get('rpc', []);
if (!empty($rpcService)) {
$this->rpcListener($rpcService);
}
$processes = array_merge($this->process, Config::get('processes', []));
foreach ($processes as $process) {
$this->manager->addProcess($process);
}
Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION);
return $this->manager->getServer()->start();
}
/**
* @param $rpcService
* @throws ReflectionException
* @throws NotFindClassException
2021-08-02 11:07:15 +08:00
* @throws ConfigException
2021-07-21 15:37:33 +08:00
*/
private function rpcListener($rpcService)
{
2021-07-21 17:55:34 +08:00
if (in_array($rpcService['mode'], [SWOOLE_SOCK_UDP, SWOOLE_UDP, SWOOLE_UDP6, SWOOLE_SOCK_UDP6])) {
$rpcService['events'][Constant::PACKET] = [Service::class, 'onPacket'];
} else {
$rpcService['events'][Constant::RECEIVE] = [Service::class, 'onReceive'];
$rpcService['events'][Constant::CONNECT] = [Service::class, 'onConnect'];
$rpcService['events'][Constant::DISCONNECT] = [Service::class, 'onDisconnect'];
$rpcService['events'][Constant::CLOSE] = [Service::class, 'onClose'];
}
2021-08-02 11:07:15 +08:00
$rpcService['settings']['enable_unsafe_event'] = true;
$this->addRpcListener($rpcService);
}
/**
* @param $rpcService
* @throws ConfigException
* @throws NotFindClassException
* @throws ReflectionException
*/
private function addRpcListener($rpcService)
{
2021-07-21 15:37:33 +08:00
$this->manager->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'], $rpcService['mode'], $rpcService);
}
/**
* @return void
*
* start server
* @throws Exception
*/
public function shutdown()
{
2021-08-12 15:39:33 +08:00
$this->manager->stopServer(0);
2021-07-21 15:37:33 +08:00
}
/**
* @param $daemon
* @return Server
*/
public function setDaemon($daemon): static
{
if (!in_array($daemon, [0, 1])) {
return $this;
}
$this->daemon = $daemon;
return $this;
}
/**
* @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
*/
#[Pure] public function getServer(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
{
return $this->manager->getServer();
}
2021-07-03 21:12:21 +08:00
2020-08-31 01:27:08 +08:00
}