Files
kiri-core/Rpc/Service.php
T

155 lines
3.2 KiB
PHP
Raw Normal View History

2021-03-23 02:29:48 +08:00
<?php
namespace Rpc;
2021-03-23 11:22:25 +08:00
use Exception;
use HttpServer\Http\Context;
use HttpServer\Http\Request;
2021-07-11 03:57:25 +08:00
use HttpServer\Route\Router;
2021-07-21 17:55:34 +08:00
use Server\Constant;
2021-03-23 11:22:25 +08:00
use Snowflake\Core\Json;
2021-07-11 03:57:25 +08:00
use Snowflake\Event;
2021-03-23 11:22:25 +08:00
use Snowflake\Snowflake;
2021-07-11 03:57:25 +08:00
use Swoole\Server;
use function Swoole\Coroutine\defer;
2021-03-23 02:29:48 +08:00
/**
* Class Service
* @package Rpc
*/
2021-07-21 17:55:34 +08:00
class Service extends \Server\Abstracts\Server
2021-03-23 02:29:48 +08:00
{
2021-07-21 17:55:34 +08:00
const RPC_CONNECT = 'RPC::CONNECT';
const RPC_CLOSE = 'RPC::CLOSE';
2021-07-11 02:05:56 +08:00
2021-07-21 17:55:34 +08:00
private Router $router;
2021-07-11 03:57:25 +08:00
2021-07-21 17:55:34 +08:00
/**
* @throws Exception
*/
public function init()
{
$this->router = Snowflake::getApp('router');
}
2021-07-11 03:57:25 +08:00
2021-07-21 17:55:34 +08:00
/**
* @param Server $server
* @param int $fd
* @param int $reactorId
* @throws Exception
*/
public function onConnect(Server $server, int $fd, int $reactorId)
{
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
$this->runEvent(Constant::CONNECT, null, [$server, $fd, $reactorId]);
}
2021-07-11 03:57:25 +08:00
2021-07-12 10:13:53 +08:00
/**
* @param Server $server
* @param int $fd
2021-07-21 17:55:34 +08:00
* on tcp client close
2021-07-12 10:13:53 +08:00
* @throws Exception
*/
2021-07-21 17:55:34 +08:00
public function onClose(Server $server, int $fd)
{
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
2021-07-11 03:57:25 +08:00
2021-07-21 17:55:34 +08:00
$this->runEvent(Constant::CLOSE, null, [$server, $fd]);
}
2021-07-11 03:57:25 +08:00
2021-07-12 10:13:53 +08:00
/**
* @param Server $server
* @param int $fd
* on tcp client close
* @throws Exception
*/
2021-07-21 17:55:34 +08:00
public function onDisconnect(Server $server, int $fd)
{
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
2021-07-11 02:05:56 +08:00
2021-07-21 17:55:34 +08:00
$this->runEvent(Constant::DISCONNECT, null, [$server, $fd]);
}
2021-07-11 02:05:56 +08:00
2021-07-21 17:55:34 +08:00
/**
* @param Server $server
* @param int $fd
* @param int $reID
* @param string $data
* @throws Exception
*/
public function onReceive(Server $server, int $fd, int $reID, string $data)
{
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
try {
$client = $server->getClientInfo($fd, $reID);
2021-07-11 03:57:25 +08:00
2021-07-21 17:55:34 +08:00
$request = $this->requestSpl((int)$client['server_port'], $data);
2021-07-11 03:57:25 +08:00
2021-07-21 17:55:34 +08:00
$result = $this->router->find_path($request)?->dispatch();
2021-07-11 03:57:25 +08:00
2021-07-21 17:55:34 +08:00
$server->send($fd, $result);
} catch (\Throwable $exception) {
$server->send($fd, $exception->getMessage());
}
}
2021-07-11 02:05:56 +08:00
2021-07-12 10:13:53 +08:00
/**
* @param Server $server
* @param string $data
* @param array $client
* @throws Exception
*/
2021-07-21 17:55:34 +08:00
public function onPacket(Server $server, string $data, array $client)
{
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
try {
$request = $this->requestSpl((int)$client['server_port'], $data);
2021-07-11 03:57:25 +08:00
2021-07-21 17:55:34 +08:00
$result = $this->router->find_path($request)?->dispatch();
2021-07-11 03:57:25 +08:00
2021-07-21 17:55:34 +08:00
$server->sendto($client['address'], $client['port'], $result);
} catch (\Throwable $exception) {
$server->sendto($client['address'], $client['port'], $exception->getMessage());
}
}
2021-03-29 10:38:47 +08:00
2021-07-12 10:13:53 +08:00
/**
* @param int $server_port
* @param string $data
* @return mixed
* @throws Exception
*/
2021-07-21 17:55:34 +08:00
public function requestSpl(int $server_port, string $data): mixed
{
$sRequest = new Request();
2021-07-11 03:57:25 +08:00
2021-07-21 17:55:34 +08:00
[$cmd, $repeat, $body] = explode("\n", $data);
if (is_null($body) || is_null($cmd) || !empty($repeat)) {
throw new Exception('Protocol format error.');
}
2021-07-11 03:57:25 +08:00
2021-07-21 17:55:34 +08:00
if (is_string($body) && is_null($data = Json::decode($body))) {
throw new Exception('Protocol format error.');
}
2021-07-11 03:57:25 +08:00
2021-07-21 17:55:34 +08:00
$sRequest->params->setPosts($data);
$sRequest->headers->setRequestUri('rpc/p' . $server_port . '/' . ltrim($cmd, '/'));
$sRequest->headers->setRequestMethod(Request::HTTP_CMD);
2021-07-11 03:57:25 +08:00
2021-07-21 17:55:34 +08:00
return Context::setContext('request', $sRequest);
}
2021-07-11 03:57:25 +08:00
2021-03-23 02:29:48 +08:00
}