Files
kiri-core/Rpc/Service.php
T

99 lines
2.3 KiB
PHP
Raw Normal View History

2021-03-23 02:29:48 +08:00
<?php
namespace Rpc;
2021-03-23 11:22:25 +08:00
use Exception;
2021-03-29 10:38:47 +08:00
use HttpServer\Events\OnClose;
use HttpServer\Events\OnConnect;
use HttpServer\Events\OnPacket;
use HttpServer\Events\OnReceive;
2021-03-23 11:22:25 +08:00
use HttpServer\Http\Context;
use HttpServer\Http\Request;
2021-03-29 10:38:47 +08:00
use HttpServer\Server;
2021-03-23 02:38:20 +08:00
use HttpServer\Service\Http;
use HttpServer\Service\Packet;
use HttpServer\Service\Receive;
use HttpServer\Service\Websocket;
2021-03-24 16:12:02 +08:00
use JetBrains\PhpStorm\Pure;
2021-03-29 10:38:47 +08:00
use ReflectionException;
2021-03-23 02:29:48 +08:00
use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\Config;
2021-03-23 11:22:25 +08:00
use Snowflake\Core\Json;
2021-03-23 10:30:14 +08:00
use Snowflake\Exception\ConfigException;
2021-03-29 10:38:47 +08:00
use Snowflake\Exception\NotFindClassException;
2021-03-23 11:22:25 +08:00
use Snowflake\Snowflake;
2021-03-23 02:29:48 +08:00
/**
* Class Service
* @package Rpc
*/
class Service extends Component
{
2021-03-23 10:30:14 +08:00
/**
* @param Packet|Websocket|Receive|Http|null $server
* @throws ConfigException
2021-03-23 11:22:25 +08:00
* @throws Exception
2021-03-23 10:30:14 +08:00
*/
2021-03-23 11:22:25 +08:00
public function instance(Packet|Websocket|Receive|null|Http $server): void
{
2021-03-24 19:09:05 +08:00
$service = Config::get('rpc');
if (empty($service) || !is_array($service)) {
2021-03-23 11:22:25 +08:00
return;
}
$mode = $service['mode'] ?? SWOOLE_SOCK_TCP6;
2021-03-23 19:03:48 +08:00
if (Snowflake::port_already($service['port'])) {
2021-03-23 19:07:24 +08:00
throw new Exception($this->already($service));
2021-03-23 19:03:48 +08:00
}
2021-03-23 19:07:24 +08:00
$this->debug(Snowflake::listen($service));
2021-03-23 19:03:48 +08:00
2021-03-30 19:57:00 +08:00
$this->addCallback($mode);
2021-03-29 10:38:47 +08:00
2021-03-23 11:22:25 +08:00
$rpcServer = $server->addlistener($service['host'], $service['port'], $mode);
2021-03-24 16:12:02 +08:00
$rpcServer->set($service['setting'] ?? [
'open_tcp_keepalive' => true,
'tcp_keepidle' => 30,
'tcp_keepinterval' => 10,
'tcp_keepcount' => 10,
'open_http_protocol' => false,
'open_websocket_protocol' => false,
]);
2021-03-23 19:07:24 +08:00
}
/**
* @param $service
* @return string
*/
2021-03-24 16:12:02 +08:00
#[Pure] private function already($service): string
2021-03-23 19:07:24 +08:00
{
return sprintf('Port %s::%d is already.', $service['host'], $service['port']);
}
2021-03-29 10:38:47 +08:00
/**
* @param $mode
* @throws Exception
*/
2021-03-30 19:57:00 +08:00
private function addCallback($mode)
2021-03-29 10:38:47 +08:00
{
$tcp = [SWOOLE_SOCK_TCP, SWOOLE_TCP, SWOOLE_TCP6, SWOOLE_SOCK_TCP6];
2021-03-30 19:57:00 +08:00
$server = Snowflake::app()->getServer();
$server->onBindCallback('connect', [make(OnConnect::class), 'onHandler']);
$server->onBindCallback('close', [make(OnClose::class), 'onHandler']);
2021-03-29 10:38:47 +08:00
2021-03-30 19:57:00 +08:00
if (in_array($mode, $tcp)) {
$server->onBindCallback('receive', [make(OnReceive::class), 'onHandler']);
} else {
$server->onBindCallback('packet', [make(OnReceive::class), 'onHandler']);
2021-03-29 10:38:47 +08:00
}
}
2021-03-23 02:29:48 +08:00
}