Skip to content

Commit

Permalink
新增支持 WebSocket、TCP、UDP 的路由未找到处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Nov 8, 2019
1 parent a144ccc commit cf8fe40
Show file tree
Hide file tree
Showing 21 changed files with 419 additions and 15 deletions.
21 changes: 21 additions & 0 deletions src/Server/TcpServer/Error/ITcpRouteNotFoundHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Imi\Server\TcpServer\Error;

use Imi\Server\TcpServer\IReceiveHandler;
use Imi\Server\TcpServer\Message\IReceiveData;

/**
* 处理未找到 TCP 路由情况的接口
*/
interface ITcpRouteNotFoundHandler
{
/**
* 处理方法
*
* @param \Imi\Server\TcpServer\Message\IReceiveData $data
* @param \Imi\Server\TcpServer\IReceiveHandler $handler
* @return void
*/
public function handle(IReceiveData $data, IReceiveHandler $handler);

}
36 changes: 36 additions & 0 deletions src/Server/TcpServer/Error/TcpRouteNotFoundHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Imi\Server\TcpServer\Error;

use Imi\App;
use Imi\Bean\Annotation\Bean;
use Imi\Server\TcpServer\IReceiveHandler;
use Imi\Server\TcpServer\Message\IReceiveData;

/**
* TCP 未匹配路由时的处理器
* @Bean("TcpRouteNotFoundHandler")
*/
class TcpRouteNotFoundHandler implements ITcpRouteNotFoundHandler
{
/**
* 处理器类名,如果为null则使用默认处理
*
* @var string
*/
protected $handler = null;

/**
* 处理方法
*
* @param \Imi\Server\TcpServer\Message\IReceiveData $data
* @param \Imi\Server\TcpServer\IReceiveHandler $handler
* @return void
*/
public function handle(IReceiveData $data, IReceiveHandler $handler)
{
if(null !== $this->handler)
{
return App::getBean($this->handler)->handle($data, $handler);
}
}
}
25 changes: 20 additions & 5 deletions src/Server/TcpServer/Middleware/RouteMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Imi\RequestContext;
use Imi\Bean\Annotation\Bean;
use Imi\Server\Annotation\ServerInject;
use Imi\Server\TcpServer\ReceiveHandler;
use Imi\Server\TcpServer\IReceiveHandler;
use Imi\Server\TcpServer\Message\IReceiveData;
Expand All @@ -12,6 +13,20 @@
*/
class RouteMiddleware implements IMiddleware
{
/**
* @ServerInject("TcpRoute")
*
* @var \Imi\Server\TcpServer\Route\TcpRoute
*/
protected $route;

/**
* @ServerInject("TcpRouteNotFoundHandler")
*
* @var \Imi\Server\TcpServer\Error\ITcpRouteNotFoundHandler
*/
protected $notFoundHandler;

/**
* 处理方法
*
Expand All @@ -22,18 +37,18 @@ class RouteMiddleware implements IMiddleware
public function process(IReceiveData $data, IReceiveHandler $handler)
{
// 路由解析
$route = RequestContext::getServerBean('TcpRoute');
$result = $route->parse($data->getFormatData());
$result = $this->route->parse($data->getFormatData());
if(null === $result || !is_callable($result->callable))
{
// 未找到匹配的命令,TODO:处理

// 未匹配到路由
$result = $this->notFoundHandler->handle($data, $handler);
}
else
{
RequestContext::set('routeResult', $result);
$result = $handler->handle($data);
}
return $handler->handle($data);
return $result;
}

}
21 changes: 21 additions & 0 deletions src/Server/UdpServer/Error/IUdpRouteNotFoundHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Imi\Server\UdpServer\Error;

use Imi\Server\UdpServer\IPacketHandler;
use Imi\Server\UdpServer\Message\IPacketData;

/**
* 处理未找到 UDP 路由情况的接口
*/
interface IUdpRouteNotFoundHandler
{
/**
* 处理方法
*
* @param \Imi\Server\UdpServer\Message\IPacketData $data
* @param \Imi\Server\UdpServer\IPacketHandler $handler
* @return void
*/
public function handle(IPacketData $data, IPacketHandler $handler);

}
37 changes: 37 additions & 0 deletions src/Server/UdpServer/Error/UdpRouteNotFoundHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace Imi\Server\UdpServer\Error;

use Imi\App;
use Imi\Bean\Annotation\Bean;
use Imi\Server\UdpServer\IPacketHandler;
use Imi\Server\UdpServer\Message\IPacketData;
use Imi\Server\UdpServer\Error\IUdpRouteNotFoundHandler;

/**
* UDP 未匹配路由时的处理器
* @Bean("UdpRouteNotFoundHandler")
*/
class UdpRouteNotFoundHandler implements IUdpRouteNotFoundHandler
{
/**
* 处理器类名,如果为null则使用默认处理
*
* @var string
*/
protected $handler = null;

/**
* 处理方法
*
* @param \Imi\Server\UdpServer\Message\IPacketData $data
* @param \Imi\Server\UdpServer\IPacketHandler $handler
* @return void
*/
public function handle(IPacketData $data, IPacketHandler $handler)
{
if(null !== $this->handler)
{
return App::getBean($this->handler)->handle($data, $handler);
}
}
}
25 changes: 20 additions & 5 deletions src/Server/UdpServer/Middleware/RouteMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Imi\RequestContext;
use Imi\Bean\Annotation\Bean;
use Imi\Server\Annotation\ServerInject;
use Imi\Server\UdpServer\PacketHandler;
use Imi\Server\UdpServer\IPacketHandler;
use Imi\Server\UdpServer\Message\IPacketData;
Expand All @@ -12,6 +13,20 @@
*/
class RouteMiddleware implements IMiddleware
{
/**
* @ServerInject("UdpRoute")
*
* @var \Imi\Server\UdpServer\Route\UdpRoute
*/
protected $route;

/**
* @ServerInject("UdpRouteNotFoundHandler")
*
* @var \Imi\Server\UdpServer\Error\IUdpRouteNotFoundHandler
*/
protected $notFoundHandler;

/**
* 处理方法
*
Expand All @@ -22,18 +37,18 @@ class RouteMiddleware implements IMiddleware
public function process(IPacketData $data, IPacketHandler $handler)
{
// 路由解析
$route = RequestContext::getServerBean('UdpRoute');
$result = $route->parse($data->getFormatData());
$result = $this->route->parse($data->getFormatData());
if(null === $result || !is_callable($result->callable))
{
// 未找到匹配的命令,TODO:处理

// 未匹配到路由
$result = $this->notFoundHandler->handle($data, $handler);
}
else
{
RequestContext::set('routeResult', $result);
$result = $handler->handle($data);
}
return $handler->handle($data);
return $result;
}

}
21 changes: 21 additions & 0 deletions src/Server/WebSocket/Error/IWSRouteNotFoundHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Imi\Server\WebSocket\Error;

use Imi\Server\WebSocket\Message\IFrame;
use Imi\Server\WebSocket\IMessageHandler;

/**
* 处理未找到 WebSocket 路由情况的接口
*/
interface IWSRouteNotFoundHandler
{
/**
* 处理方法
*
* @param IFrame $frame
* @param IMessageHandler $handler
* @return mixed
*/
public function handle(IFrame $frame, IMessageHandler $handler);

}
36 changes: 36 additions & 0 deletions src/Server/WebSocket/Error/WSRouteNotFoundHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Imi\Server\WebSocket\Error;

use Imi\App;
use Imi\Bean\Annotation\Bean;
use Imi\Server\WebSocket\Message\IFrame;
use Imi\Server\WebSocket\IMessageHandler;

/**
* WebSocket 未匹配路由时的处理器
* @Bean("WSRouteNotFoundHandler")
*/
class WSRouteNotFoundHandler implements IWSRouteNotFoundHandler
{
/**
* 处理器类名,如果为null则使用默认处理
*
* @var string
*/
protected $handler = null;

/**
* 处理方法
*
* @param IFrame $frame
* @param IMessageHandler $handler
* @return mixed
*/
public function handle(IFrame $frame, IMessageHandler $handler)
{
if(null !== $this->handler)
{
return App::getBean($this->handler)->handle($frame, $handler);
}
}
}
26 changes: 21 additions & 5 deletions src/Server/WebSocket/Middleware/RouteMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
namespace Imi\Server\WebSocket\Middleware;

use Imi\App;
use Imi\RequestContext;
use Imi\Bean\Annotation\Bean;
use Imi\Server\Annotation\ServerInject;
use Imi\Server\WebSocket\Message\IFrame;
use Imi\Server\WebSocket\MessageHandler;
use Imi\Server\WebSocket\IMessageHandler;
Expand All @@ -12,6 +14,20 @@
*/
class RouteMiddleware implements IMiddleware
{
/**
* @ServerInject("WSRoute")
*
* @var \Imi\Server\WebSocket\Route\WSRoute
*/
protected $route;

/**
* @ServerInject("WSRouteNotFoundHandler")
*
* @var \Imi\Server\Http\Error\IWSRouteNotFoundHandler
*/
protected $notFoundHandler;

/**
* 处理方法
*
Expand All @@ -22,18 +38,18 @@ class RouteMiddleware implements IMiddleware
public function process(IFrame $frame, IMessageHandler $handler)
{
// 路由解析
$route = RequestContext::getServerBean('WSRoute');
$result = $route->parse($frame->getFormatData());
$result = $this->route->parse($frame->getFormatData());
if(null === $result || !is_callable($result->callable))
{
// 未找到匹配的命令,TODO:处理

// 未匹配到路由
$result = $this->notFoundHandler->handle($frame, $handler);
}
else
{
RequestContext::set('routeResult', $result);
$result = $handler->handle($frame);
}
return $handler->handle($frame);
return $result;
}

}
21 changes: 21 additions & 0 deletions tests/unit/HttpServer/ApiServer/Error/HttpNotFoundHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Imi\Test\HttpServer\ApiServer\Error;

use Imi\Bean\Annotation\Bean;
use Imi\Util\Stream\MemoryStream;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Imi\Server\Http\Error\IHttpNotFoundHandler;

/**
* @Bean("MyHttpNotFoundHandler")
*/
class HttpNotFoundHandler implements IHttpNotFoundHandler
{
public function handle(RequestHandlerInterface $requesthandler, ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
return $response->withBody(new MemoryStream('gg'));
}

}
4 changes: 4 additions & 0 deletions tests/unit/HttpServer/ApiServer/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'Imi\Test\HttpServer\ApiServer\Controller',
'Imi\Test\HttpServer\Modules',
'Imi\Test\HttpServer\OutsideController',
'Imi\Test\HttpServer\ApiServer\Error',
],
'beans' => [
'SessionManager' => [
Expand Down Expand Up @@ -48,6 +49,9 @@
'ExecuteTimeoutMiddleware' => [
'maxExecuteTime' => 3000,
],
'HttpNotFoundHandler' => [
'handler' => 'MyHttpNotFoundHandler',
],
],
'middleware' => [
'groups' => [
Expand Down
Loading

0 comments on commit cf8fe40

Please sign in to comment.