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