2021-10-26 18:58:09 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Rpc;
|
|
|
|
|
|
2021-11-29 15:43:54 +08:00
|
|
|
use Http\Constrict\RequestInterface;
|
2021-12-02 16:22:44 +08:00
|
|
|
use Http\Handler\Handler;
|
2021-10-26 18:58:09 +08:00
|
|
|
use Http\Handler\Router;
|
2021-11-29 15:43:54 +08:00
|
|
|
use Http\Message\ServerRequest;
|
2021-10-28 15:20:45 +08:00
|
|
|
use Kiri\Abstracts\Component;
|
2021-12-06 17:10:45 +08:00
|
|
|
use Kiri\Abstracts\Config;
|
2021-10-29 11:05:18 +08:00
|
|
|
use Kiri\Consul\Agent;
|
2021-11-29 15:43:54 +08:00
|
|
|
use Kiri\Context;
|
2021-10-29 14:06:56 +08:00
|
|
|
use Kiri\Events\EventProvider;
|
2021-12-06 17:10:45 +08:00
|
|
|
use Kiri\Exception\ConfigException;
|
2021-10-28 14:02:25 +08:00
|
|
|
use Kiri\Kiri;
|
2021-12-01 19:04:59 +08:00
|
|
|
use Note\Inject;
|
|
|
|
|
use Note\Note;
|
2021-11-29 15:43:54 +08:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
2021-11-29 10:58:45 +08:00
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2021-11-29 15:43:54 +08:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2021-12-02 14:06:57 +08:00
|
|
|
use ReflectionException;
|
2021-11-18 11:37:13 +08:00
|
|
|
use Server\Contract\OnCloseInterface;
|
|
|
|
|
use Server\Contract\OnConnectInterface;
|
|
|
|
|
use Server\Contract\OnReceiveInterface;
|
2021-11-29 15:43:54 +08:00
|
|
|
use Server\Events\OnBeforeShutdown;
|
2021-12-02 14:06:57 +08:00
|
|
|
use Server\Events\OnServerBeforeStart;
|
|
|
|
|
use Server\Events\OnTaskerStart;
|
2021-12-01 19:04:59 +08:00
|
|
|
use Server\Events\OnWorkerStart;
|
2021-10-26 18:58:09 +08:00
|
|
|
use Swoole\Coroutine;
|
|
|
|
|
use Swoole\Coroutine\Channel;
|
|
|
|
|
use Swoole\Server;
|
2021-12-02 14:06:57 +08:00
|
|
|
use Swoole\Timer;
|
2021-10-26 18:58:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2021-10-28 15:20:45 +08:00
|
|
|
class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterface, OnCloseInterface
|
2021-10-26 18:58:09 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[Inject(Router::class)]
|
|
|
|
|
public Router $router;
|
|
|
|
|
|
|
|
|
|
|
2021-11-30 15:10:01 +08:00
|
|
|
#[Inject(Note::class)]
|
|
|
|
|
public Note $annotation;
|
2021-10-28 15:20:45 +08:00
|
|
|
|
2021-10-29 14:06:56 +08:00
|
|
|
|
|
|
|
|
#[Inject(EventProvider::class)]
|
|
|
|
|
public EventProvider $eventProvider;
|
|
|
|
|
|
2021-10-29 14:15:40 +08:00
|
|
|
|
2021-12-02 14:06:57 +08:00
|
|
|
private RpcManager $manager;
|
|
|
|
|
|
2021-10-28 15:20:45 +08:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
public function init(): void
|
|
|
|
|
{
|
2021-10-29 14:15:40 +08:00
|
|
|
$this->eventProvider->on(OnBeforeShutdown::class, [$this, 'onBeforeShutdown']);
|
2021-10-29 14:06:56 +08:00
|
|
|
|
2021-10-29 14:13:41 +08:00
|
|
|
scan_directory(APP_PATH . 'rpc', 'Rpc');
|
2021-12-01 19:04:59 +08:00
|
|
|
|
2021-12-02 14:06:57 +08:00
|
|
|
$this->eventProvider->on(OnWorkerStart::class, [$this, 'consulWatches']);
|
|
|
|
|
$this->eventProvider->on(OnServerBeforeStart::class, [$this, 'register']);
|
|
|
|
|
|
|
|
|
|
$this->manager = Kiri::getDi()->get(RpcManager::class);
|
2021-10-29 11:05:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-10-29 14:15:40 +08:00
|
|
|
/**
|
|
|
|
|
* @param OnBeforeShutdown $beforeShutdown
|
2021-11-29 10:58:45 +08:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
2021-10-29 14:15:40 +08:00
|
|
|
*/
|
|
|
|
|
public function onBeforeShutdown(OnBeforeShutdown $beforeShutdown)
|
|
|
|
|
{
|
2021-12-02 14:06:57 +08:00
|
|
|
$doneList = $this->manager->doneList();
|
2021-10-29 14:15:40 +08:00
|
|
|
$agent = $this->container->get(Agent::class);
|
2021-10-29 18:06:04 +08:00
|
|
|
foreach ($doneList as $value) {
|
2021-12-02 14:06:57 +08:00
|
|
|
$agent->service->deregister($value['config']['ID']);
|
|
|
|
|
$agent->checks->deregister($value['config']['Check']['CheckId']);
|
2021-10-29 18:06:04 +08:00
|
|
|
}
|
2021-10-29 14:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-10-29 11:06:52 +08:00
|
|
|
/**
|
2021-12-02 14:06:57 +08:00
|
|
|
* @param OnWorkerStart|OnTaskerStart $server
|
2021-12-06 17:10:45 +08:00
|
|
|
* @throws ConfigException
|
2021-10-29 11:06:52 +08:00
|
|
|
*/
|
2021-12-02 14:06:57 +08:00
|
|
|
public function consulWatches(OnWorkerStart|OnTaskerStart $server)
|
2021-10-29 11:05:18 +08:00
|
|
|
{
|
2021-12-02 15:26:43 +08:00
|
|
|
if ($server->workerId != 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-06 17:10:45 +08:00
|
|
|
$async_time = (int)Config::get('consul.async_time', 1000);
|
|
|
|
|
Timer::tick($async_time, static function ($timeId) {
|
2021-12-02 15:36:54 +08:00
|
|
|
if (env('state', 'start') == 'exit') {
|
|
|
|
|
Timer::clear($timeId);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-02 15:25:41 +08:00
|
|
|
Kiri::getDi()->get(RpcManager::class)->tick();
|
|
|
|
|
});
|
2021-12-02 14:06:57 +08:00
|
|
|
}
|
2021-12-01 19:04:59 +08:00
|
|
|
|
|
|
|
|
|
2021-12-02 14:06:57 +08:00
|
|
|
/**
|
|
|
|
|
* @param OnServerBeforeStart $server
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public function register(OnServerBeforeStart $server)
|
|
|
|
|
{
|
|
|
|
|
$this->manager->register();
|
2021-10-28 15:20:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-10-26 18:58:09 +08:00
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
{
|
|
|
|
|
$data = json_decode($data, true);
|
|
|
|
|
if (is_null($data)) {
|
|
|
|
|
$this->failure(-32700, 'Parse error语法解析错误');
|
|
|
|
|
} else if (!isset($data['jsonrpc']) || !isset($data['method']) || $data['jsonrpc'] != '2.0') {
|
|
|
|
|
$this->failure(-32600, 'Invalid Request无效请求');
|
|
|
|
|
} else {
|
|
|
|
|
$this->batchDispatch($server, $fd, $data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $fd
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function batchDispatch(Server $server, int $fd, array $data): void
|
|
|
|
|
{
|
|
|
|
|
if (isset($data['jsonrpc'])) {
|
2021-10-28 16:19:20 +08:00
|
|
|
$dispatch = $this->dispatch($data);
|
|
|
|
|
if (!isset($data['id'])) {
|
|
|
|
|
$dispatch = [1];
|
|
|
|
|
}
|
|
|
|
|
$result = json_encode($dispatch, JSON_UNESCAPED_UNICODE);
|
2021-10-26 18:58:09 +08:00
|
|
|
} else {
|
|
|
|
|
$channel = new Channel($total = count($data));
|
|
|
|
|
foreach ($data as $datum) {
|
|
|
|
|
$this->_execute($channel, $datum);
|
|
|
|
|
}
|
|
|
|
|
$result = [];
|
|
|
|
|
for ($i = 0; $i < $total; $i++) {
|
2021-10-28 16:17:25 +08:00
|
|
|
$result[] = $channel->pop();
|
2021-10-26 18:58:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$server->send($fd, 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 {
|
2021-10-28 16:19:20 +08:00
|
|
|
$dispatch = $this->dispatch($datum);
|
|
|
|
|
if (!isset($dispatch['id'])) {
|
|
|
|
|
$dispatch = [1];
|
|
|
|
|
}
|
|
|
|
|
$channel->push($dispatch);
|
2021-10-26 18:58:09 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function dispatch($data): array
|
|
|
|
|
{
|
|
|
|
|
try {
|
2021-12-02 16:18:10 +08:00
|
|
|
[$handler, $params] = $this->container->get(RpcManager::class)->get($data['service'], $data['method']);
|
2021-10-28 15:14:21 +08:00
|
|
|
if (is_null($handler)) {
|
2021-10-26 18:58:09 +08:00
|
|
|
throw new \Exception('Method not found', -32601);
|
|
|
|
|
} else {
|
2021-12-02 16:22:44 +08:00
|
|
|
Context::setContext(RequestInterface::class, $this->createServerRequest($params));
|
2021-11-29 15:43:54 +08:00
|
|
|
|
2021-12-02 16:22:44 +08:00
|
|
|
return $this->handler($handler);
|
2021-10-26 18:58:09 +08:00
|
|
|
}
|
|
|
|
|
} catch (\Throwable $throwable) {
|
|
|
|
|
$code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode();
|
|
|
|
|
return $this->failure($code, jTraceEx($throwable), [], $data['id'] ?? null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-11-29 15:43:54 +08:00
|
|
|
/**
|
|
|
|
|
* @param $params
|
|
|
|
|
* @return ServerRequestInterface
|
2021-11-29 15:53:33 +08:00
|
|
|
* @throws \Exception
|
2021-11-29 15:43:54 +08:00
|
|
|
*/
|
|
|
|
|
private function createServerRequest($params): ServerRequestInterface
|
|
|
|
|
{
|
|
|
|
|
return (new ServerRequest())->withParsedBody($params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-10-26 18:58:09 +08:00
|
|
|
/**
|
2021-12-02 16:22:44 +08:00
|
|
|
* @param Handler $handler
|
2021-10-26 18:58:09 +08:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2021-12-02 16:22:44 +08:00
|
|
|
private function handler(Handler $handler): array
|
2021-10-26 18:58:09 +08:00
|
|
|
{
|
2021-11-29 15:43:54 +08:00
|
|
|
return [
|
|
|
|
|
'jsonrpc' => '2.0',
|
2021-12-02 16:22:44 +08:00
|
|
|
'result' => call_user_func($handler->callback, ...$handler->params),
|
2021-11-29 15:43:54 +08:00
|
|
|
'id' => $data['id'] ?? null
|
|
|
|
|
];
|
2021-10-26 18:58:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $code
|
|
|
|
|
* @param $message
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @param null $id
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
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 Server $server
|
|
|
|
|
* @param int $fd
|
|
|
|
|
*/
|
|
|
|
|
public function onClose(Server $server, int $fd): void
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement onClose() method.
|
|
|
|
|
}
|
|
|
|
|
}
|