改名
This commit is contained in:
@@ -6,6 +6,8 @@ namespace HttpServer\Events;
|
||||
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use HttpServer\Http\Request;
|
||||
use HttpServer\Route\Router;
|
||||
use Snowflake\Application;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
@@ -25,6 +27,15 @@ class OnReceive extends Callback
|
||||
public string $host = '';
|
||||
|
||||
|
||||
private Router $router;
|
||||
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->router = Snowflake::app()->getRouter();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
@@ -36,9 +47,8 @@ class OnReceive extends Callback
|
||||
public function onHandler(Server $server, int $fd, int $reID, string $data): mixed
|
||||
{
|
||||
try {
|
||||
$request = Request::createListenRequest($fd, $server, $data, $reID);
|
||||
$router = Snowflake::app()->getRouter();
|
||||
if (($node = $router->find_path($request)) === null) {
|
||||
$request = Request::createListenRequest($fd, $data, $reID);
|
||||
if (($node = $this->router->find_path($request)) === null) {
|
||||
return $server->send($fd, Json::encode(['state' => 404]));
|
||||
}
|
||||
$dispatch = $node->dispatch();
|
||||
|
||||
@@ -35,6 +35,25 @@ class HttpHeaders
|
||||
$this->headers = $headers;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
*/
|
||||
public function setRequestUri(string $uri)
|
||||
{
|
||||
$this->headers['request_uri'] = $uri;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
*/
|
||||
public function setRequestMethod(string $method)
|
||||
{
|
||||
$this->headers['request_method'] = $method;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
|
||||
+21
-23
@@ -7,18 +7,14 @@ use Annotation\Route\Socket;
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\HttpService;
|
||||
use HttpServer\IInterface\AuthIdentity;
|
||||
|
||||
use HttpServer\Route\Router;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use ReflectionException;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Core\ArrayAccess;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use stdClass;
|
||||
use Swoole\Server;
|
||||
use function router;
|
||||
|
||||
defined('REQUEST_OK') or define('REQUEST_OK', 0);
|
||||
@@ -69,6 +65,7 @@ class Request extends HttpService
|
||||
const HTTP_POST = 'post';
|
||||
const HTTP_GET = 'get';
|
||||
const HTTP_CMD = 'rpc';
|
||||
const HTTP_LISTEN = 'listen';
|
||||
const HTTP_SOCKET = 'sw::socket';
|
||||
|
||||
|
||||
@@ -515,7 +512,6 @@ class Request extends HttpService
|
||||
|
||||
/**
|
||||
* @param $fd
|
||||
* @param Server $server
|
||||
* @param $data
|
||||
* @param int $reID
|
||||
* @return Request
|
||||
@@ -523,7 +519,7 @@ class Request extends HttpService
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function createListenRequest($fd, Server $server, $data, $reID = 0): Request
|
||||
public static function createListenRequest($fd, $data, $reID = 0): Request
|
||||
{
|
||||
/** @var Request $sRequest */
|
||||
$sRequest = Snowflake::createObject(Request::class);
|
||||
@@ -532,30 +528,31 @@ class Request extends HttpService
|
||||
$sRequest->clientInfo = self::getClientInfo($fd, $reID);
|
||||
$sRequest->startTime = microtime(true);
|
||||
$sRequest->headers = new HttpHeaders([]);
|
||||
$sRequest->params = new HttpParams($data, [], []);
|
||||
|
||||
$port = $sRequest->clientInfo['server_port'];
|
||||
|
||||
$rpc = Config::get('rpc.port', []);
|
||||
if ($rpc !== $port) {
|
||||
$sRequest->headers->replace('request_uri', 'add-port-listen/port_' . $port);
|
||||
$sRequest->headers->replace('request_method', 'listen');
|
||||
} else {
|
||||
static::rpc_service($sRequest, $data, $rpc);
|
||||
}
|
||||
$sRequest->params = new HttpParams($data, [], []);
|
||||
$sRequest->parseUri();
|
||||
$sRequest->headers->setRequestUri('add-port-listen/port_' . $port);
|
||||
$sRequest->headers->setRequestMethod(self::HTTP_LISTEN);
|
||||
|
||||
$sRequest->checkIsRpcClient()->parseUri();
|
||||
|
||||
return Context::setContext('request', $sRequest);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $sRequest
|
||||
* @param $data
|
||||
* @param $rpc
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
private static function rpc_service($sRequest, $data, $rpc)
|
||||
private function checkIsRpcClient(): static
|
||||
{
|
||||
$port = $this->clientInfo['server_port'];
|
||||
if (($rpc = Config::get('rpc.port', 0)) !== $port) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$data = $this->params->getBody();
|
||||
if (is_string($data) && is_null($data = Json::decode($data))) {
|
||||
throw new Exception('Protocol format error.');
|
||||
}
|
||||
@@ -568,9 +565,10 @@ class Request extends HttpService
|
||||
$data['cmd'] = ltrim($data['cmd'], '/');
|
||||
}
|
||||
|
||||
$sRequest->params = new HttpParams(['body' => $data], [], []);
|
||||
$sRequest->headers->replace('request_uri', 'rpc/p' . $rpc . '/' . $data['cmd']);
|
||||
$sRequest->headers->replace('request_method', Request::HTTP_CMD);
|
||||
$this->headers->setRequestUri('rpc/p' . $rpc . '/' . $data['cmd']);
|
||||
$this->headers->setRequestMethod(Request::HTTP_CMD);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user