This commit is contained in:
2021-08-31 19:12:57 +08:00
parent 302247866b
commit 7851259003
4 changed files with 127 additions and 16 deletions
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace Kiri\Rpc\Annotation;
use Annotation\Attribute;
use Kiri\Kiri;
use Kiri\Rpc\RpcManager;
use ReflectionException;
/**
*
*/
#[\Attribute(\Attribute::TARGET_CLASS)] class RpcProduct extends Attribute
{
/**
* @param string $cmd
* @param string $protocol
*/
public function __construct(public string $cmd, public string $protocol)
{
}
/**
* @param mixed $class
* @param mixed|string $method
* @return mixed
* @throws ReflectionException
*/
public function execute(mixed $class, mixed $method = ''): mixed
{
$class = Kiri::getDi()->get($class);
RpcManager::addCmdHandler($this->cmd, [$class, $method], $this->protocol);
return parent::execute($class, $method); // TODO: Change the autogenerated stub
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace Kiri\Rpc;
class Protocol
{
const SPLIT_STRING = "\r\r\n\n";
/**
* @param string $data
* @return array|null
*/
public static function parse(string $data)
{
if (!str_contains($data, Protocol::SPLIT_STRING)) {
return null;
}
[$cmd, $requestBody] = explode(Protocol::SPLIT_STRING, $data);
return [$cmd, json_decode($requestBody, true)];
}
/**
* @param string $cmd
* @param array $data
* @return string
*/
public static function create(string $cmd, array $data): string
{
return implode("\r\r\n\n", [$cmd, json_encode($data, JSON_UNESCAPED_UNICODE)]);
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace Kiri\Rpc;
class RpcManager
{
private static array $_handler = [];
/**
* @param $cmd
* @param array $handler
* @param string $protocol
*/
public static function addCmdHandler($cmd, array $handler, string $protocol)
{
static::$_handler[$cmd] = [$handler, $protocol];
}
/**
* @param $cmd
* @return array|null
*/
public static function getHandler($cmd): ?array
{
return static::$_handler[$cmd] ?? null;
}
}
+15 -16
View File
@@ -3,13 +3,12 @@
namespace Kiri\Rpc;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use ReflectionException;
use Server\Abstracts\Tcp;
use Server\Constant;
use Server\ServerManager;
use Server\SInterface\OnClose;
use Server\SInterface\OnConnect;
use Server\SInterface\OnPacket;
use Server\SInterface\OnReceive;
use Server\SInterface\OnRequest;
use Swoole\Http\Request;
@@ -20,7 +19,7 @@ use Swoole\Server;
/**
*
*/
class Service implements OnClose, OnConnect, OnReceive, OnPacket, OnRequest
class Service extends Tcp implements OnClose, OnConnect, OnReceive, OnRequest
{
@@ -32,6 +31,7 @@ class Service implements OnClose, OnConnect, OnReceive, OnPacket, OnRequest
*/
public static function addRpcListener(ServerManager $manager, array $config)
{
$config['settings']['enable_delay_receive'] = true;
$config['settings']['enable_unsafe_event'] = true;
$config['events'][Constant::RECEIVE] = [Service::class, 'onReceive'];
$implements = class_implements(Service::class);
@@ -74,18 +74,7 @@ class Service implements OnClose, OnConnect, OnReceive, OnPacket, OnRequest
*/
public function onConnect(Server $server, int $fd): void
{
// TODO: Implement onConnect() method.
}
/**
* @param Server|\Server\Abstracts\Server $server
* @param string $data
* @param array $clientInfo
*/
public function onPacket(Server|\Server\Abstracts\Server $server, string $data, array $clientInfo): void
{
// TODO: Implement onPacket() method.
$server->confirm($fd);
}
@@ -97,7 +86,17 @@ class Service implements OnClose, OnConnect, OnReceive, OnPacket, OnRequest
*/
public function onReceive(Server $server, int $fd, int $reactor_id, string $data): void
{
// TODO: Implement onReceive() method.
try {
// TODO: Implement onReceive() method.
[$cmd, [$body, $protocol]] = Protocol::parse($data);
} catch(\Throwable $throwable){
}
}