This commit is contained in:
2021-07-27 14:10:54 +08:00
parent 9e36ea7983
commit b0362ea68f
+101 -102
View File
@@ -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);
}
}