Files
kiri-core/Rpc/Service.php
T

172 lines
3.7 KiB
PHP
Raw Normal View History

2021-03-23 02:29:48 +08:00
<?php
namespace Rpc;
2021-08-03 18:18:09 +08:00
use Annotation\Inject;
2021-03-23 11:22:25 +08:00
use Exception;
use HttpServer\Http\Context;
2021-07-11 03:57:25 +08:00
use HttpServer\Route\Router;
2021-07-21 17:55:34 +08:00
use Server\Constant;
2021-08-03 18:18:09 +08:00
use Server\Events\OnAfterRequest;
2021-03-23 11:22:25 +08:00
use Snowflake\Core\Json;
2021-08-03 18:18:09 +08:00
use Snowflake\Events\EventDispatch;
use Snowflake\Events\EventProvider;
2021-03-23 11:22:25 +08:00
use Snowflake\Snowflake;
2021-08-03 18:18:09 +08:00
use Swoole\Http\Request;
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-08-03 18:18:09 +08:00
/** @var EventProvider */
#[Inject(EventProvider::class)]
public EventProvider $eventProvider;
/** @var EventDispatch */
#[Inject(EventDispatch::class)]
public EventDispatch $eventDispatch;
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)
{
2021-08-03 18:18:09 +08:00
defer(fn() => $this->eventDispatch->dispatch(new OnAfterRequest()));
2021-07-21 17:55:34 +08:00
$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)
{
2021-08-03 18:18:09 +08:00
defer(fn() => $this->eventDispatch->dispatch(new OnAfterRequest()));
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)
{
2021-08-03 18:18:09 +08:00
defer(fn() => $this->eventDispatch->dispatch(new OnAfterRequest()));
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)
{
2021-08-03 18:18:09 +08:00
defer(fn() => $this->eventDispatch->dispatch(new OnAfterRequest()));
2021-07-21 17:55:34 +08:00
try {
$client = $server->getClientInfo($fd, $reID);
2021-07-11 03:57:25 +08:00
2021-08-03 18:18:09 +08:00
$request = $this->requestSpl((int)$client['server_port'], $data, $fd);
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)
{
2021-08-03 18:18:09 +08:00
defer(fn() => $this->eventDispatch->dispatch(new OnAfterRequest()));
2021-07-21 17:55:34 +08:00
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
2021-08-03 18:18:09 +08:00
* @param int $fd
2021-07-12 10:13:53 +08:00
* @return mixed
* @throws Exception
*/
2021-08-03 18:18:09 +08:00
public function requestSpl(int $server_port, string $data, int $fd = 0): \HttpServer\Http\Request
2021-07-21 17:55:34 +08:00
{
$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-08-03 18:18:09 +08:00
$sRequest->fd = $fd;
$sRequest->post = $data;
$sRequest->header['request_uri'] = 'rpc/p' . $server_port . '/' . ltrim($cmd, '/');
$sRequest->header['request_method'] = 'rpc';
Context::setContext(Request::class, $sRequest);
2021-07-11 03:57:25 +08:00
2021-08-03 18:18:09 +08:00
return \request();
2021-07-21 17:55:34 +08:00
}
2021-07-11 03:57:25 +08:00
2021-03-23 02:29:48 +08:00
}