This commit is contained in:
2021-07-21 17:55:34 +08:00
parent 503a188dd7
commit ad43351306
3 changed files with 95 additions and 104 deletions
+5 -9
View File
@@ -545,21 +545,17 @@ class Router extends HttpService implements RouterInterface
*/
public function find_path(Request $request): ?Node
{
// return $this->Branch_search($request);
$method = $request->getMethod();
$uri = $request->headers->get('request_uri', '/');
if (!isset(static::$nodes[$method])) {
return null;
}
$methods = static::$nodes[$method];
if (isset($methods[$uri])) {
$methods = static::$nodes[$method][$uri] ?? null;
if (!is_null($methods[$uri])) {
return $methods[$uri];
}
if (!$request->isOption || !isset($methods['/'])) {
return null;
if ($request->isOption) {
return static::$nodes[$method]['*'] ?? null;
}
return $methods['/'];
return null;
}
+7 -4
View File
@@ -92,11 +92,14 @@ class Server extends HttpService
*/
private function rpcListener($rpcService)
{
$rpcService['events'][Constant::CONNECT] = [Service::class, 'onConnect'];
$rpcService['events'][Constant::DISCONNECT] = [Service::class, 'onClose'];
$rpcService['events'][Constant::CLOSE] = [Service::class, 'onClose'];
$rpcService['events'][Constant::RECEIVE] = [Service::class, 'onReceive'];
if (in_array($rpcService['mode'], [SWOOLE_SOCK_UDP, SWOOLE_UDP, SWOOLE_UDP6, SWOOLE_SOCK_UDP6])) {
$rpcService['events'][Constant::PACKET] = [Service::class, 'onPacket'];
} else {
$rpcService['events'][Constant::RECEIVE] = [Service::class, 'onReceive'];
$rpcService['events'][Constant::CONNECT] = [Service::class, 'onConnect'];
$rpcService['events'][Constant::DISCONNECT] = [Service::class, 'onDisconnect'];
$rpcService['events'][Constant::CLOSE] = [Service::class, 'onClose'];
}
$this->manager->addListener($rpcService['type'], $rpcService['host'], $rpcService['port'], $rpcService['mode'], $rpcService);
}
+18 -26
View File
@@ -7,15 +7,9 @@ use Exception;
use HttpServer\Http\Context;
use HttpServer\Http\Request;
use HttpServer\Route\Router;
use HttpServer\Service\Http;
use HttpServer\Service\Packet;
use HttpServer\Service\Receive;
use HttpServer\Service\Websocket;
use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\Config;
use Server\Constant;
use Snowflake\Core\Json;
use Snowflake\Event;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
use Swoole\Server;
use function Swoole\Coroutine\defer;
@@ -25,19 +19,9 @@ use function Swoole\Coroutine\defer;
* Class Service
* @package Rpc
*/
class Service extends Component
class Service extends \Server\Abstracts\Server
{
const defaultConfig = [
'open_tcp_keepalive' => true,
'tcp_keepidle' => 30,
'tcp_keepinterval' => 10,
'tcp_keepcount' => 10,
'open_http_protocol' => false,
'open_websocket_protocol' => false,
];
const RPC_CONNECT = 'RPC::CONNECT';
const RPC_CLOSE = 'RPC::CLOSE';
@@ -63,11 +47,7 @@ class Service extends Component
{
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
$config = $server->setting['enable_delay_receive'] ?? null;
if ($config === true) {
$server->confirm($fd);
}
Event::trigger(Service::RPC_CONNECT, [$server, $fd, $reactorId]);
$this->runEvent(Constant::CONNECT, null, [$server, $fd, $reactorId]);
}
@@ -81,7 +61,21 @@ class Service extends Component
{
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
Event::trigger(Service::RPC_CLOSE, [$server, $fd]);
$this->runEvent(Constant::CLOSE, null, [$server, $fd]);
}
/**
* @param Server $server
* @param int $fd
* on tcp client close
* @throws Exception
*/
public function onDisconnect(Server $server, int $fd)
{
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
$this->runEvent(Constant::DISCONNECT, null, [$server, $fd]);
}
@@ -104,7 +98,6 @@ class Service extends Component
$server->send($fd, $result);
} catch (\Throwable $exception) {
$this->addError($exception, 'rpc-service');
$server->send($fd, $exception->getMessage());
}
}
@@ -126,7 +119,6 @@ class Service extends Component
$server->sendto($client['address'], $client['port'], $result);
} catch (\Throwable $exception) {
$this->addError($exception, 'rpc-service');
$server->sendto($client['address'], $client['port'], $exception->getMessage());
}
}