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