2022-01-09 14:00:32 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Rpc;
|
|
|
|
|
|
2022-03-03 16:39:46 +08:00
|
|
|
use Exception;
|
2022-05-04 03:11:10 +08:00
|
|
|
use JetBrains\PhpStorm\ArrayShape;
|
2022-01-14 11:29:16 +08:00
|
|
|
use Kiri;
|
2022-01-09 14:00:32 +08:00
|
|
|
use Kiri\Abstracts\Component;
|
|
|
|
|
use Kiri\Abstracts\Config;
|
2022-01-14 11:29:16 +08:00
|
|
|
use Kiri\Annotation\Annotation;
|
2022-01-09 14:00:32 +08:00
|
|
|
use Kiri\Consul\Agent;
|
|
|
|
|
use Kiri\Context;
|
2022-06-17 13:44:24 +08:00
|
|
|
use Kiri\Core\Json;
|
2022-03-03 18:30:59 +08:00
|
|
|
use Kiri\Events\EventProvider;
|
2022-01-09 14:00:32 +08:00
|
|
|
use Kiri\Exception\ConfigException;
|
2022-01-14 11:29:16 +08:00
|
|
|
use Kiri\Message\Constrict\RequestInterface;
|
2022-03-02 16:27:15 +08:00
|
|
|
use Kiri\Message\Handler\DataGrip;
|
2022-01-14 11:29:16 +08:00
|
|
|
use Kiri\Message\Handler\Router;
|
2022-03-02 16:27:15 +08:00
|
|
|
use Kiri\Message\Handler\RouterCollector;
|
2022-01-14 11:29:16 +08:00
|
|
|
use Kiri\Message\ServerRequest;
|
2022-01-10 11:39:56 +08:00
|
|
|
use Kiri\Server\Contract\OnCloseInterface;
|
|
|
|
|
use Kiri\Server\Contract\OnConnectInterface;
|
|
|
|
|
use Kiri\Server\Contract\OnReceiveInterface;
|
|
|
|
|
use Kiri\Server\Events\OnBeforeShutdown;
|
|
|
|
|
use Kiri\Server\Events\OnServerBeforeStart;
|
2022-01-14 11:29:16 +08:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
2022-06-16 17:38:23 +08:00
|
|
|
use Kiri\Di\ContainerInterface;
|
2022-01-14 11:29:16 +08:00
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2022-02-23 16:32:08 +08:00
|
|
|
use ReflectionException;
|
2022-01-09 14:00:32 +08:00
|
|
|
use Swoole\Coroutine;
|
|
|
|
|
use Swoole\Coroutine\Channel;
|
|
|
|
|
use Swoole\Server;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterface, OnCloseInterface
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2022-06-16 17:38:23 +08:00
|
|
|
private array $consul = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ContainerInterface $container
|
|
|
|
|
* @param Router $router
|
|
|
|
|
* @param Annotation $annotation
|
|
|
|
|
* @param DataGrip $dataGrip
|
|
|
|
|
* @param RpcManager $manager
|
|
|
|
|
* @param RouterCollector $collector
|
|
|
|
|
* @param EventProvider $eventProvider
|
|
|
|
|
* @param array $config
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(public ContainerInterface $container,
|
|
|
|
|
public Router $router,
|
|
|
|
|
public Annotation $annotation,
|
|
|
|
|
public DataGrip $dataGrip,
|
|
|
|
|
public RpcManager $manager,
|
|
|
|
|
public RouterCollector $collector,
|
|
|
|
|
public EventProvider $eventProvider,
|
|
|
|
|
array $config = [])
|
|
|
|
|
{
|
|
|
|
|
parent::__construct($config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws ReflectionException|ConfigException
|
|
|
|
|
*/
|
|
|
|
|
public function init(): void
|
|
|
|
|
{
|
|
|
|
|
$this->eventProvider->on(OnBeforeShutdown::class, [$this, 'onBeforeShutdown']);
|
|
|
|
|
scan_directory(APP_PATH . 'rpc', 'app\Rpc');
|
|
|
|
|
$this->consul = Config::get('rpc.consul', null);
|
|
|
|
|
if (!empty($this->consul)) {
|
|
|
|
|
$this->eventProvider->on(OnServerBeforeStart::class, [$this, 'register']);
|
2022-05-31 20:41:31 +08:00
|
|
|
}
|
2022-06-16 17:38:23 +08:00
|
|
|
$this->collector = $this->dataGrip->get('rpc');
|
|
|
|
|
}
|
2022-06-08 14:27:49 +08:00
|
|
|
|
2022-06-16 17:38:23 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param OnBeforeShutdown $beforeShutdown
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
|
*/
|
|
|
|
|
public function onBeforeShutdown(OnBeforeShutdown $beforeShutdown): void
|
|
|
|
|
{
|
|
|
|
|
if (env('environmental') != Kiri::WORKER && env('environmental_workerId') != 0) {
|
2022-05-31 11:43:06 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2022-06-16 17:38:23 +08:00
|
|
|
|
|
|
|
|
$agent = $this->container->get(Agent::class);
|
|
|
|
|
|
2022-05-31 11:43:06 +08:00
|
|
|
$this->logger->debug("disconnect consul.");
|
2022-06-16 17:38:23 +08:00
|
|
|
|
|
|
|
|
$agent->service->deregister($this->consul['ID']);
|
|
|
|
|
$agent->checks->deregister($this->consul['Check']['CheckId']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-05-31 11:11:12 +08:00
|
|
|
/**
|
|
|
|
|
* @param OnServerBeforeStart $server
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
*/
|
2022-06-16 17:38:23 +08:00
|
|
|
public function register(OnServerBeforeStart $server)
|
|
|
|
|
{
|
2022-05-31 11:11:12 +08:00
|
|
|
$consumers = Config::get("rpc.consumers", []);
|
|
|
|
|
if (!empty($consumers)) {
|
|
|
|
|
foreach ($consumers as $service => $consumer) {
|
2022-06-16 17:38:23 +08:00
|
|
|
$this->manager->add($service, $consumer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->manager->register($this->consul);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $fd
|
|
|
|
|
*/
|
|
|
|
|
public function onConnect(Server $server, int $fd): void
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement onConnect() method.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $fd
|
|
|
|
|
* @param int $reactor_id
|
|
|
|
|
* @param string $data
|
|
|
|
|
*/
|
|
|
|
|
public function onReceive(Server $server, int $fd, int $reactor_id, string $data): void
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$data = json_decode($data, true);
|
|
|
|
|
if (!is_array($data)) {
|
|
|
|
|
throw new Exception('Parse error语法解析错误', -32700);
|
|
|
|
|
}
|
|
|
|
|
if (!isset($data['jsonrpc']) || !isset($data['method']) || $data['jsonrpc'] != '2.0') {
|
|
|
|
|
throw new Exception('Invalid Request无效请求', -32600);
|
|
|
|
|
}
|
|
|
|
|
$server->send($fd, $this->batchDispatch($data));
|
|
|
|
|
} catch (\Throwable $throwable) {
|
|
|
|
|
$this->logger->error('JsonRpc: ' . $throwable->getMessage());
|
2022-06-17 13:44:24 +08:00
|
|
|
$response = Json::encode($this->failure(-32700, $throwable->getMessage()));
|
|
|
|
|
$server->send($fd, $response);
|
2022-06-16 17:38:23 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return string|bool
|
|
|
|
|
*/
|
|
|
|
|
private function batchDispatch(array $data): string|bool
|
|
|
|
|
{
|
|
|
|
|
if (isset($data['jsonrpc'])) {
|
|
|
|
|
$result = $this->dispatch($data);
|
|
|
|
|
if (!isset($data['id'])) {
|
|
|
|
|
$result = [1];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$channel = new Channel($total = count($data));
|
|
|
|
|
foreach ($data as $datum) {
|
|
|
|
|
$this->_execute($channel, $datum);
|
|
|
|
|
}
|
|
|
|
|
$result = [];
|
|
|
|
|
for ($i = 0; $i < $total; $i++) {
|
|
|
|
|
$result[] = $channel->pop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return json_encode($result, JSON_UNESCAPED_UNICODE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $channel
|
|
|
|
|
* @param $datum
|
|
|
|
|
*/
|
|
|
|
|
private function _execute($channel, $datum)
|
|
|
|
|
{
|
|
|
|
|
Coroutine::create(function () use ($channel, $datum) {
|
|
|
|
|
if (empty($datum) || !isset($datum['jsonrpc'])) {
|
|
|
|
|
$channel->push($this->failure(-32700, 'Parse error语法解析错误'));
|
|
|
|
|
} else if (!isset($datum['method'])) {
|
|
|
|
|
$channel->push($this->failure(-32700, 'Parse error语法解析错误'));
|
|
|
|
|
} else {
|
|
|
|
|
$dispatch = $this->dispatch($datum);
|
|
|
|
|
if (!isset($dispatch['id'])) {
|
|
|
|
|
$dispatch = [1];
|
|
|
|
|
}
|
|
|
|
|
$channel->push($dispatch);
|
2022-05-31 11:11:12 +08:00
|
|
|
}
|
2022-06-16 17:38:23 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function dispatch($data): array
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$handler = $this->collector->find($data['service'], 'GET');
|
|
|
|
|
if (is_integer($handler) || is_null($handler)) {
|
|
|
|
|
throw new Exception('Handler not found', -32601);
|
|
|
|
|
}
|
|
|
|
|
$controller = $this->container->get($handler->callback[0]);
|
|
|
|
|
if (!method_exists($controller, $data['method'])) {
|
|
|
|
|
throw new Exception('Method not found', -32601);
|
|
|
|
|
}
|
2022-06-16 18:49:33 +08:00
|
|
|
$params = $this->container->getArgs($data['method'], $controller::class);
|
2022-06-16 17:38:23 +08:00
|
|
|
|
|
|
|
|
Context::setContext(RequestInterface::class, $this->createServerRequest($params));
|
|
|
|
|
|
|
|
|
|
return $this->handler($controller, $data['method'], $params);
|
|
|
|
|
} catch (\Throwable $throwable) {
|
|
|
|
|
$code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode();
|
|
|
|
|
return $this->failure($code, jTraceEx($throwable), [], $data['id'] ?? null);
|
2022-05-31 11:11:12 +08:00
|
|
|
}
|
2022-06-16 17:38:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $params
|
|
|
|
|
* @return ServerRequestInterface
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function createServerRequest($params): ServerRequestInterface
|
|
|
|
|
{
|
|
|
|
|
return (new ServerRequest())->withParsedBody($params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $controller
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @param $params
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
#[ArrayShape([])]
|
|
|
|
|
private function handler($controller, string $method, $params): array
|
|
|
|
|
{
|
|
|
|
|
$result = call_user_func([$controller, $method], ...$params);
|
|
|
|
|
return [
|
|
|
|
|
'jsonrpc' => '2.0',
|
|
|
|
|
'result' => $result,
|
|
|
|
|
'id' => $data['id'] ?? null
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $code
|
|
|
|
|
* @param $message
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @param null $id
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
#[ArrayShape([])]
|
|
|
|
|
protected function failure($code, $message, array $data = [], $id = null): array
|
|
|
|
|
{
|
|
|
|
|
$error = [
|
|
|
|
|
'jsonrpc' => '2.0',
|
|
|
|
|
'error' => [
|
|
|
|
|
'code' => $code,
|
|
|
|
|
'message' => $message,
|
|
|
|
|
'data' => $data
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
if (!is_null($id)) {
|
|
|
|
|
$error['id'] = $id;
|
|
|
|
|
}
|
|
|
|
|
return $error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $fd
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2022-06-16 18:07:27 +08:00
|
|
|
public function onClose(int $fd): void
|
2022-06-16 17:38:23 +08:00
|
|
|
{
|
|
|
|
|
// TODO: Implement onClose() method.
|
|
|
|
|
}
|
2022-01-09 14:00:32 +08:00
|
|
|
}
|