This commit is contained in:
2021-07-21 19:05:11 +08:00
parent 7dd1322ea4
commit de937ace09
5 changed files with 54 additions and 4 deletions
+14
View File
@@ -6,6 +6,7 @@ namespace Server\Abstracts;
use Closure; use Closure;
use Exception; use Exception;
use Snowflake\Event;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -20,6 +21,19 @@ abstract class Server
protected array $_events = []; protected array $_events = [];
protected Event $_event;
/**
* Server constructor.
* @throws Exception
*/
public function __construct()
{
$this->_event = Snowflake::getApp('event');
}
/** /**
* @param string $name * @param string $name
* @param array|null $events * @param array|null $events
+11 -4
View File
@@ -32,9 +32,6 @@ class HTTPServerListener extends Abstracts\Server
private Router $router; private Router $router;
private Event $_event;
/** /**
* HTTPServerListener constructor. * HTTPServerListener constructor.
* @throws Exception * @throws Exception
@@ -42,7 +39,7 @@ class HTTPServerListener extends Abstracts\Server
public function __construct() public function __construct()
{ {
$this->router = Snowflake::getApp('router'); $this->router = Snowflake::getApp('router');
$this->_event = Snowflake::getApp('event'); parent::__construct();
} }
/** /**
@@ -83,10 +80,13 @@ class HTTPServerListener extends Abstracts\Server
/** /**
* @param Server $server * @param Server $server
* @param int $fd * @param int $fd
* @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);
} }
@@ -104,6 +104,7 @@ class HTTPServerListener extends Abstracts\Server
} }
$this->router->dispatch(); $this->router->dispatch();
} catch (ExitException | Error | Throwable $exception) { } catch (ExitException | Error | Throwable $exception) {
$response->setHeader('Content-Type', 'text/html; charset=utf-8');
$response->status($exception->getCode() == 0 ? 500 : $exception->getCode()); $response->status($exception->getCode() == 0 ? 500 : $exception->getCode());
$response->end($exception->getMessage()); $response->end($exception->getMessage());
} finally { } finally {
@@ -115,20 +116,26 @@ class HTTPServerListener extends Abstracts\Server
/** /**
* @param Server $server * @param Server $server
* @param int $fd * @param int $fd
* @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);
} }
/** /**
* @param Server $server * @param Server $server
* @param int $fd * @param int $fd
* @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);
} }
} }
+13
View File
@@ -4,6 +4,7 @@ namespace Server;
use Exception; use Exception;
use ReflectionException; use ReflectionException;
use Snowflake\Event;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Server; use Swoole\Server;
@@ -62,20 +63,26 @@ class TCPServerListener extends Abstracts\Server
* @param Server $server * @param Server $server
* @param int $fd * @param int $fd
* @param int|null $reactor_id * @param int|null $reactor_id
* @throws Exception
*/ */
public function onDisconnect(Server $server, int $fd, ?int $reactor_id = null) public function onDisconnect(Server $server, int $fd, ?int $reactor_id = null)
{ {
$this->runEvent(Constant::HANDSHAKE, null, [$server, $fd, $reactor_id]); $this->runEvent(Constant::HANDSHAKE, null, [$server, $fd, $reactor_id]);
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
} }
/** /**
* @param Server $server * @param Server $server
* @param int $fd * @param int $fd
* @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);
} }
@@ -84,20 +91,26 @@ class TCPServerListener extends Abstracts\Server
* @param int $fd * @param int $fd
* @param int $reactor_id * @param int $reactor_id
* @param string $data * @param string $data
* @throws Exception
*/ */
public function onReceive(Server $server, int $fd, int $reactor_id, string $data) public function onReceive(Server $server, int $fd, int $reactor_id, string $data)
{ {
$this->runEvent(Constant::RECEIVE, null, [$server, $fd, $reactor_id, $data]); $this->runEvent(Constant::RECEIVE, null, [$server, $fd, $reactor_id, $data]);
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
} }
/** /**
* @param Server $server * @param Server $server
* @param int $fd * @param int $fd
* @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);
} }
} }
+4
View File
@@ -4,6 +4,7 @@ namespace Server;
use Exception; use Exception;
use ReflectionException; use ReflectionException;
use Snowflake\Event;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Server; use Swoole\Server;
@@ -56,10 +57,13 @@ class UDPServerListener extends Abstracts\Server
* @param Server $server * @param Server $server
* @param string $data * @param string $data
* @param array $clientInfo * @param array $clientInfo
* @throws Exception
*/ */
public function onPacket(Server $server, string $data, array $clientInfo) public function onPacket(Server $server, string $data, array $clientInfo)
{ {
$this->runEvent(Constant::MESSAGE, null, [$server, $data, $clientInfo]); $this->runEvent(Constant::MESSAGE, null, [$server, $data, $clientInfo]);
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
} }
} }
+12
View File
@@ -4,6 +4,7 @@ namespace Server;
use Exception; use Exception;
use ReflectionException; use ReflectionException;
use Snowflake\Event;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Http\Request; use Swoole\Http\Request;
@@ -86,6 +87,8 @@ class WebSocketServerListener extends Abstracts\Server
$response->setHeader($key, $val); $response->setHeader($key, $val);
} }
$this->runEvent(Constant::HANDSHAKE, fn() => $this->disconnect($request, $response), [$request, $response]); $this->runEvent(Constant::HANDSHAKE, fn() => $this->disconnect($request, $response), [$request, $response]);
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
} }
@@ -103,30 +106,39 @@ class WebSocketServerListener extends Abstracts\Server
/** /**
* @param Server $server * @param Server $server
* @param int $fd * @param int $fd
* @throws Exception
*/ */
public function onConnect(Server $server, int $fd) public function onConnect(Server $server, int $fd)
{ {
$this->runEvent(Constant::CONNECT, fn() => $server->confirm($fd), [$server, $fd]); $this->runEvent(Constant::CONNECT, fn() => $server->confirm($fd), [$server, $fd]);
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
} }
/** /**
* @param \Swoole\WebSocket\Server $server * @param \Swoole\WebSocket\Server $server
* @param Frame $frame * @param Frame $frame
* @throws Exception
*/ */
public function onMessage(\Swoole\WebSocket\Server $server, Frame $frame) public function onMessage(\Swoole\WebSocket\Server $server, Frame $frame)
{ {
$this->runEvent(Constant::MESSAGE, fn() => $server->push($frame->fd, '.'), [$server, $frame]); $this->runEvent(Constant::MESSAGE, fn() => $server->push($frame->fd, '.'), [$server, $frame]);
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
} }
/** /**
* @param Server $server * @param Server $server
* @param int $fd * @param int $fd
* @throws Exception
*/ */
public function onClose(Server $server, int $fd) public function onClose(Server $server, int $fd)
{ {
$this->runEvent(Constant::CLOSE, fn() => $server->confirm($fd), [$server, $fd]); $this->runEvent(Constant::CLOSE, fn() => $server->confirm($fd), [$server, $fd]);
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
} }
} }