From b0362ea68f0262e88e23a10ee65a23b5ded2ec4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 27 Jul 2021 14:10:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/HTTPServerListener.php | 203 +++++++++++++++++----------------- 1 file changed, 101 insertions(+), 102 deletions(-) diff --git a/Server/HTTPServerListener.php b/Server/HTTPServerListener.php index fde4bf92..dd284e16 100644 --- a/Server/HTTPServerListener.php +++ b/Server/HTTPServerListener.php @@ -3,7 +3,6 @@ namespace Server; use Exception; -use HttpServer\Exception\ExitException; use HttpServer\Http\Request as HRequest; use HttpServer\Http\Response as HResponse; use HttpServer\Route\Router; @@ -26,124 +25,124 @@ use Throwable; class HTTPServerListener extends Abstracts\Server { - protected static bool|Port $_http; + protected static bool|Port $_http; - use ListenerHelper; + use ListenerHelper; - private Router $router; + private Router $router; - /** - * HTTPServerListener constructor. - * @throws Exception - */ - public function __construct() - { - $this->router = Snowflake::getApp('router'); - parent::__construct(); - } + /** + * HTTPServerListener constructor. + * @throws Exception + */ + public function __construct() + { + $this->router = Snowflake::getApp('router'); + parent::__construct(); + } - /** - * UDPServerListener constructor. - * @param Server|\Swoole\WebSocket\Server|\Swoole\Http\Server $server - * @param string $host - * @param int $port - * @param int $mode - * @param array|null $settings - * @return Server\Port - * @throws NotFindClassException - * @throws ReflectionException - * @throws Exception - */ - public static function instance(mixed $server, string $host, int $port, int $mode, ?array $settings = []): Server\Port - { - if (!in_array($mode, [SWOOLE_TCP, SWOOLE_TCP6])) { - trigger_error('Port mode ' . $host . '::' . $port . ' must is udp listener type.'); - } + /** + * UDPServerListener constructor. + * @param Server|\Swoole\WebSocket\Server|\Swoole\Http\Server $server + * @param string $host + * @param int $port + * @param int $mode + * @param array|null $settings + * @return Server\Port + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + public static function instance(mixed $server, string $host, int $port, int $mode, ?array $settings = []): Server\Port + { + if (!in_array($mode, [SWOOLE_TCP, SWOOLE_TCP6])) { + trigger_error('Port mode ' . $host . '::' . $port . ' must is udp listener type.'); + } - /** @var static $reflect */ - $reflect = Snowflake::getDi()->getReflect(static::class)?->newInstance(); - static::$_http = $server->addlistener($host, $port, $mode); - if (!(static::$_http instanceof Port)) { - trigger_error('Port is ' . $host . '::' . $port . ' must is tcp listener type.'); - } + /** @var static $reflect */ + $reflect = Snowflake::getDi()->getReflect(static::class)?->newInstance(); + static::$_http = $server->addlistener($host, $port, $mode); + if (!(static::$_http instanceof Port)) { + trigger_error('Port is ' . $host . '::' . $port . ' must is tcp listener type.'); + } - static::$_http->set($settings['settings'] ?? []); - static::$_http->on('request', [$reflect, 'onRequest']); - static::$_http->on('connect', [$reflect, 'onConnect']); - static::$_http->on('close', [$reflect, 'onClose']); - if (swoole_version() >= '4.7.0') { - static::$_http->on('disconnect', [$reflect, 'onDisconnect']); - $reflect->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT] ?? null); - } - $reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null); - $reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null); - return static::$_http; - } + static::$_http->set($settings['settings'] ?? []); + static::$_http->on('request', [$reflect, 'onRequest']); + static::$_http->on('connect', [$reflect, 'onConnect']); + static::$_http->on('close', [$reflect, 'onClose']); + if (swoole_version() >= '4.7.0') { + static::$_http->on('disconnect', [$reflect, 'onDisconnect']); + $reflect->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT] ?? null); + } + $reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null); + $reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null); + return static::$_http; + } - /** - * @param Server $server - * @param int $fd - * @throws Exception - */ - public function onConnect(Server $server, int $fd) - { - $this->runEvent(Constant::CONNECT, null, [$server, $fd]); + /** + * @param Server $server + * @param int $fd + * @throws Exception + */ + public function onConnect(Server $server, int $fd) + { + $this->runEvent(Constant::CONNECT, null, [$server, $fd]); - $this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES); - } + $this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES); + } - /** - * @param Request $request - * @param Response $response - * @throws Exception - */ - public function onRequest(Request $request, Response $response) - { - [$sRequest, $sResponse] = [HRequest::create($request), HResponse::create($response)]; - try { - if ($sRequest->is('favicon.ico')) { - $this->router->status404(); - } else if (!($node = $this->router->find_path($sRequest))) { - $this->router->status404(); - } else { - $sResponse->send($node->dispatch(), 200); - } - } catch (Error | Throwable $exception) { - $sResponse->addHeader('Content-Type', 'text/html; charset=utf-8'); - $sResponse->send(jTraceEx($exception), - $exception->getCode() == 0 ? 500 : $exception->getCode()); - } finally { - $this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES); - } - } + /** + * @param Request $request + * @param Response $response + * @throws Exception + */ + public function onRequest(Request $request, Response $response) + { + [$sRequest, $sResponse] = [HRequest::create($request), HResponse::create($response)]; + try { + if ($sRequest->is('favicon.ico')) { + $this->router->status404(); + } else if (!($node = $this->router->find_path($sRequest))) { + $this->router->status404(); + } else { + $sResponse->send($node->dispatch(), 200); + } + } catch (Error | Throwable $exception) { + $sResponse->addHeader('Content-Type', 'text/html; charset=utf-8'); + $sResponse->send(jTraceEx($exception, null, true), + $exception->getCode() == 0 ? 500 : $exception->getCode()); + } finally { + $this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES); + } + } - /** - * @param Server $server - * @param int $fd - * @throws Exception - */ - public function onDisconnect(Server $server, int $fd) - { - $this->runEvent(Constant::DISCONNECT, null, [$server, $fd]); + /** + * @param Server $server + * @param int $fd + * @throws Exception + */ + public function onDisconnect(Server $server, int $fd) + { + $this->runEvent(Constant::DISCONNECT, null, [$server, $fd]); - $this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES); - } + $this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES); + } - /** - * @param Server $server - * @param int $fd - * @throws Exception - */ - public function onClose(Server $server, int $fd) - { - $this->runEvent(Constant::CLOSE, null, [$server, $fd]); + /** + * @param Server $server + * @param int $fd + * @throws Exception + */ + public function onClose(Server $server, int $fd) + { + $this->runEvent(Constant::CLOSE, null, [$server, $fd]); - $this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES); - } + $this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES); + } }