This commit is contained in:
2021-10-28 15:14:21 +08:00
parent 791af89ba9
commit 7e91e227b2
4 changed files with 47 additions and 30 deletions
+6 -4
View File
@@ -3,8 +3,8 @@
namespace Kiri\Rpc\Annotation; namespace Kiri\Rpc\Annotation;
use Annotation\Attribute; use Annotation\Attribute;
use Http\Handler\Router; use Kiri\Rpc\RpcManager;
use Kiri\Kiri; use ReflectionException;
#[\Attribute(\Attribute::TARGET_CLASS)] class JsonRpc extends Attribute #[\Attribute(\Attribute::TARGET_CLASS)] class JsonRpc extends Attribute
{ {
@@ -13,6 +13,7 @@ use Kiri\Kiri;
/** /**
* @param string $method * @param string $method
* @param string $version * @param string $version
* @param string $protocol
*/ */
public function __construct(public string $method, public string $version = '2.0', public string $protocol = 'json') public function __construct(public string $method, public string $version = '2.0', public string $protocol = 'json')
{ {
@@ -24,10 +25,11 @@ use Kiri\Kiri;
* @param mixed $class * @param mixed $class
* @param mixed|string $method * @param mixed|string $method
* @return mixed * @return mixed
* @throws ReflectionException
*/ */
public function execute(mixed $class, mixed $method = ''): mixed public function execute(mixed $class, mixed $method = ''): bool
{ {
return parent::execute($class, $method); // TODO: Change the autogenerated stub return RpcManager::add($this->method, $class);
} }
+2 -2
View File
@@ -39,7 +39,7 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface
} else { } else {
$client = $this->clientNotCoroutine($config); $client = $this->clientNotCoroutine($config);
} }
$client->send(json_encode(['jsonrpc' => $version, 'method' => $method, 'params' => $data])); $client->send(json_encode(['jsonrpc' => $version, 'service' => $service, 'method' => $method, 'params' => $data]));
$client->close(); $client->close();
} }
@@ -61,7 +61,7 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface
} else { } else {
$client = $this->clientNotCoroutine($config); $client = $this->clientNotCoroutine($config);
} }
$client->send(json_encode(['jsonrpc' => $version, 'method' => $method, 'params' => $data, 'id' => $id])); $client->send(json_encode(['jsonrpc' => $version, 'service' => $service, 'method' => $method, 'params' => $data, 'id' => $id]));
$read = $client->recv(); $read = $client->recv();
$client->close(); $client->close();
return json_decode($read, true); return json_decode($read, true);
+10 -21
View File
@@ -4,9 +4,7 @@ namespace Kiri\Rpc;
use Annotation\Inject; use Annotation\Inject;
use Http\Constrict\ResponseInterface; use Http\Constrict\ResponseInterface;
use Http\Handler\Abstracts\HandlerManager;
use Http\Handler\Dispatcher; use Http\Handler\Dispatcher;
use Http\Handler\Handler;
use Http\Handler\Router; use Http\Handler\Router;
use Http\Message\ServerRequest; use Http\Message\ServerRequest;
use Kiri\Kiri; use Kiri\Kiri;
@@ -120,13 +118,11 @@ class RpcJsonp implements OnConnectInterface, OnReceiveInterface, OnCloseInterfa
private function dispatch($data): array private function dispatch($data): array
{ {
try { try {
$handler = HandlerManager::get($data['method'], 'json-rpc'); [$handler, $params] = RpcManager::get($data['service'], $data['method']);
if (is_integer($handler)) { if (is_null($handler)) {
throw new \Exception('Invalid Request无效请求', -32600);
} else if (is_null($handler)) {
throw new \Exception('Method not found', -32601); throw new \Exception('Method not found', -32601);
} else { } else {
return $this->handler($handler, $data); return $this->handler($handler, $params, $data);
} }
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
$code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode(); $code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode();
@@ -136,26 +132,19 @@ class RpcJsonp implements OnConnectInterface, OnReceiveInterface, OnCloseInterfa
/** /**
* @param Handler $handler * @param array $handler
* @param array $params
* @param $data * @param $data
* @return array * @return array
* @throws \Exception
*/ */
private function handler(Handler $handler, $data): array private function handler(array $handler, array $params, $data): array
{ {
/** @var ReflectionMethod $reflection */ $controller = Kiri::getDi()->get($handler[0]);
$reflection = Kiri::getDi()->getReflectMethod($handler->callback[0]::class, $handler->callback[1]);
$params = []; $params = array_merge($params, $data['params']);
foreach ($reflection->getParameters() as $value) {
$params[] = $data['params'][$value->getName()] ?? null; $dispatcher = $controller->{$handler[1]}(...$params);
}
$handler->params = $params;
$dispatcher = (new Dispatcher($handler, $handler->_middlewares))->handle((new ServerRequest())->withData($data['params']));
if ($dispatcher instanceof ResponseInterface) {
$dispatcher = json_decode($dispatcher->getBody()->getContents(), true);
}
return ['jsonrpc' => '2.0', 'result' => $dispatcher, 'id' => $data['id'] ?? null]; return ['jsonrpc' => '2.0', 'result' => $dispatcher, 'id' => $data['id'] ?? null];
} }
+29 -3
View File
@@ -2,19 +2,45 @@
namespace Kiri\Rpc; namespace Kiri\Rpc;
use Kiri\Kiri;
use ReflectionException;
class RpcManager class RpcManager
{ {
private static array $_rpc = [];
public static function add()
/**
* @param string $name
* @param string $class
* @return bool
* @throws ReflectionException
*/
public static function add(string $name, string $class): bool
{ {
$methods = Kiri::getDi()->getReflect($class);
$lists = $methods->getMethods(\ReflectionMethod::IS_PUBLIC);
if (!isset(static::$_rpc[$name])) static::$_rpc[$name] = [];
foreach ($lists as $reflection) {
$params = Kiri::getDi()->getMethodParameters($class, $reflection->getName());
static::$_rpc[$name][] = [$reflection->getName(), $params];
}
return true;
} }
public static function get() /**
* @param string $name
* @param string $method
* @return mixed
*/
public static function get(string $name, string $method): array
{ {
return static::$_rpc[$name][$method] ?? [null, null];
} }
} }