改名
This commit is contained in:
+28
-14
@@ -88,9 +88,7 @@ class Request extends HttpService
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
* @throws ComponentException
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getConnectInfo(): array|null
|
||||
{
|
||||
@@ -112,9 +110,9 @@ class Request extends HttpService
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @return AuthIdentity|null
|
||||
*/
|
||||
public function getIdentity(): mixed
|
||||
public function getIdentity(): ?AuthIdentity
|
||||
{
|
||||
return $this->_grant;
|
||||
}
|
||||
@@ -237,7 +235,7 @@ class Request extends HttpService
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws ComponentException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function adapter(): mixed
|
||||
{
|
||||
@@ -398,7 +396,7 @@ class Request extends HttpService
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRuntime(): string
|
||||
#[Pure] public function getRuntime(): string
|
||||
{
|
||||
return sprintf('%.5f', microtime(TRUE) - $this->startTime);
|
||||
}
|
||||
@@ -514,26 +512,42 @@ class Request extends HttpService
|
||||
* @return Request
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function createListenRequest($fd, $port, Server $server, $data, $reID = 0): Request
|
||||
{
|
||||
/** @var Request $sRequest */
|
||||
$sRequest = Snowflake::createObject(Request::class);
|
||||
if (is_array($fd)) {
|
||||
$sRequest->fd = 0;
|
||||
$sRequest->clientInfo = $fd;
|
||||
} else {
|
||||
$sRequest->fd = $fd;
|
||||
$sRequest->clientInfo = $server->getClientInfo($fd, $reID);
|
||||
}
|
||||
|
||||
$sRequest->fd = is_array($fd) ? 0 : $fd;
|
||||
$sRequest->clientInfo = self::getClientInfo($fd, $reID);
|
||||
$sRequest->startTime = microtime(true);
|
||||
|
||||
$sRequest->params = new HttpParams(['body' => $data], [], []);
|
||||
|
||||
$sRequest->headers = new HttpHeaders([]);
|
||||
$sRequest->headers->replace('request_method', 'listen');
|
||||
$sRequest->headers->replace('request_uri', 'add-port-listen/port_' . $port);
|
||||
$sRequest->parseUri();
|
||||
|
||||
return Context::setContext('request', $sRequest);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $fd
|
||||
* @param int $re
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
private static function getClientInfo($fd, $re = 0): mixed
|
||||
{
|
||||
$server = Snowflake::app()->getSwoole();
|
||||
if (!is_array($fd)) {
|
||||
return $server->getClientInfo($fd, $re);
|
||||
}
|
||||
return $fd;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -576,17 +576,18 @@ class Node extends HttpService
|
||||
private function httpFilter(): mixed
|
||||
{
|
||||
try {
|
||||
if (!is_array($this->handler)) {
|
||||
if ($this->handler instanceof Closure) {
|
||||
return $this->runWith(...func_get_args());
|
||||
}
|
||||
|
||||
/** @var HttpFilter $filter */
|
||||
$filter = Snowflake::app()->get('filter');
|
||||
$validator = $filter->check(get_class($this->handler[0]), $this->handler[1]);
|
||||
if (!($validator instanceof Validator)) {
|
||||
return $this->runWith(...func_get_args());
|
||||
}
|
||||
|
||||
if (!$validator->validation()) {
|
||||
var_dump($validator->getError());
|
||||
return Json::to(401, $validator->getError());
|
||||
}
|
||||
return $this->runWith(...func_get_args());
|
||||
|
||||
+60
-19
@@ -3,13 +3,23 @@
|
||||
namespace Rpc;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Http\Context;
|
||||
use HttpServer\Http\HttpHeaders;
|
||||
use HttpServer\Http\HttpParams;
|
||||
use HttpServer\Http\Request;
|
||||
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 Snowflake\Core\Json;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
/**
|
||||
@@ -23,26 +33,57 @@ class Service extends Component
|
||||
/**
|
||||
* @param Packet|Websocket|Receive|Http|null $server
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function instance(Packet|Websocket|Receive|null|Http $server): void
|
||||
{
|
||||
$services = Config::get('rpc.service', false, []);
|
||||
if (empty($services)) {
|
||||
return;
|
||||
}
|
||||
foreach ($services as $service) {
|
||||
$mode = $service['mode'] ?? SWOOLE_SOCK_TCP6;
|
||||
$rpcServer = $server->addlistener($service['host'], $service['port'], $mode);
|
||||
$rpcServer->set([
|
||||
'open_tcp_keepalive' => true,
|
||||
'tcp_keepidle' => 30,
|
||||
'tcp_keepinterval' => 10,
|
||||
'tcp_keepcount' => 10,
|
||||
'open_http_protocol' => false,
|
||||
'open_websocket_protocol' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
public function instance(Packet|Websocket|Receive|null|Http $server): void
|
||||
{
|
||||
$services = Config::get('rpc.service', false, []);
|
||||
if (empty($services)) {
|
||||
return;
|
||||
}
|
||||
$router = Snowflake::app()->getRouter();
|
||||
foreach ($services as $service) {
|
||||
$this->addService($router, $server, $service);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $router
|
||||
* @param $server
|
||||
* @param $service
|
||||
* @throws Exception
|
||||
*/
|
||||
private function addService($router, $server, $service)
|
||||
{
|
||||
$mode = $service['mode'] ?? SWOOLE_SOCK_TCP6;
|
||||
$rpcServer = $server->addlistener($service['host'], $service['port'], $mode);
|
||||
$rpcServer->set([
|
||||
'open_tcp_keepalive' => true,
|
||||
'tcp_keepidle' => 30,
|
||||
'tcp_keepinterval' => 10,
|
||||
'tcp_keepcount' => 10,
|
||||
'open_http_protocol' => false,
|
||||
'open_websocket_protocol' => false,
|
||||
]);
|
||||
$router->addPortListen($service['port'], function () use ($service, $mode) {
|
||||
try {
|
||||
$request = Context::getContext('request');
|
||||
$request->headers->replace('request_method', Request::HTTP_CMD);
|
||||
|
||||
$router = Snowflake::app()->getRouter();
|
||||
if (($node = $router->find_path($request)) === null) {
|
||||
throw new Exception('Cmd not find.');
|
||||
}
|
||||
return $node->dispatch();
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception);
|
||||
return serialize(['state' => 'fail', 'message' => $exception->getMessage()]);
|
||||
} finally {
|
||||
fire(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user