14 Commits

Author SHA1 Message Date
as2252258 b279e56efd 改名 2021-11-30 15:10:01 +08:00
as2252258 92a1e68bec 改名 2021-11-30 14:44:51 +08:00
as2252258 e8b9387252 改名 2021-11-30 14:44:34 +08:00
as2252258 46e6e2b2c2 改名 2021-11-29 15:57:36 +08:00
as2252258 aa94fea000 改名 2021-11-29 15:53:33 +08:00
as2252258 c6973dbe83 改名 2021-11-29 15:43:54 +08:00
as2252258 62e0eabe4f 改名 2021-11-29 15:19:07 +08:00
as2252258 adaa4ba14d 改名 2021-11-29 15:18:09 +08:00
as2252258 ddb55ee5a0 改名 2021-11-29 15:02:44 +08:00
as2252258 0228ed2241 改名 2021-11-29 11:44:03 +08:00
as2252258 168ceac6a8 改名 2021-11-29 10:58:45 +08:00
as2252258 40f1a49e77 1 2021-11-27 17:43:29 +08:00
as2252258 3c3d0a4107 改名 2021-11-18 11:37:13 +08:00
as2252258 f4b137ac91 改名 2021-11-05 15:36:40 +08:00
7 changed files with 87 additions and 47 deletions
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace PHPSTORM_META {
// Reflect
use Kiri\Di\Container;
use Psr\Container\ContainerInterface;
override(ContainerInterface::get(0), map('@'));
override(Container::get(0), map('@'));
override(Container::make(0), map('@'));
override(Container::create(0), map('@'));
// override(\Hyperf\Utils\Context::get(0), map('@'));
// override(\make(0), map('@'));
override(\di(0), map('@'));
override(\duplicate(0), map('@'));
}
-1
View File
@@ -11,7 +11,6 @@
"require": {
"php": ">=8.0",
"ext-json": "*",
"game-worker/kiri-consul": "dev-master",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0"
},
+1 -1
View File
@@ -2,7 +2,7 @@
namespace Kiri\Rpc;
use Annotation\Inject;
use Note\Inject;
use Exception;
use Http\Message\Response;
use Http\Message\Stream;
@@ -1,8 +1,8 @@
<?php
namespace Kiri\Rpc\Annotation;
namespace Kiri\Rpc\Note;
use Annotation\Attribute;
use Note\Attribute;
use Kiri\Abstracts\Config;
use Kiri\Consul\Agent;
use Kiri\Exception\ConfigException;
+45 -17
View File
@@ -2,21 +2,28 @@
namespace Kiri\Rpc;
use Annotation\Annotation;
use Annotation\Inject;
use Note\Note;
use Note\Inject;
use Http\Constrict\RequestInterface;
use Http\Handler\Router;
use Http\Message\ServerRequest;
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
use Kiri\Consul\Agent;
use Kiri\Di\ContainerInterface;
use Kiri\Context;
use Kiri\Core\Json;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ServerRequestInterface;
use Server\Contract\OnCloseInterface;
use Server\Contract\OnConnectInterface;
use Server\Contract\OnReceiveInterface;
use Server\Events\OnBeforeShutdown;
use Server\Events\OnStart;
use Server\SInterface\OnCloseInterface;
use Server\SInterface\OnConnectInterface;
use Server\SInterface\OnReceiveInterface;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use Swoole\Server;
@@ -33,8 +40,8 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
public Router $router;
#[Inject(Annotation::class)]
public Annotation $annotation;
#[Inject(Note::class)]
public Note $annotation;
#[Inject(EventProvider::class)]
@@ -58,6 +65,8 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
/**
* @param OnBeforeShutdown $beforeShutdown
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function onBeforeShutdown(OnBeforeShutdown $beforeShutdown)
{
@@ -70,7 +79,10 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
/**
* @param OnStart $server
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function register(OnStart $server)
{
@@ -174,7 +186,9 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
if (is_null($handler)) {
throw new \Exception('Method not found', -32601);
} else {
return $this->handler($handler, $data);
$PsrRequest = Context::setContext(RequestInterface::class, $this->createServerRequest($params));
return $this->handler($handler, $PsrRequest);
}
} catch (\Throwable $throwable) {
$code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode();
@@ -184,17 +198,31 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
/**
* @param array $handler
* @param $data
* @return array
* @param $params
* @return ServerRequestInterface
* @throws \Exception
*/
private function handler(array $handler, $data): array
private function createServerRequest($params): ServerRequestInterface
{
return (new ServerRequest())->withParsedBody($params);
}
/**
* @param array $handler
* @param $request
* @return array
* @throws \ReflectionException
*/
private function handler(array $handler, $request): array
{
$controller = Kiri::getDi()->get($handler[0]);
$dispatcher = $controller->{$handler[1]}(...$data['params']);
return ['jsonrpc' => '2.0', 'result' => $dispatcher, 'id' => $data['id'] ?? null];
$dispatcher = $controller->{$handler[1]}($request);
return [
'jsonrpc' => '2.0',
'result' => $dispatcher,
'id' => $data['id'] ?? null
];
}
+7
View File
@@ -8,6 +8,10 @@ use ReflectionException;
class RpcManager
{
/**
* @var array
*/
private static array $_rpc = [];
@@ -34,6 +38,9 @@ class RpcManager
}
/**
* @return array
*/
public static function doneList(): array
{
$array = [];
+14 -26
View File
@@ -14,6 +14,9 @@ trait TraitTransporter
protected array $config;
protected array $clients = [];
/**
* @param $config
* @return $this
@@ -48,24 +51,14 @@ trait TraitTransporter
*/
private function newClient(): Coroutine\Client|Client
{
if (Context::inCoroutine()) {
$client = $this->clientOnCoroutine($this->config);
} else {
$client = $this->clientNotCoroutine($this->config);
$alias = $this->alias($this->config);
$client = $this->clients[$alias] ?? null;
if (is_null($client)) {
$client = Context::inCoroutine() ? new Coroutine\Client(SWOOLE_SOCK_TCP) : new Client(SWOOLE_SOCK_TCP);
$this->clients[$alias] = $client;
}
return $client;
}
/**
* @param $config
* @return Coroutine\Client
* @throws Exception
*/
private function clientOnCoroutine($config): Coroutine\Client
{
$client = new Coroutine\Client(SWOOLE_SOCK_TCP);
if (!$client->connect($config['ServiceAddress'], $config['ServicePort'], 60)) {
[$host, $port] = [$this->config['ServiceAddress'], $this->config['ServicePort']];
if (!$client->isConnected() && !$client->connect($host, $port, 60)) {
throw new Exception('connect fail.');
}
return $client;
@@ -73,17 +66,12 @@ trait TraitTransporter
/**
* @param $config
* @return Client
* @throws Exception
* @param array $config
* @return string
*/
private function clientNotCoroutine($config): Client
private function alias(array $config): string
{
$client = new Client(SWOOLE_SOCK_TCP);
if (!$client->connect($config['ServiceAddress'], $config['ServicePort'], 60)) {
throw new Exception('connect fail.');
}
return $client;
return $config['ServiceAddress'] . '::' . $config['ServicePort'];
}