Files
kiri-core/Rpc/Service.php
T

163 lines
4.0 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;
use HttpServer\Http\Context;
use HttpServer\Http\Request;
2021-07-11 03:57:25 +08:00
use HttpServer\Route\Router;
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-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-07-11 03:57:25 +08:00
use Snowflake\Event;
2021-03-23 10:30:14 +08:00
use Snowflake\Exception\ConfigException;
2021-03-23 11:22:25 +08:00
use Snowflake\Snowflake;
2021-07-11 03:57:25 +08:00
use Swoole\Server;
use function Swoole\Coroutine\defer;
2021-03-23 02:29:48 +08:00
/**
* Class Service
* @package Rpc
*/
class Service extends Component
{
2021-07-11 02:05:56 +08:00
const defaultConfig = [
'open_tcp_keepalive' => true,
'tcp_keepidle' => 30,
'tcp_keepinterval' => 10,
'tcp_keepcount' => 10,
'open_http_protocol' => false,
'open_websocket_protocol' => false,
];
2021-07-11 03:57:25 +08:00
const RPC_CONNECT = 'RPC::CONNECT';
const RPC_CLOSE = 'RPC::CLOSE';
private Router $router;
/**
2021-07-12 10:13:53 +08:00
* @throws Exception
2021-07-11 03:57:25 +08:00
*/
public function init()
{
$this->router = Snowflake::getApp('router');
}
2021-07-12 10:13:53 +08:00
/**
* @param Server $server
* @param int $fd
* @param int $reactorId
* @throws Exception
*/
2021-07-20 01:37:16 +08:00
public function onConnect(Server $server, int $fd, int $reactorId)
2021-07-11 03:57:25 +08:00
{
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
$config = $server->setting['enable_delay_receive'] ?? null;
if ($config === true) {
$server->confirm($fd);
2021-07-11 02:05:56 +08:00
}
2021-07-11 03:57:25 +08:00
Event::trigger(Service::RPC_CONNECT, [$server, $fd, $reactorId]);
}
2021-07-12 10:13:53 +08:00
/**
* @param Server $server
* @param int $fd
* on tcp client close
* @throws Exception
*/
2021-07-20 01:37:16 +08:00
public function onClose(Server $server, int $fd)
2021-07-11 03:57:25 +08:00
{
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
2021-07-11 02:05:56 +08:00
2021-07-11 03:57:25 +08:00
Event::trigger(Service::RPC_CLOSE, [$server, $fd]);
2021-07-11 02:05:56 +08:00
}
/**
2021-07-12 10:13:53 +08:00
* @param Server $server
2021-07-11 03:57:25 +08:00
* @param int $fd
* @param int $reID
* @param string $data
2021-07-12 10:13:53 +08:00
* @throws Exception
2021-07-11 02:05:56 +08:00
*/
2021-07-20 01:37:16 +08:00
public function onReceive(Server $server, int $fd, int $reID, string $data)
2021-07-11 02:05:56 +08:00
{
2021-07-11 03:57:25 +08:00
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
try {
$client = $server->getClientInfo($fd, $reID);
$request = $this->requestSpl((int)$client['server_port'], $data);
$result = $this->router->find_path($request)?->dispatch();
$server->send($fd, $result);
} catch (\Throwable $exception) {
$this->addError($exception, 'rpc-service');
$server->send($fd, $exception->getMessage());
}
2021-07-11 02:05:56 +08:00
}
2021-07-12 10:13:53 +08:00
/**
* @param Server $server
* @param string $data
* @param array $client
* @throws Exception
*/
2021-07-20 01:37:16 +08:00
public function onPacket(Server $server, string $data, array $client)
2021-07-11 02:05:56 +08:00
{
2021-07-11 03:57:25 +08:00
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
try {
$request = $this->requestSpl((int)$client['server_port'], $data);
$result = $this->router->find_path($request)?->dispatch();
$server->sendto($client['address'], $client['port'], $result);
} catch (\Throwable $exception) {
$this->addError($exception, 'rpc-service');
$server->sendto($client['address'], $client['port'], $exception->getMessage());
2021-07-11 02:05:56 +08:00
}
}
2021-03-29 10:38:47 +08:00
2021-07-12 10:13:53 +08:00
/**
* @param int $server_port
* @param string $data
* @return mixed
* @throws Exception
*/
2021-07-20 01:37:16 +08:00
public function requestSpl(int $server_port, string $data): mixed
2021-07-11 03:57:25 +08:00
{
$sRequest = new Request();
[$cmd, $repeat, $body] = explode("\n", $data);
if (is_null($body) || is_null($cmd) || !empty($repeat)) {
throw new Exception('Protocol format error.');
}
if (is_string($body) && is_null($data = Json::decode($body))) {
throw new Exception('Protocol format error.');
}
$sRequest->params->setPosts($data);
$sRequest->headers->setRequestUri('rpc/p' . $server_port . '/' . ltrim($cmd, '/'));
$sRequest->headers->setRequestMethod(Request::HTTP_CMD);
return Context::setContext('request', $sRequest);
}
2021-03-23 02:29:48 +08:00
}