Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b279e56efd | |||
| 92a1e68bec | |||
| e8b9387252 | |||
| 46e6e2b2c2 | |||
| aa94fea000 | |||
| c6973dbe83 | |||
| 62e0eabe4f | |||
| adaa4ba14d | |||
| ddb55ee5a0 | |||
| 0228ed2241 | |||
| 168ceac6a8 | |||
| 40f1a49e77 |
@@ -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": {
|
"require": {
|
||||||
"php": ">=8.0",
|
"php": ">=8.0",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"game-worker/kiri-consul": "^v1.0",
|
|
||||||
"psr/http-client": "^1.0",
|
"psr/http-client": "^1.0",
|
||||||
"psr/http-message": "^1.0"
|
"psr/http-message": "^1.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Kiri\Rpc;
|
namespace Kiri\Rpc;
|
||||||
|
|
||||||
use Annotation\Inject;
|
use Note\Inject;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Http\Message\Response;
|
use Http\Message\Response;
|
||||||
use Http\Message\Stream;
|
use Http\Message\Stream;
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Kiri\Rpc\Annotation;
|
namespace Kiri\Rpc\Note;
|
||||||
|
|
||||||
use Annotation\Attribute;
|
use Note\Attribute;
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Consul\Agent;
|
use Kiri\Consul\Agent;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
+44
-16
@@ -2,21 +2,28 @@
|
|||||||
|
|
||||||
namespace Kiri\Rpc;
|
namespace Kiri\Rpc;
|
||||||
|
|
||||||
use Annotation\Annotation;
|
use Note\Note;
|
||||||
use Annotation\Inject;
|
use Note\Inject;
|
||||||
|
use Http\Constrict\RequestInterface;
|
||||||
use Http\Handler\Router;
|
use Http\Handler\Router;
|
||||||
|
use Http\Message\ServerRequest;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Consul\Agent;
|
use Kiri\Consul\Agent;
|
||||||
use Kiri\Di\ContainerInterface;
|
use Kiri\Context;
|
||||||
|
use Kiri\Core\Json;
|
||||||
use Kiri\Events\EventProvider;
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
use Server\Events\OnBeforeShutdown;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Server\Events\OnStart;
|
use Psr\Container\ContainerInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Server\Contract\OnCloseInterface;
|
use Server\Contract\OnCloseInterface;
|
||||||
use Server\Contract\OnConnectInterface;
|
use Server\Contract\OnConnectInterface;
|
||||||
use Server\Contract\OnReceiveInterface;
|
use Server\Contract\OnReceiveInterface;
|
||||||
|
use Server\Events\OnBeforeShutdown;
|
||||||
|
use Server\Events\OnStart;
|
||||||
use Swoole\Coroutine;
|
use Swoole\Coroutine;
|
||||||
use Swoole\Coroutine\Channel;
|
use Swoole\Coroutine\Channel;
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
@@ -33,8 +40,8 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
|||||||
public Router $router;
|
public Router $router;
|
||||||
|
|
||||||
|
|
||||||
#[Inject(Annotation::class)]
|
#[Inject(Note::class)]
|
||||||
public Annotation $annotation;
|
public Note $annotation;
|
||||||
|
|
||||||
|
|
||||||
#[Inject(EventProvider::class)]
|
#[Inject(EventProvider::class)]
|
||||||
@@ -58,6 +65,8 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param OnBeforeShutdown $beforeShutdown
|
* @param OnBeforeShutdown $beforeShutdown
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
public function onBeforeShutdown(OnBeforeShutdown $beforeShutdown)
|
public function onBeforeShutdown(OnBeforeShutdown $beforeShutdown)
|
||||||
{
|
{
|
||||||
@@ -70,7 +79,10 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param OnStart $server
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
*/
|
*/
|
||||||
public function register(OnStart $server)
|
public function register(OnStart $server)
|
||||||
{
|
{
|
||||||
@@ -174,7 +186,9 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
|||||||
if (is_null($handler)) {
|
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);
|
$PsrRequest = Context::setContext(RequestInterface::class, $this->createServerRequest($params));
|
||||||
|
|
||||||
|
return $this->handler($handler, $PsrRequest);
|
||||||
}
|
}
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
$code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode();
|
$code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode();
|
||||||
@@ -184,17 +198,31 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $handler
|
* @param $params
|
||||||
* @param $data
|
* @return ServerRequestInterface
|
||||||
* @return array
|
* @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]);
|
$controller = Kiri::getDi()->get($handler[0]);
|
||||||
|
$dispatcher = $controller->{$handler[1]}($request);
|
||||||
$dispatcher = $controller->{$handler[1]}(...$data['params']);
|
return [
|
||||||
|
'jsonrpc' => '2.0',
|
||||||
return ['jsonrpc' => '2.0', 'result' => $dispatcher, 'id' => $data['id'] ?? null];
|
'result' => $dispatcher,
|
||||||
|
'id' => $data['id'] ?? null
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ use ReflectionException;
|
|||||||
class RpcManager
|
class RpcManager
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
private static array $_rpc = [];
|
private static array $_rpc = [];
|
||||||
|
|
||||||
|
|
||||||
@@ -34,6 +38,9 @@ class RpcManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public static function doneList(): array
|
public static function doneList(): array
|
||||||
{
|
{
|
||||||
$array = [];
|
$array = [];
|
||||||
|
|||||||
+14
-26
@@ -14,6 +14,9 @@ trait TraitTransporter
|
|||||||
protected array $config;
|
protected array $config;
|
||||||
|
|
||||||
|
|
||||||
|
protected array $clients = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $config
|
* @param $config
|
||||||
* @return $this
|
* @return $this
|
||||||
@@ -48,24 +51,14 @@ trait TraitTransporter
|
|||||||
*/
|
*/
|
||||||
private function newClient(): Coroutine\Client|Client
|
private function newClient(): Coroutine\Client|Client
|
||||||
{
|
{
|
||||||
if (Context::inCoroutine()) {
|
$alias = $this->alias($this->config);
|
||||||
$client = $this->clientOnCoroutine($this->config);
|
$client = $this->clients[$alias] ?? null;
|
||||||
} else {
|
if (is_null($client)) {
|
||||||
$client = $this->clientNotCoroutine($this->config);
|
$client = Context::inCoroutine() ? new Coroutine\Client(SWOOLE_SOCK_TCP) : new Client(SWOOLE_SOCK_TCP);
|
||||||
|
$this->clients[$alias] = $client;
|
||||||
}
|
}
|
||||||
return $client;
|
[$host, $port] = [$this->config['ServiceAddress'], $this->config['ServicePort']];
|
||||||
}
|
if (!$client->isConnected() && !$client->connect($host, $port, 60)) {
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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)) {
|
|
||||||
throw new Exception('connect fail.');
|
throw new Exception('connect fail.');
|
||||||
}
|
}
|
||||||
return $client;
|
return $client;
|
||||||
@@ -73,17 +66,12 @@ trait TraitTransporter
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $config
|
* @param array $config
|
||||||
* @return Client
|
* @return string
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
private function clientNotCoroutine($config): Client
|
private function alias(array $config): string
|
||||||
{
|
{
|
||||||
$client = new Client(SWOOLE_SOCK_TCP);
|
return $config['ServiceAddress'] . '::' . $config['ServicePort'];
|
||||||
if (!$client->connect($config['ServiceAddress'], $config['ServicePort'], 60)) {
|
|
||||||
throw new Exception('connect fail.');
|
|
||||||
}
|
|
||||||
return $client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user