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-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-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-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-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
|
|
|
$this->listenPort($service, $mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $service
|
|
|
|
|
* @param $mode
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function listenPort($service, $mode)
|
|
|
|
|
{
|
|
|
|
|
router()->addPortListen($service['port'], function () use ($service, $mode) {
|
2021-03-23 11:22:25 +08:00
|
|
|
try {
|
2021-03-23 16:14:05 +08:00
|
|
|
/** @var Request $request */
|
2021-03-23 11:22:25 +08:00
|
|
|
$request = Context::getContext('request');
|
2021-03-23 18:23:45 +08:00
|
|
|
if (($node = router()->find_path(Service::replace($request, $service))) === null) {
|
2021-03-23 11:22:25 +08:00
|
|
|
throw new Exception('Cmd not find.');
|
|
|
|
|
}
|
2021-03-23 18:23:45 +08:00
|
|
|
$response = $node->dispatch();
|
|
|
|
|
if (is_string($response)) {
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
return Json::encode($response);
|
2021-03-23 11:22:25 +08:00
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
$this->addError($exception);
|
2021-03-23 18:05:16 +08:00
|
|
|
return Json::encode(['state' => 'fail', 'message' => $exception->getMessage()]);
|
2021-03-23 11:22:25 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-03-23 02:29:48 +08:00
|
|
|
|
|
|
|
|
|
2021-03-23 16:14:05 +08:00
|
|
|
/**
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @param array $service
|
|
|
|
|
* @return Request
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-03-23 18:23:45 +08:00
|
|
|
public static function replace(Request $request, array $service): Request
|
2021-03-23 16:14:05 +08:00
|
|
|
{
|
2021-03-23 18:29:03 +08:00
|
|
|
$body = $request->params->getBodyAndClear();
|
|
|
|
|
if (is_string($body) && is_null($body = Json::decode($body))) {
|
2021-03-23 16:14:05 +08:00
|
|
|
throw new Exception('Protocol format error.');
|
|
|
|
|
}
|
2021-03-23 18:31:46 +08:00
|
|
|
|
2021-03-23 18:27:45 +08:00
|
|
|
if (!isset($body['cmd'])) {
|
2021-03-23 18:29:03 +08:00
|
|
|
throw new Exception('Unknown system cmd.');
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
2021-03-23 18:27:45 +08:00
|
|
|
$request->params->setPosts($body);
|
2021-03-23 16:14:05 +08:00
|
|
|
|
2021-03-23 18:27:45 +08:00
|
|
|
$body['cmd'] = ltrim($body['cmd'], '/');
|
2021-03-23 16:14:05 +08:00
|
|
|
|
|
|
|
|
$header = $request->headers;
|
2021-03-23 18:27:45 +08:00
|
|
|
$header->replace('request_uri', 'rpc/p' . $service['port'] . '/' . $body['cmd']);
|
2021-03-23 16:14:05 +08:00
|
|
|
$header->replace('request_method', Request::HTTP_CMD);
|
2021-03-23 18:32:35 +08:00
|
|
|
$request->parseUri();
|
2021-03-23 16:14:05 +08:00
|
|
|
|
|
|
|
|
return $request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-03-23 02:29:48 +08:00
|
|
|
}
|