改名
This commit is contained in:
@@ -545,21 +545,17 @@ class Router extends HttpService implements RouterInterface
|
||||
*/
|
||||
public function find_path(Request $request): ?Node
|
||||
{
|
||||
// return $this->Branch_search($request);
|
||||
$method = $request->getMethod();
|
||||
$uri = $request->headers->get('request_uri', '/');
|
||||
|
||||
if (!isset(static::$nodes[$method])) {
|
||||
return null;
|
||||
}
|
||||
$methods = static::$nodes[$method];
|
||||
if (isset($methods[$uri])) {
|
||||
$methods = static::$nodes[$method][$uri] ?? null;
|
||||
if (!is_null($methods[$uri])) {
|
||||
return $methods[$uri];
|
||||
}
|
||||
if (!$request->isOption || !isset($methods['/'])) {
|
||||
return null;
|
||||
if ($request->isOption) {
|
||||
return static::$nodes[$method]['*'] ?? null;
|
||||
}
|
||||
return $methods['/'];
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -92,11 +92,14 @@ class Server extends HttpService
|
||||
*/
|
||||
private function rpcListener($rpcService)
|
||||
{
|
||||
$rpcService['events'][Constant::CONNECT] = [Service::class, 'onConnect'];
|
||||
$rpcService['events'][Constant::DISCONNECT] = [Service::class, 'onClose'];
|
||||
$rpcService['events'][Constant::CLOSE] = [Service::class, 'onClose'];
|
||||
$rpcService['events'][Constant::RECEIVE] = [Service::class, 'onReceive'];
|
||||
$rpcService['events'][Constant::PACKET] = [Service::class, 'onPacket'];
|
||||
if (in_array($rpcService['mode'], [SWOOLE_SOCK_UDP, SWOOLE_UDP, SWOOLE_UDP6, SWOOLE_SOCK_UDP6])) {
|
||||
$rpcService['events'][Constant::PACKET] = [Service::class, 'onPacket'];
|
||||
} else {
|
||||
$rpcService['events'][Constant::RECEIVE] = [Service::class, 'onReceive'];
|
||||
$rpcService['events'][Constant::CONNECT] = [Service::class, 'onConnect'];
|
||||
$rpcService['events'][Constant::DISCONNECT] = [Service::class, 'onDisconnect'];
|
||||
$rpcService['events'][Constant::CLOSE] = [Service::class, 'onClose'];
|
||||
}
|
||||
$this->manager->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'], $rpcService['mode'], $rpcService);
|
||||
}
|
||||
|
||||
|
||||
+82
-90
@@ -7,15 +7,9 @@ use Exception;
|
||||
use HttpServer\Http\Context;
|
||||
use HttpServer\Http\Request;
|
||||
use HttpServer\Route\Router;
|
||||
use HttpServer\Service\Http;
|
||||
use HttpServer\Service\Packet;
|
||||
use HttpServer\Service\Receive;
|
||||
use HttpServer\Service\Websocket;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Server\Constant;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
use function Swoole\Coroutine\defer;
|
||||
@@ -25,32 +19,22 @@ use function Swoole\Coroutine\defer;
|
||||
* Class Service
|
||||
* @package Rpc
|
||||
*/
|
||||
class Service extends Component
|
||||
class Service extends \Server\Abstracts\Server
|
||||
{
|
||||
|
||||
const defaultConfig = [
|
||||
'open_tcp_keepalive' => true,
|
||||
'tcp_keepidle' => 30,
|
||||
'tcp_keepinterval' => 10,
|
||||
'tcp_keepcount' => 10,
|
||||
'open_http_protocol' => false,
|
||||
'open_websocket_protocol' => false,
|
||||
];
|
||||
const RPC_CONNECT = 'RPC::CONNECT';
|
||||
const RPC_CLOSE = 'RPC::CLOSE';
|
||||
|
||||
private Router $router;
|
||||
|
||||
|
||||
const RPC_CONNECT = 'RPC::CONNECT';
|
||||
const RPC_CLOSE = 'RPC::CLOSE';
|
||||
|
||||
private Router $router;
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->router = Snowflake::getApp('router');
|
||||
}
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->router = Snowflake::getApp('router');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -59,16 +43,12 @@ class Service extends Component
|
||||
* @param int $reactorId
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onConnect(Server $server, int $fd, int $reactorId)
|
||||
{
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
public function onConnect(Server $server, int $fd, int $reactorId)
|
||||
{
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
|
||||
$config = $server->setting['enable_delay_receive'] ?? null;
|
||||
if ($config === true) {
|
||||
$server->confirm($fd);
|
||||
}
|
||||
Event::trigger(Service::RPC_CONNECT, [$server, $fd, $reactorId]);
|
||||
}
|
||||
$this->runEvent(Constant::CONNECT, null, [$server, $fd, $reactorId]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -77,37 +57,50 @@ class Service extends Component
|
||||
* on tcp client close
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onClose(Server $server, int $fd)
|
||||
{
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
public function onClose(Server $server, int $fd)
|
||||
{
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
|
||||
Event::trigger(Service::RPC_CLOSE, [$server, $fd]);
|
||||
}
|
||||
$this->runEvent(Constant::CLOSE, null, [$server, $fd]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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);
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* on tcp client close
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onDisconnect(Server $server, int $fd)
|
||||
{
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
|
||||
$request = $this->requestSpl((int)$client['server_port'], $data);
|
||||
$this->runEvent(Constant::DISCONNECT, null, [$server, $fd]);
|
||||
}
|
||||
|
||||
$result = $this->router->find_path($request)?->dispatch();
|
||||
|
||||
$server->send($fd, $result);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception, 'rpc-service');
|
||||
$server->send($fd, $exception->getMessage());
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @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);
|
||||
|
||||
$request = $this->requestSpl((int)$client['server_port'], $data);
|
||||
|
||||
$result = $this->router->find_path($request)?->dispatch();
|
||||
|
||||
$server->send($fd, $result);
|
||||
} catch (\Throwable $exception) {
|
||||
$server->send($fd, $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -116,20 +109,19 @@ class Service extends Component
|
||||
* @param array $client
|
||||
* @throws Exception
|
||||
*/
|
||||
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);
|
||||
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);
|
||||
|
||||
$result = $this->router->find_path($request)?->dispatch();
|
||||
$result = $this->router->find_path($request)?->dispatch();
|
||||
|
||||
$server->sendto($client['address'], $client['port'], $result);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception, 'rpc-service');
|
||||
$server->sendto($client['address'], $client['port'], $exception->getMessage());
|
||||
}
|
||||
}
|
||||
$server->sendto($client['address'], $client['port'], $result);
|
||||
} catch (\Throwable $exception) {
|
||||
$server->sendto($client['address'], $client['port'], $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -138,25 +130,25 @@ class Service extends Component
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function requestSpl(int $server_port, string $data): mixed
|
||||
{
|
||||
$sRequest = new Request();
|
||||
public function requestSpl(int $server_port, string $data): mixed
|
||||
{
|
||||
$sRequest = new Request();
|
||||
|
||||
[$cmd, $repeat, $body] = explode("\n", $data);
|
||||
if (is_null($body) || is_null($cmd) || !empty($repeat)) {
|
||||
throw new Exception('Protocol format error.');
|
||||
}
|
||||
[$cmd, $repeat, $body] = explode("\n", $data);
|
||||
if (is_null($body) || is_null($cmd) || !empty($repeat)) {
|
||||
throw new Exception('Protocol format error.');
|
||||
}
|
||||
|
||||
if (is_string($body) && is_null($data = Json::decode($body))) {
|
||||
throw new Exception('Protocol format error.');
|
||||
}
|
||||
if (is_string($body) && is_null($data = Json::decode($body))) {
|
||||
throw new Exception('Protocol format error.');
|
||||
}
|
||||
|
||||
$sRequest->params->setPosts($data);
|
||||
$sRequest->headers->setRequestUri('rpc/p' . $server_port . '/' . ltrim($cmd, '/'));
|
||||
$sRequest->headers->setRequestMethod(Request::HTTP_CMD);
|
||||
$sRequest->params->setPosts($data);
|
||||
$sRequest->headers->setRequestUri('rpc/p' . $server_port . '/' . ltrim($cmd, '/'));
|
||||
$sRequest->headers->setRequestMethod(Request::HTTP_CMD);
|
||||
|
||||
return Context::setContext('request', $sRequest);
|
||||
}
|
||||
return Context::setContext('request', $sRequest);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user