This commit is contained in:
2021-07-21 15:37:33 +08:00
parent 8093c6ab3b
commit 8c221e9e0e
+131 -129
View File
@@ -32,161 +32,163 @@ defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
class Server extends HttpService class Server extends HttpService
{ {
private array $process = [ private array $process = [
Biomonitoring::class, Biomonitoring::class,
LoggerProcess::class LoggerProcess::class
]; ];
private ServerManager $manager; private ServerManager $manager;
/** /**
* *
*/ */
public function init() public function init()
{ {
$this->manager = ServerManager::getContext(); $this->manager = ServerManager::getContext();
} }
/** /**
* @param $process * @param $process
*/ */
public function addProcess($process) public function addProcess($process)
{ {
$this->process[] = $process; $this->process[] = $process;
} }
/** /**
* @return string start server * @return string start server
* *
* start server * start server
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public function start(): string public function start(): string
{ {
$this->manager->initBaseServer(Config::get('server', [], true)); $this->manager->initBaseServer(Config::get('server', [], true));
$rpcService = Config::get('rpc', []); $rpcService = Config::get('rpc', []);
if (!empty($rpcService)) { if (!empty($rpcService)) {
$this->rpcListener($rpcService); $this->rpcListener($rpcService);
} }
foreach ($this->process as $process) {
$this->manager->addProcess($process);
}
Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION);
return $this->manager->getServer()->start(); $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 * @param $rpcService
* @throws ReflectionException * @throws ReflectionException
* @throws NotFindClassException * @throws NotFindClassException
*/ */
private function rpcListener($rpcService) private function rpcListener($rpcService)
{ {
$rpcService['events'][Constant::CONNECT] = [Service::class, 'onConnect']; $rpcService['events'][Constant::CONNECT] = [Service::class, 'onConnect'];
$rpcService['events'][Constant::DISCONNECT] = [Service::class, 'onClose']; $rpcService['events'][Constant::DISCONNECT] = [Service::class, 'onClose'];
$rpcService['events'][Constant::CLOSE] = [Service::class, 'onClose']; $rpcService['events'][Constant::CLOSE] = [Service::class, 'onClose'];
$rpcService['events'][Constant::RECEIVE] = [Service::class, 'onReceive']; $rpcService['events'][Constant::RECEIVE] = [Service::class, 'onReceive'];
$rpcService['events'][Constant::PACKET] = [Service::class, 'onPacket']; $rpcService['events'][Constant::PACKET] = [Service::class, 'onPacket'];
$this->manager->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'], $rpcService['mode'], $rpcService); $this->manager->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'], $rpcService['mode'], $rpcService);
} }
/** /**
* @param $host * @param $host
* @param $Port * @param $Port
* @return Packet|Websocket|Receive|Http|null * @return Packet|Websocket|Receive|Http|null
* @throws Exception * @throws Exception
*/ */
public function error_stop($host, $Port): Packet|Websocket|Receive|Http|null public function error_stop($host, $Port): Packet|Websocket|Receive|Http|null
{ {
$this->error(sprintf('Port %s::%d is already.', $host, $Port)); $this->error(sprintf('Port %s::%d is already.', $host, $Port));
if ($this->swoole) { if ($this->swoole) {
$this->swoole->shutdown(); $this->swoole->shutdown();
} else { } else {
$this->shutdown(); $this->shutdown();
} }
return $this->swoole; return $this->swoole;
} }
/** /**
* @return bool * @return bool
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public function isRunner(): bool public function isRunner(): bool
{ {
$port = Config::get('servers'); $port = Config::get('servers');
if (empty($port)) { if (empty($port)) {
return false; return false;
} }
foreach ($port as $value) { foreach ($port as $value) {
if ($this->checkPort($value['port'])) { if ($this->checkPort($value['port'])) {
return true; return true;
} }
} }
return false; return false;
} }
/** /**
* @param $port * @param $port
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
private function checkPort($port): bool private function checkPort($port): bool
{ {
if (Snowflake::getPlatform()->isLinux()) { if (Snowflake::getPlatform()->isLinux()) {
exec('netstat -tunlp | grep ' . $port, $output); exec('netstat -tunlp | grep ' . $port, $output);
} else { } else {
exec('lsof -i :' . $port . ' | grep -i "LISTEN"', $output); exec('lsof -i :' . $port . ' | grep -i "LISTEN"', $output);
} }
return !empty($output); return !empty($output);
} }
/** /**
* @return void * @return void
* *
* start server * start server
* @throws Exception * @throws Exception
*/ */
public function shutdown() public function shutdown()
{ {
/** @var Shutdown $shutdown */ /** @var Shutdown $shutdown */
$shutdown = Snowflake::app()->get('shutdown'); $shutdown = Snowflake::app()->get('shutdown');
$shutdown->shutdown(); $shutdown->shutdown();
} }
/** /**
* @param $daemon * @param $daemon
* @return Server * @return Server
*/ */
public function setDaemon($daemon): static public function setDaemon($daemon): static
{ {
if (!in_array($daemon, [0, 1])) { if (!in_array($daemon, [0, 1])) {
return $this; return $this;
} }
$this->daemon = $daemon; $this->daemon = $daemon;
return $this; return $this;
} }
/** /**
* @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null * @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
*/ */
#[Pure] public function getServer(): \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(); return $this->manager->getServer();
} }
} }