From 3d690d6d15b76f313924ce7b7f6b695e6c5c5c9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Tue, 26 Oct 2021 18:58:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 1 - src/Annotation/JsonRpc.php | 19 ++++ src/Consumers.php | 53 +++++++++ src/InvalidRpcParamsException.php | 23 ++++ src/OnJsonRpcInterface.php | 12 ++ src/OnRpcConsumerInterface.php | 8 ++ src/Protocol.php | 37 ------ src/Registry.php | 68 ----------- src/RpcJsonp.php | 183 ++++++++++++++++++++++++++++++ src/RpcManager.php | 32 ------ src/RpcProvider.php | 109 ------------------ src/TestRpcService.php | 33 ++++++ src/config.php | 32 ++++-- 13 files changed, 352 insertions(+), 258 deletions(-) create mode 100644 src/Annotation/JsonRpc.php create mode 100644 src/Consumers.php create mode 100644 src/InvalidRpcParamsException.php create mode 100644 src/OnJsonRpcInterface.php create mode 100644 src/OnRpcConsumerInterface.php delete mode 100644 src/Protocol.php delete mode 100644 src/Registry.php create mode 100644 src/RpcJsonp.php delete mode 100644 src/RpcManager.php delete mode 100644 src/RpcProvider.php create mode 100644 src/TestRpcService.php diff --git a/composer.json b/composer.json index 2612c24..c8558c9 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,5 @@ } }, "require-dev": { - "kwn/php-rdkafka-stubs": "^2.0" } } diff --git a/src/Annotation/JsonRpc.php b/src/Annotation/JsonRpc.php new file mode 100644 index 0000000..1c15003 --- /dev/null +++ b/src/Annotation/JsonRpc.php @@ -0,0 +1,19 @@ +get($name); - } - - -} diff --git a/src/RpcJsonp.php b/src/RpcJsonp.php new file mode 100644 index 0000000..70c15a3 --- /dev/null +++ b/src/RpcJsonp.php @@ -0,0 +1,183 @@ +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'])) { + $dispatch = $this->dispatch($data); + if (!isset($data['id'])) { + return; + } + $result = json_encode($dispatch, JSON_UNESCAPED_UNICODE); + } else { + $channel = new Channel($total = count($data)); + foreach ($data as $datum) { + $this->_execute($channel, $datum); + } + $result = []; + for ($i = 0; $i < $total; $i++) { + $params = $channel->pop(); + if (empty($params)) { + continue; + } + $result[] = $params; + } + } + $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 { + $dispatch = $this->dispatch($datum); + if (!isset($dispatch['id'])) { + $dispatch = null; + } + $channel->push($dispatch); + } + }); + } + + + /** + * @param $data + * @return array + */ + private function dispatch($data): array + { + try { + $handler = HandlerManager::get($data['method'], 'json-rpc'); + if (is_integer($handler)) { + throw new \Exception('Invalid Request无效请求', -32600); + } else if (is_null($handler)) { + throw new \Exception('Method not found', -32601); + } else { + return $this->handler($handler, $data); + } + } catch (\Throwable $throwable) { + $code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode(); + return $this->failure($code, jTraceEx($throwable), [], $data['id'] ?? null); + } + } + + + /** + * @param $handler + * @param $data + * @return array + * @throws \Exception + */ + private function handler($handler, $data): array + { + $dispatcher = (new Dispatcher($handler, $handler->_middlewares))->handle(new ServerRequest()); + if ($dispatcher instanceof ResponseInterface) { + $dispatcher = json_decode($dispatcher->getBody()->getContents(), true); + } + return ['jsonrpc' => '2.0', 'result' => $dispatcher, 'id' => $data['id'] ?? null]; + } + + + /** + * @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. + } +} diff --git a/src/RpcManager.php b/src/RpcManager.php deleted file mode 100644 index 6a83e53..0000000 --- a/src/RpcManager.php +++ /dev/null @@ -1,32 +0,0 @@ -addListener( - $config['type'], $config['host'], $config['port'], $config['mode'], $config - ); - } - - - /** - * @param Server $server - * @param int $fd - */ - public function onClose(Server $server, int $fd): void - { - // TODO: Implement onClose() method. - } - - - /** - * @param Server $server - * @param int $fd - */ - public function onDisconnect(Server $server, int $fd): void - { - // TODO: Implement onDisconnect() method. - } - - - /** - * @param Server $server - * @param int $fd - */ - public function onConnect(Server $server, int $fd): void - { - $server->confirm($fd); - } - - - /** - * @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 { - [$cmd, [$body, $protocol]] = Protocol::parse($data); - - - - - } catch(\Throwable $throwable){ - - } - } - - - /** - * @param Request $request - * @param Response $response - */ - public function onRequest(Request $request, Response $response): void - { - // TODO: Implement onRequest() method. - } -} diff --git a/src/TestRpcService.php b/src/TestRpcService.php new file mode 100644 index 0000000..0e0627f --- /dev/null +++ b/src/TestRpcService.php @@ -0,0 +1,33 @@ + [ + 'name' => 'json-rpc', + 'type' => Constant::SERVER_TYPE_BASE, + 'mode' => SWOOLE_SOCK_TCP, + 'host' => '0.0.0.0', + 'port' => 9526, + 'settings' => [ + + ], + 'events' => [ + Constant::RECEIVE => [RpcJsonp::class, 'onReceive'] + ], 'consumers' => [ - 'userService' => [ - - 'type' => 'json', - - - 'class' => '' - - + 'class' => TestRpcService::class, + 'name' => 'test-rpc', + 'package' => 'test', + 'register' => [ + 'host' => '', + 'port' => '' ] - - ] - ] ];