Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b279e56efd | |||
| 92a1e68bec | |||
| e8b9387252 | |||
| 46e6e2b2c2 | |||
| aa94fea000 | |||
| c6973dbe83 | |||
| 62e0eabe4f | |||
| adaa4ba14d | |||
| ddb55ee5a0 | |||
| 0228ed2241 | |||
| 168ceac6a8 | |||
| 40f1a49e77 | |||
| 3c3d0a4107 |
@@ -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('@'));
|
||||
|
||||
}
|
||||
@@ -11,7 +11,6 @@
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"ext-json": "*",
|
||||
"game-worker/kiri-consul": "^v1.0",
|
||||
"psr/http-client": "^1.0",
|
||||
"psr/http-message": "^1.0"
|
||||
},
|
||||
|
||||
@@ -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
@@ -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
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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
@@ -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'];
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user