This commit is contained in:
2021-03-30 11:10:24 +08:00
parent e8f63aefc3
commit 6a9b828bd4
2 changed files with 36 additions and 63 deletions
+36 -6
View File
@@ -11,6 +11,7 @@ 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;
@@ -521,20 +522,49 @@ class Request extends HttpService
$sRequest->fd = is_array($fd) ? 0 : $fd;
$sRequest->clientInfo = self::getClientInfo($fd, $reID);
$sRequest->startTime = microtime(true);
$sRequest->headers = new HttpHeaders([]);
$port = $sRequest->clientInfo['server_port'];
$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);
$rpc = Config::get('rpc.port', false, []);
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();
return Context::setContext('request', $sRequest);
}
/**
* @param $sRequest
* @param $data
* @param $rpc
* @throws Exception
*/
private static function rpc_service($sRequest, $data, $rpc)
{
if (is_string($data) && is_null($data = Json::decode($data))) {
throw new Exception('Protocol format error.');
}
if (!isset($data['cmd'])) {
throw new Exception('Unknown system cmd.');
}
if (str_starts_with($data['cmd'], '/')) {
$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);
}
/**
* @param $fd
* @param int $re
-57
View File
@@ -62,7 +62,6 @@ class Service extends Component
'open_http_protocol' => false,
'open_websocket_protocol' => false,
]);
$this->listenPort($service, $mode);
}
@@ -103,60 +102,4 @@ class Service extends Component
}
/**
* @param $service
* @param $mode
* @throws Exception
*/
private function listenPort($service, $mode)
{
router()->addPortListen($service['port'], function () use ($service, $mode) {
try {
/** @var Request $request */
$request = Context::getContext('request');
if (($node = router()->find_path(Service::replace($request, $service))) === null) {
throw new Exception('Cmd not find.');
}
$response = $node->dispatch();
if (is_string($response)) {
return $response;
}
return Json::encode($response);
} catch (\Throwable $exception) {
$this->addError($exception);
return Json::encode(['state' => 'fail', 'message' => $exception->getMessage()]);
}
});
}
/**
* @param Request $request
* @param array $service
* @return Request
* @throws Exception
*/
public static function replace(Request $request, array $service): Request
{
$body = $request->params->getBodyAndClear();
if (is_string($body) && is_null($body = Json::decode($body))) {
throw new Exception('Protocol format error.');
}
if (!isset($body['cmd'])) {
throw new Exception('Unknown system cmd.');
}
$request->params->setPosts($body);
$body['cmd'] = ltrim($body['cmd'], '/');
$header = $request->headers;
$header->replace('request_uri', 'rpc/p' . $service['port'] . '/' . $body['cmd']);
$header->replace('request_method', Request::HTTP_CMD);
$request->parseUri();
return $request;
}
}