This commit is contained in:
2021-08-03 14:06:47 +08:00
parent 5fc88d3076
commit 2ebb787b74
6 changed files with 69 additions and 138 deletions
+3
View File
@@ -10,6 +10,7 @@ use Snowflake\Abstracts\Config;
use Snowflake\Event;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
use Swoole\Server\Port;
/**
@@ -26,6 +27,8 @@ abstract class Server
protected Event $_event;
abstract public function bindCallback(\Swoole\Server|Port $server, ?array $settings = []);
/**
+9 -20
View File
@@ -46,32 +46,21 @@ class HTTPServerListener extends Abstracts\Server
/**
* UDPServerListener constructor.
* @param Server|\Swoole\WebSocket\Server|\Swoole\Http\Server $server
* @param string $host
* @param int $port
* @param int $mode
* @param Server|Port $server
* @param array|null $settings
* @return Server\Port
* @throws ReflectionException
* @throws Exception
*/
public static function instance(mixed $server, string $host, int $port, int $mode, ?array $settings = []): Server\Port
public function bindCallback(Server|Port $server, ?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();
$reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
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(array_merge($settings['settings'] ?? [], ['enable_unsafe_event' => false]));
static::$_http->on('request', [$reflect, 'onRequest']);
static::$_http->on('connect', [$reflect, 'onConnect']);
static::$_http->on('close', [$reflect, 'onClose']);
return static::$_http;
$this->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
$server->set(array_merge($settings['settings'] ?? [], ['enable_unsafe_event' => false]));
$server->on('request', [$this, 'onRequest']);
$server->on('connect', [$this, 'onConnect']);
$server->on('close', [$this, 'onClose']);
return $server;
}
+21 -48
View File
@@ -226,25 +226,22 @@ class ServerManager extends Abstracts\Server
* @param int $mode
* @param array $settings
* @throws ReflectionException
* @throws NotFindClassException
* @throws Exception
*/
private function addNewListener(string $type, string $host, int $port, int $mode, array $settings = [])
{
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m $type service %s::%d start.", $host, $port) . PHP_EOL;
switch ($type) {
case Constant::SERVER_TYPE_TCP:
$this->ports[$port] = TCPServerListener::instance($this->server, $host, $port, $mode, $settings);
break;
case Constant::SERVER_TYPE_UDP:
$this->ports[$port] = UDPServerListener::instance($this->server, $host, $port, $mode, $settings);
break;
case Constant::SERVER_TYPE_HTTP:
$this->ports[$port] = HTTPServerListener::instance($this->server, $host, $port, $mode, $settings);
break;
case Constant::SERVER_TYPE_WEBSOCKET:
$this->ports[$port] = WebSocketServerListener::instance($this->server, $host, $port, $mode, $settings);
break;
}
/** @var Server\Port $service */
$this->ports[$port] = $this->server->addlistener($host, $port, $mode);
$this->ports[$port]->set($settings['settings'] ?? []);
$reflect = match ($type) {
Constant::SERVER_TYPE_TCP => Snowflake::getDi()->getReflect(TCPServerListener::class),
Constant::SERVER_TYPE_UDP => Snowflake::getDi()->getReflect(UDPServerListener::class),
Constant::SERVER_TYPE_HTTP => Snowflake::getDi()->getReflect(HTTPServerListener::class),
Constant::SERVER_TYPE_WEBSOCKET => Snowflake::getDi()->getReflect(WebSocketServerListener::class),
default => throw new Exception(''),
};
$reflect->newInstance()->bindCallback($this->ports[$port], $settings['events'] ?? []);
}
@@ -326,6 +323,7 @@ class ServerManager extends Abstracts\Server
* @param array $settings
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
private function addDefaultListener(string $type, array $settings): void
{
@@ -334,46 +332,21 @@ class ServerManager extends Abstracts\Server
$this->addTaskListener($settings['events']);
}
if ($type === Constant::SERVER_TYPE_WEBSOCKET) {
/** @var WebSocketServerListener $reflect */
$reflect = $this->getNewInstance(WebSocketServerListener::class);
$this->server->on('handshake', [$reflect, 'onHandshake']);
$this->server->on('message', [$reflect, 'onMessage']);
$this->server->on('connect', [$reflect, 'onConnect']);
if (isset($settings['events'][Constant::REQUEST])) {
$request = $this->getNewInstance(HTTPServerListener::class);
if (is_array($settings['events'][Constant::REQUEST])) {
$settings['events'][Constant::REQUEST][0] = $this->getNewInstance($settings['events'][Constant::REQUEST][0]);
}
$this->server->on('request', $settings['events'][Constant::REQUEST] ?? [$request, 'onRequest']);
}
$reflect->setEvents(Constant::HANDSHAKE, $settings['events'][Constant::HANDSHAKE] ?? null);
$reflect->setEvents(Constant::MESSAGE, $settings['events'][Constant::MESSAGE] ?? null);
$reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
$reflect->bindCallback($this->server, $settings);
} else if ($type === Constant::SERVER_TYPE_UDP) {
/** @var UDPServerListener $reflect */
$reflect = $this->getNewInstance(UDPServerListener::class);
$this->server->on('packet', [$reflect, 'onPacket']);
$reflect->setEvents(Constant::PACKET, $settings['events'][Constant::PACKET] ?? null);
$reflect->bindCallback($this->server, $settings);
} else if ($type === Constant::SERVER_TYPE_HTTP) {
/** @var HTTPServerListener $reflect */
$reflect = $this->getNewInstance(HTTPServerListener::class);
$this->server->on('request', [$reflect, 'onRequest']);
$this->server->on('connect', [$reflect, 'onConnect']);
$this->server->on('close', [$reflect, 'onClose']);
$reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
$reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
$reflect->bindCallback($this->server, $settings);
} else {
/** @var TCPServerListener $reflect */
$reflect = $this->getNewInstance(TCPServerListener::class);
$this->server->on('connect', [$reflect, 'onConnect']);
$this->server->on('close', [$reflect, 'onClose']);
$this->server->on('receive', [$reflect, 'onReceive']);
$reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
$reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
$reflect->setEvents(Constant::RECEIVE, $settings['events'][Constant::RECEIVE] ?? null);
}
if ($this->server instanceof WServer && swoole_version() >= '4.7') {
$reflect->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT] ?? null);
$this->server->on('disconnect', [$reflect, 'onDisconnect']);
$reflect->bindCallback($this->server, $settings);
}
}
+10 -27
View File
@@ -5,8 +5,6 @@ namespace Server;
use Exception;
use ReflectionException;
use Snowflake\Event;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Swoole\Server;
use Swoole\Server\Port;
@@ -25,37 +23,22 @@ class TCPServerListener extends Abstracts\Server
/**
* UDPServerListener constructor.
* @param Server $server
* @param string $host
* @param int $port
* @param int $mode
* @param Server|Port $server
* @param array|null $settings
* @return Port
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public static function instance(Server $server, string $host, int $port, int $mode, ?array $settings = []): Port
public function bindCallback(Server|Port $server, ?array $settings = []): Port
{
if (!in_array($mode, [SWOOLE_TCP, SWOOLE_TCP6])) {
trigger_error('Port mode ' . $host . '::' . $port . ' must is tcp listener type.');
}
$this->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
$this->setEvents(Constant::RECEIVE, $settings['events'][Constant::RECEIVE] ?? null);
$this->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
/** @var static $reflect */
$reflect = Snowflake::getDi()->getReflect(static::class)?->newInstance();
$reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
$reflect->setEvents(Constant::RECEIVE, $settings['events'][Constant::RECEIVE] ?? null);
$reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
static::$_tcp = $server->addlistener($host, $port, $mode);
if (!(static::$_tcp instanceof Port)) {
trigger_error('Port is ' . $host . '::' . $port . ' must is tcp listener type.');
}
static::$_tcp->set($settings['settings'] ?? []);
static::$_tcp->on('receive', [$reflect, 'onReceive']);
static::$_tcp->on('connect', [$reflect, 'onConnect']);
static::$_tcp->on('close', [$reflect, 'onClose']);
return static::$_tcp;
$server->set($settings['settings'] ?? []);
$server->on('receive', [$this, 'onReceive']);
$server->on('connect', [$this, 'onConnect']);
$server->on('close', [$this, 'onClose']);
return $server;
}
+5 -17
View File
@@ -6,7 +6,6 @@ use Exception;
use ReflectionException;
use Snowflake\Event;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Swoole\Server;
use Swoole\Server\Port;
@@ -35,25 +34,14 @@ class UDPServerListener extends Abstracts\Server
* @throws ReflectionException
* @throws Exception
*/
public static function instance(Server $server, string $host, int $port, int $mode, ?array $settings = []): Server\Port
public function bindCallback(Server|Port $server, ?array $settings = []): Server\Port
{
if (!in_array($mode, [SWOOLE_UDP, SWOOLE_UDP6])) {
trigger_error('Port mode ' . $host . '::' . $port . ' must is udp listener type.');
}
$this->setEvents(Constant::PACKET, $settings['events'][Constant::PACKET] ?? null);
/** @var static $reflect */
$reflect = Snowflake::getDi()->getReflect(static::class)->newInstance();
$reflect->setEvents(Constant::PACKET, $settings['events'][Constant::PACKET] ?? null);
$server->set($settings['settings'] ?? []);
$server->on('packet', [$this, 'onPacket']);
static::$_udp = $server->addlistener($host, $port, $mode);
if (!(static::$_udp instanceof Port)) {
trigger_error('Port is ' . $host . '::' . $port . ' must is tcp listener type.');
}
static::$_udp->set($settings['settings'] ?? []);
static::$_udp->on('packet', [$reflect, 'onPacket']);
return static::$_udp;
return $server;
}
+21 -26
View File
@@ -27,41 +27,36 @@ class WebSocketServerListener extends Abstracts\Server
/**
* @param mixed $server
* @param string $host
* @param int $port
* @param int $mode
* @param array|null $settings
* @return Server\Port
* @throws ReflectionException
* @throws NotFindClassException
* @throws Exception
*/
public static function instance(mixed $server, string $host, int $port, int $mode, ?array $settings = []): Server\Port
public function bindCallback(Server|Port $server, ?array $settings = []): Server\Port
{
if (!in_array($mode, [SWOOLE_TCP, SWOOLE_TCP6])) {
trigger_error('Port mode ' . $host . '::' . $port . ' must is udp listener type.');
$this->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
$this->setEvents(Constant::HANDSHAKE, $settings['events'][Constant::HANDSHAKE] ?? null);
$this->setEvents(Constant::MESSAGE, $settings['events'][Constant::MESSAGE] ?? null);
$this->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
$server->set($settings['settings'] ?? []);
$server->on('connect', [$this, 'onConnect']);
$server->on('handshake', [$this, 'onHandshake']);
$server->on('message', [$this, 'onMessage']);
$server->on('close', [$this, 'onClose']);
if (swoole_version() >= '4.7' && isset($settings['events'][Constant::DISCONNECT])) {
$this->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT]);
$server->on('disconnect', [$this, 'onDisconnect']);
}
/** @var static $reflect */
$reflect = Snowflake::getDi()->getReflect(static::class)?->newInstance();
$reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
$reflect->setEvents(Constant::HANDSHAKE, $settings['events'][Constant::HANDSHAKE] ?? null);
$reflect->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT] ?? null);
$reflect->setEvents(Constant::MESSAGE, $settings['events'][Constant::MESSAGE] ?? null);
$reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
static::$_http = $server->addlistener($host, $port, $mode);
if (!(static::$_http instanceof Port)) {
trigger_error('Port is ' . $host . '::' . $port . ' must is tcp listener type.');
$events = $settings['events'][Constant::REQUEST] ?? [];
if (!empty($events) && is_array($events)) {
$events[0] = Snowflake::getDi()->get($events[0]);
$server->on('request', $events);
}
static::$_http->set($settings['settings'] ?? []);
static::$_http->on('connect', [$reflect, 'onConnect']);
static::$_http->on('handshake', [$reflect, 'onHandshake']);
static::$_http->on('message', [$reflect, 'onMessage']);
static::$_http->on('disconnect', [$reflect, 'onDisconnect']);
static::$_http->on('close', [$reflect, 'onClose']);
return static::$_http;
return $server;
}