This commit is contained in:
2021-07-12 10:13:53 +08:00
parent efb231a765
commit 20fbbeb666
3 changed files with 29 additions and 90 deletions
+1 -5
View File
@@ -35,11 +35,7 @@ class OnReceive extends Callback
$client = $server->getClientInfo($fd, $reID);
$name = $this->getName($client, Event::SERVER_RECEIVE);
if (Config::get('rpc.port', 0) == $client['server_port']) {
$result = router()->find_path(Request::rpcRequest($fd, $data, $reID))?->dispatch();
} else {
$result = Event::trigger($name, [$server, $data, $client]);
}
$result = Event::trigger($name, [$server, $data, $client]);
if (is_array($result) || is_object($result)) {
$result = Json::encode($result);
}
-56
View File
@@ -495,60 +495,4 @@ class Request extends HttpService
}
/**
* @param $fd
* @param $data
* @param int $reID
* @return mixed|null
* @throws ConfigException
* @throws Exception
*/
public static function rpcRequest($fd, $data, int $reID = 0): Request|null
{
$sRequest = new Request();
$sRequest->fd = is_array($fd) ? 0 : $fd;
$sRequest->clientInfo = self::getClientInfo($fd, $reID);
$sRequest->startTime = microtime(true);
$sRequest->headers = new HttpHeaders([]);
$sRequest->params = new HttpParams($data, [], []);
$port = $sRequest->clientInfo['server_port'];
if (($rpc = Config::get('rpc.port', 0)) !== $port) {
return null;
}
[$cmd, $repeat, $body] = explode("\n", $sRequest->params->getBody());
if (is_null($body) || is_null($cmd) || !empty($repeat)) {
throw new Exception('Protocol format error.');
}
if (is_string($body) && is_null($data = Json::decode($body))) {
throw new Exception('Protocol format error.');
}
$sRequest->params->setPosts($data);
$sRequest->headers->setRequestUri('rpc/p' . $rpc . '/' . ltrim($cmd, '/'));
$sRequest->headers->setRequestMethod(Request::HTTP_CMD);
return Context::setContext('request', $sRequest);
}
/**
* @param $fd
* @param int $re
* @return mixed
* @throws Exception
*/
private static function getClientInfo($fd, int $re = 0): mixed
{
$server = Snowflake::app()->getSwoole();
if (!is_array($fd)) {
return $server->getClientInfo($fd, $re);
}
return $fd;
}
}
+28 -29
View File
@@ -45,7 +45,7 @@ class Service extends Component
/**
* @throws \Exception
* @throws Exception
*/
public function init()
{
@@ -101,11 +101,12 @@ class Service extends Component
}
/**
* @param \Swoole\Server $server
* @param int $fd
* @param int $reactorId
*/
/**
* @param Server $server
* @param int $fd
* @param int $reactorId
* @throws Exception
*/
private function onConnect(Server $server, int $fd, int $reactorId)
{
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
@@ -118,11 +119,12 @@ class Service extends Component
}
/**
* @param \Swoole\Server $server
* @param int $fd
* on tcp client close
*/
/**
* @param Server $server
* @param int $fd
* on tcp client close
* @throws Exception
*/
private function onClose(Server $server, int $fd)
{
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
@@ -132,11 +134,11 @@ class Service extends Component
/**
* @param \Swoole\Server $server
* @param Server $server
* @param int $fd
* @param int $reID
* @param string $data
* @throws \Exception
* @throws Exception
*/
private function onReceive(Server $server, int $fd, int $reID, string $data)
{
@@ -156,13 +158,12 @@ class Service extends Component
}
/**
* @param \Swoole\Server $server
* @param int $fd
* @param int $reID
* @param string $data
* @throws \Exception
*/
/**
* @param Server $server
* @param string $data
* @param array $client
* @throws Exception
*/
private function onPacket(Server $server, string $data, array $client)
{
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
@@ -179,15 +180,13 @@ class Service extends Component
}
/**
* @param \Swoole\Server $server
* @param int $fd
* @param int $reID
* @param string $data
* @return mixed
* @throws \Exception
*/
private function requestSpl(int $server_port, string $data)
/**
* @param int $server_port
* @param string $data
* @return mixed
* @throws Exception
*/
private function requestSpl(int $server_port, string $data): mixed
{
$sRequest = new Request();