This commit is contained in:
2020-12-15 14:04:02 +08:00
parent c2d9250be9
commit a37df0ce7a
16 changed files with 225 additions and 162 deletions
+16 -7
View File
@@ -4,11 +4,13 @@ declare(strict_types=1);
namespace HttpServer\Events;
use Annotation\Route\Socket;
use HttpServer\Abstracts\Callback;
use HttpServer\Route\Annotation\Http;
use HttpServer\Route\Annotation\Tcp;
use HttpServer\Route\Annotation\Websocket;
use HttpServer\Route\Annotation\Websocket as AWebsocket;
use HttpServer\Route\Node;
use Snowflake\Event;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
@@ -47,14 +49,17 @@ class OnClose extends Callback
* @throws ComponentException
* @throws Exception
*/
private function execute(Server $server, int $fd)
private function execute(Server $server, int $fd): void
{
try {
[$manager, $name] = $this->resolve($server, $fd);
if (empty($manager) || !$manager->has($name)) {
$router = Snowflake::app()->getRouter();
if (!($server instanceof WServer) || !$server->isEstablished($fd)) {
return;
}
$manager->runWith($name, [$fd]);
$node = $router->search(Socket::CLOSE . '::event', 'sw:socket');
if ($node instanceof Node) {
$node->dispatch();
}
} catch (\Throwable $exception) {
$this->addError($exception);
} finally {
@@ -70,14 +75,18 @@ class OnClose extends Callback
* @return array|null
* @throws Exception
*/
public function resolve($server, $fd)
public function resolve($server, $fd): ?array
{
if ($server instanceof WServer) {
if (!$server->isEstablished($fd)) {
return [null, null];
}
$manager = Snowflake::app()->annotation->websocket;
$name = $manager->getName(AWebsocket::CLOSE);
$router = Snowflake::app()->getRouter();
$node = $router->search(Socket::HANDSHAKE . '::' . null, 'sw:socket');
if ($node === null) {
return [null, null];
}
return $node->dispatch();
} else if ($server instanceof HServer) {
$manager = Snowflake::app()->annotation->http;
$name = $manager->getName(Http::CLOSE);
+9 -7
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace HttpServer\Events;
use Annotation\Route\Socket;
use Exception;
use HttpServer\Abstracts\Callback;
use HttpServer\Route\Annotation\Websocket as AWebsocket;
@@ -87,20 +88,21 @@ class OnHandshake extends Callback
/**
* @param SRequest $request
* @param SResponse $response
* @return mixed|bool|null
* @return mixed
* @throws ComponentException
* @throws Exception
*/
private function execute(SRequest $request, SResponse $response)
private function execute(SRequest $request, SResponse $response): mixed
{
try {
$this->resolveParse($request, $response);
$manager = Snowflake::app()->annotation->websocket;
$name = $manager->getName(AWebsocket::HANDSHAKE);
if (!$manager->has($name)) {
return $this->disconnect($response);
$router = Snowflake::app()->getRouter();
$node = $router->search(Socket::HANDSHAKE . '::event', 'sw:socket');
if ($node === null) {
return $this->disconnect($response, 502);
}
return $manager->runWith($name, [$request, $response]);
return $node->dispatch();
} catch (\Throwable $exception) {
$this->addError($exception->getMessage());
return $this->disconnect($response, 500);
+18 -23
View File
@@ -4,8 +4,10 @@ declare(strict_types=1);
namespace HttpServer\Events;
use Annotation\Route\Socket;
use Exception;
use HttpServer\Abstracts\Callback;
use HttpServer\Http\Request;
use HttpServer\Route\Annotation\Websocket as AWebsocket;
use Snowflake\Event;
use Snowflake\Snowflake;
@@ -28,16 +30,14 @@ class OnMessage extends Callback
public function onHandler(Server $server, Frame $frame)
{
try {
if ($frame->opcode == 0x08) {
return;
if ($frame->opcode != 0x08) {
$event = Snowflake::app()->getEvent();
Coroutine::defer(function () use ($event) {
$event->trigger(Event::EVENT_AFTER_REQUEST);
});
$content = $this->resolve($event, $frame, $server);
$server->send($frame->fd, $content);
}
$event = Snowflake::app()->getEvent();
Coroutine::defer(function () use ($event) {
$event->trigger(Event::EVENT_AFTER_REQUEST);
});
$this->resolve($event, $frame, $server);
$manager = Snowflake::app()->getAnnotation()->websocket;
$manager->runWith($this->name($manager, $frame), [$frame, $server]);
} catch (\Throwable $exception) {
$this->addError($exception->getMessage(), 'websocket');
$server->send($frame->fd, $exception->getMessage());
@@ -50,30 +50,25 @@ class OnMessage extends Callback
* @param $event
* @param $frame
* @param $server
* @return mixed
* @throws Exception
*/
private function resolve($event, $frame, $server)
private function resolve($event, $frame, $server): mixed
{
if ($event->exists(Event::SERVER_MESSAGE)) {
$event->trigger(Event::SERVER_MESSAGE, [$server, $frame]);
} else {
$frame->data = json_decode($frame->data, true);
}
if (!isset($frame->data['route'])) {
if (empty($route = $frame->data['route'] ?? null)) {
throw new Exception('Format error.');
}
$router = Snowflake::app()->getRouter();
$node = $router->search(Socket::MESSAGE . '::' . $route, 'sw:socket');
if ($node === null) {
throw new Exception('Page not found.');
}
return $node->dispatch();
}
/**
* @param $manager
* @param $frame
* @return mixed
*/
private function name($manager, $frame)
{
return $manager->getName(AWebsocket::MESSAGE, [null, null, $frame->data['route']]);
}
}
+4 -3
View File
@@ -29,10 +29,11 @@ class OnWorkerStart extends Callback
* @param Server $server
* @param int $worker_id
*
* @return mixed|void
* @return mixed
* @throws ConfigException
* @throws Exception
*/
public function onHandler(Server $server, int $worker_id)
public function onHandler(Server $server, int $worker_id): void
{
$get_name = $this->get_process_name($server, $worker_id);
if (!empty($get_name) && !Snowflake::isMac()) {
@@ -66,7 +67,7 @@ class OnWorkerStart extends Callback
* @return string
* @throws ConfigException
*/
private function get_process_name($socket, $worker_id)
private function get_process_name($socket, $worker_id): string
{
$prefix = rtrim(Config::get('id',false, 'system:'), ':');
if ($worker_id >= $socket->setting['worker_num']) {
+26 -9
View File
@@ -76,10 +76,10 @@ class Router extends Application implements RouterInterface
* @param $path
* @param $handler
* @param string $method
* @return mixed|Node|null
* @throws
* @return ?Node
* @throws ConfigException
*/
public function addRoute($path, $handler, $method = 'any')
public function addRoute($path, $handler, $method = 'any'): ?Node
{
$method = strtolower($method);
if (!isset($this->nodes[$method])) {
@@ -99,9 +99,9 @@ class Router extends Application implements RouterInterface
* @param $path
* @param $handler
* @param string $method
* @return mixed
* @return ?Node
*/
private function hash($path, $handler, $method = 'any')
private function hash($path, $handler, $method = 'any'): ?Node
{
$path = $this->resolve($path);
@@ -278,7 +278,7 @@ class Router extends Application implements RouterInterface
* @return Node
* @throws
*/
public function NodeInstance($value, $index = 0, $method = 'get')
public function NodeInstance($value, $index = 0, $method = 'get'): Node
{
$node = new Node();
$node->childes = [];
@@ -301,7 +301,7 @@ class Router extends Application implements RouterInterface
* @param $method
* @return array
*/
private function loadNamespace($method)
private function loadNamespace($method): array
{
$name = array_column($this->groupTacks, 'namespace');
if ($method == 'package' || $method == 'receive') {
@@ -333,7 +333,7 @@ class Router extends Application implements RouterInterface
/**
* @return string
*/
public function addPrefix()
public function addPrefix(): string
{
$prefix = array_column($this->groupTacks, 'prefix');
@@ -352,7 +352,7 @@ class Router extends Application implements RouterInterface
* @return Node|null
* 查找指定路由
*/
public function tree_search(?array $explode, $method)
public function tree_search(?array $explode, $method): ?Node
{
if (empty($explode)) {
return $this->nodes[$method]['/'] ?? null;
@@ -523,7 +523,24 @@ class Router extends Application implements RouterInterface
return null;
}
return $methods['/'];
}
/**
* @param $uri
* @param $method
* @return Node|null
*/
public function search($uri, $method): Node|null
{
if (!isset($this->nodes[$method])) {
return null;
}
$methods = $this->nodes[$method];
if (isset($methods[$uri])) {
return $methods[$uri];
}
return $methods['/'] ?? null;
}
+49 -22
View File
@@ -3,6 +3,7 @@
namespace HttpServer;
use Annotation\IAnnotation;
use HttpServer\Events\OnClose;
use HttpServer\Events\OnConnect;
use HttpServer\Events\OnPacket;
@@ -57,7 +58,7 @@ class Server extends Application
/** @var Http|Websocket|Packet|Receive */
private $baseServer;
private Packet|Websocket|Receive|Http $baseServer;
public int $daemon = 0;
@@ -83,7 +84,7 @@ class Server extends Application
* @return Http|Packet|Receive|Websocket
* @throws Exception
*/
public function initCore(array $configs)
public function initCore(array $configs): Packet|Websocket|Receive|Http
{
$this->enableCoroutine((bool)Config::get('settings.enable_coroutine'));
@@ -95,10 +96,10 @@ class Server extends Application
/**
* @param $configs
* @return null
* @return Packet|Websocket|Receive|Http|null
* @throws Exception
*/
private function orders($configs)
private function orders($configs): Packet|Websocket|Receive|Http|null
{
$servers = $this->sortServers($configs);
foreach ($servers as $server) {
@@ -133,14 +134,16 @@ class Server extends Application
/**
* @param $host
* @param $Port
* @return Http|Packet|Receive|Websocket
* @throws Exception
*/
public function error_stop($host, $Port)
public function error_stop($host, $Port): Packet|Websocket|Receive|Http
{
$this->error(sprintf('Port %s::%d is already.', $host, $Port));
if ($this->baseServer) {
$this->baseServer->shutdown();
}
return $this->baseServer;
}
@@ -148,7 +151,7 @@ class Server extends Application
* @return bool
* @throws ConfigException
*/
public function isRunner()
public function isRunner(): bool
{
$port = $this->sortServers(Config::get('servers'));
if (empty($port)) {
@@ -200,16 +203,17 @@ class Server extends Application
* @throws ConfigException
* @throws Exception
*/
public function onProcessListener()
public function onProcessListener(): void
{
if (!($this->baseServer instanceof \Swoole\Server)) {
return false;
return;
}
$processes = Config::get('processes');
if (!empty($processes) && is_array($processes)) {
return $this->deliveryProcess(merge($processes, $this->process));
$this->deliveryProcess(merge($processes, $this->process));
} else {
$this->deliveryProcess($this->process);
}
return $this->deliveryProcess($this->process);
}
@@ -243,7 +247,7 @@ class Server extends Application
* @param $daemon
* @return Server
*/
public function setDaemon($daemon)
public function setDaemon($daemon): static
{
if (!in_array($daemon, [0, 1])) {
return $this;
@@ -256,7 +260,7 @@ class Server extends Application
/**
* @return Http|Websocket|Packet|Receive
*/
public function getServer()
public function getServer(): Packet|Websocket|Receive|Http
{
return $this->baseServer;
}
@@ -267,7 +271,7 @@ class Server extends Application
* @return mixed
* @throws Exception
*/
private function create($config)
private function create($config): mixed
{
$settings = Config::get('settings', false, []);
if (!isset($this->server[$config['type']])) {
@@ -299,10 +303,10 @@ class Server extends Application
/**
* @param $config
* @param $settings
* @return mixed
* @return \Swoole\Server|Packet|Receive|Http|Websocket
* @throws Exception
*/
private function dispatchCreate($config, $settings)
private function dispatchCreate($config, $settings): \Swoole\Server|Packet|Receive|Http|Websocket
{
if (!($this->baseServer instanceof \Swoole\Server)) {
$this->parseServer($config, $settings);
@@ -323,10 +327,10 @@ class Server extends Application
/**
* @param $config
* @param $settings
* @return void
* @return Packet|Websocket|Receive|Http
* @throws Exception
*/
private function parseServer($config, $settings)
private function parseServer($config, $settings): Packet|Websocket|Receive|Http
{
$class = $this->dispatch($config['type']);
if ($this->isUse($config['port'])) {
@@ -342,7 +346,7 @@ class Server extends Application
} else if ($this->baseServer instanceof Http) {
$this->onLoadHttpHandler();
}
$this->baseServer->set($settings);
return $this->baseServer->set($settings);
}
@@ -427,6 +431,19 @@ class Server extends Application
$event->on(Event::SERVER_WORKER_START, function () {
$router = Snowflake::app()->getRouter();
$router->loadRouterSetting();
$attributes = Snowflake::app()->getAttributes();
$attributes->readControllers(CONTROLLER_PATH, 'controllers');
$aliases = $attributes->getAlias('controllers');
foreach ($aliases as $alias) {
$handler = $alias['handler'];
foreach ($alias['attributes'] as $key => $attribute) {
if ($attribute instanceof IAnnotation) {
$attribute->setHandler($handler);
}
}
}
});
}
@@ -438,8 +455,18 @@ class Server extends Application
{
$event = Snowflake::app()->getEvent();
$event->on(Event::SERVER_WORKER_START, function () {
$websocket = Snowflake::app()->annotation->websocket;
$websocket->registration_notes(APP_PATH . 'app/Websocket', 'App\\Websocket');
$attributes = Snowflake::app()->getAttributes();
$attributes->readControllers(SOCKET_PATH, 'sockets');
$aliases = $attributes->getAlias('sockets');
foreach ($aliases as $alias) {
$handler = $alias['handler'];
foreach ($alias['attributes'] as $key => $attribute) {
if ($attribute instanceof IAnnotation) {
$attribute->setHandler($handler);
}
}
}
});
}
@@ -448,7 +475,7 @@ class Server extends Application
* @param $type
* @return string
*/
private function dispatch($type)
private function dispatch($type): string
{
$default = [
self::HTTP => Http::class,
@@ -463,7 +490,7 @@ class Server extends Application
* @param $servers
* @return array
*/
private function sortServers($servers)
private function sortServers($servers): array
{
$array = [];
foreach ($servers as $server) {