改名
This commit is contained in:
+101
-102
@@ -3,7 +3,6 @@
|
|||||||
namespace Server;
|
namespace Server;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use HttpServer\Exception\ExitException;
|
|
||||||
use HttpServer\Http\Request as HRequest;
|
use HttpServer\Http\Request as HRequest;
|
||||||
use HttpServer\Http\Response as HResponse;
|
use HttpServer\Http\Response as HResponse;
|
||||||
use HttpServer\Route\Router;
|
use HttpServer\Route\Router;
|
||||||
@@ -26,124 +25,124 @@ use Throwable;
|
|||||||
class HTTPServerListener extends Abstracts\Server
|
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.
|
* HTTPServerListener constructor.
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->router = Snowflake::getApp('router');
|
$this->router = Snowflake::getApp('router');
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UDPServerListener constructor.
|
* UDPServerListener constructor.
|
||||||
* @param Server|\Swoole\WebSocket\Server|\Swoole\Http\Server $server
|
* @param Server|\Swoole\WebSocket\Server|\Swoole\Http\Server $server
|
||||||
* @param string $host
|
* @param string $host
|
||||||
* @param int $port
|
* @param int $port
|
||||||
* @param int $mode
|
* @param int $mode
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return Server\Port
|
* @return Server\Port
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function instance(mixed $server, string $host, int $port, int $mode, ?array $settings = []): Server\Port
|
public static function instance(mixed $server, string $host, int $port, int $mode, ?array $settings = []): Server\Port
|
||||||
{
|
{
|
||||||
if (!in_array($mode, [SWOOLE_TCP, SWOOLE_TCP6])) {
|
if (!in_array($mode, [SWOOLE_TCP, SWOOLE_TCP6])) {
|
||||||
trigger_error('Port mode ' . $host . '::' . $port . ' must is udp listener type.');
|
trigger_error('Port mode ' . $host . '::' . $port . ' must is udp listener type.');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var static $reflect */
|
/** @var static $reflect */
|
||||||
$reflect = Snowflake::getDi()->getReflect(static::class)?->newInstance();
|
$reflect = Snowflake::getDi()->getReflect(static::class)?->newInstance();
|
||||||
static::$_http = $server->addlistener($host, $port, $mode);
|
static::$_http = $server->addlistener($host, $port, $mode);
|
||||||
if (!(static::$_http instanceof Port)) {
|
if (!(static::$_http instanceof Port)) {
|
||||||
trigger_error('Port is ' . $host . '::' . $port . ' must is tcp listener type.');
|
trigger_error('Port is ' . $host . '::' . $port . ' must is tcp listener type.');
|
||||||
}
|
}
|
||||||
|
|
||||||
static::$_http->set($settings['settings'] ?? []);
|
static::$_http->set($settings['settings'] ?? []);
|
||||||
static::$_http->on('request', [$reflect, 'onRequest']);
|
static::$_http->on('request', [$reflect, 'onRequest']);
|
||||||
static::$_http->on('connect', [$reflect, 'onConnect']);
|
static::$_http->on('connect', [$reflect, 'onConnect']);
|
||||||
static::$_http->on('close', [$reflect, 'onClose']);
|
static::$_http->on('close', [$reflect, 'onClose']);
|
||||||
if (swoole_version() >= '4.7.0') {
|
if (swoole_version() >= '4.7.0') {
|
||||||
static::$_http->on('disconnect', [$reflect, 'onDisconnect']);
|
static::$_http->on('disconnect', [$reflect, 'onDisconnect']);
|
||||||
$reflect->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT] ?? null);
|
$reflect->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT] ?? null);
|
||||||
}
|
}
|
||||||
$reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
|
$reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
|
||||||
$reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
|
$reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
|
||||||
return static::$_http;
|
return static::$_http;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $fd
|
* @param int $fd
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onConnect(Server $server, int $fd)
|
public function onConnect(Server $server, int $fd)
|
||||||
{
|
{
|
||||||
$this->runEvent(Constant::CONNECT, null, [$server, $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 Request $request
|
||||||
* @param Response $response
|
* @param Response $response
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onRequest(Request $request, Response $response)
|
public function onRequest(Request $request, Response $response)
|
||||||
{
|
{
|
||||||
[$sRequest, $sResponse] = [HRequest::create($request), HResponse::create($response)];
|
[$sRequest, $sResponse] = [HRequest::create($request), HResponse::create($response)];
|
||||||
try {
|
try {
|
||||||
if ($sRequest->is('favicon.ico')) {
|
if ($sRequest->is('favicon.ico')) {
|
||||||
$this->router->status404();
|
$this->router->status404();
|
||||||
} else if (!($node = $this->router->find_path($sRequest))) {
|
} else if (!($node = $this->router->find_path($sRequest))) {
|
||||||
$this->router->status404();
|
$this->router->status404();
|
||||||
} else {
|
} else {
|
||||||
$sResponse->send($node->dispatch(), 200);
|
$sResponse->send($node->dispatch(), 200);
|
||||||
}
|
}
|
||||||
} catch (Error | Throwable $exception) {
|
} catch (Error | Throwable $exception) {
|
||||||
$sResponse->addHeader('Content-Type', 'text/html; charset=utf-8');
|
$sResponse->addHeader('Content-Type', 'text/html; charset=utf-8');
|
||||||
$sResponse->send(jTraceEx($exception),
|
$sResponse->send(jTraceEx($exception, null, true),
|
||||||
$exception->getCode() == 0 ? 500 : $exception->getCode());
|
$exception->getCode() == 0 ? 500 : $exception->getCode());
|
||||||
} finally {
|
} finally {
|
||||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $fd
|
* @param int $fd
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onDisconnect(Server $server, int $fd)
|
public function onDisconnect(Server $server, int $fd)
|
||||||
{
|
{
|
||||||
$this->runEvent(Constant::DISCONNECT, null, [$server, $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 Server $server
|
||||||
* @param int $fd
|
* @param int $fd
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onClose(Server $server, int $fd)
|
public function onClose(Server $server, int $fd)
|
||||||
{
|
{
|
||||||
$this->runEvent(Constant::CLOSE, null, [$server, $fd]);
|
$this->runEvent(Constant::CLOSE, null, [$server, $fd]);
|
||||||
|
|
||||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user