Skip to content

Commit

Permalink
优化 UDP Middleware 机制
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Aug 23, 2019
1 parent eaf4fcb commit 753f5a7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Server/UdpServer/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function dispatch(IPacketData $data)
protected function getMiddlewares()
{
return array_merge($this->middlewares, [

\Imi\Server\UdpServer\Middleware\ActionWrapMiddleware::class,
]);
}
}
36 changes: 36 additions & 0 deletions src/Server/UdpServer/Middleware/ActionWrapMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Imi\Server\UdpServer\Middleware;

use Imi\RequestContext;
use Imi\Bean\Annotation\Bean;
use Imi\Server\UdpServer\PacketHandler;
use Imi\Server\UdpServer\IPacketHandler;
use Imi\Server\UdpServer\Message\IPacketData;

/**
* @Bean
*/
class ActionWrapMiddleware implements IMiddleware
{
/**
* 处理方法
*
* @param IPacketData $data
* @param IPacketHandler $handler
* @return void
*/
public function process(IPacketData $data, IPacketHandler $handler)
{
// 获取路由结果
$result = RequestContext::get('routeResult');
if(null === $result)
{
return $handler->handle($data);
}
$middlewares = $result->routeItem->middlewares;
$middlewares[] = ActionMiddleware::class;
$handler = new PacketHandler($middlewares);
return $handler->handle($data);
}

}
5 changes: 0 additions & 5 deletions src/Server/UdpServer/Middleware/RouteMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ public function process(IPacketData $data, IPacketHandler $handler)
else
{
RequestContext::set('routeResult', $result);

$middlewares = $result->routeItem->middlewares;
$middlewares[] = ActionMiddleware::class;
$handler = new PacketHandler($middlewares);
return $handler->handle($data);
}
return $handler->handle($data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Imi\Test\UDPServer\MainServer\Controller;

use Imi\ConnectContext;
use Imi\RequestContext;
use Imi\Server\Route\Annotation\Udp\UdpRoute;
use Imi\Server\Route\Annotation\Udp\UdpAction;
use Imi\Server\Route\Annotation\Udp\UdpController;
Expand All @@ -23,7 +24,8 @@ public function hello()
{
$data = $this->data->getFormatData();
return [
'time' => date($data->format, $data->time),
'time' => date($data->format, $data->time),
'middlewareData' => RequestContext::get('middlewareData'),
];
}

Expand Down
11 changes: 6 additions & 5 deletions tests/unit/UDPServer/MainServer/Middleware/Test.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<?php
namespace Imi\Test\UDPServer\MainServer\Middleware;

use Imi\RequestContext;
use Imi\Bean\Annotation\Bean;
use Imi\Server\UDPServer\IReceiveHandler;
use Imi\Server\UDPServer\Message\IReceiveData;
use Imi\Server\UDPServer\Middleware\IMiddleware;
use Imi\Server\UdpServer\IPacketHandler;
use Imi\Server\UdpServer\Message\IPacketData;
use Imi\Server\UdpServer\Middleware\IMiddleware;

/**
* @Bean
*/
class Test implements IMiddleware
{
public function process(IReceiveData $data, IReceiveHandler $handler)
public function process(IPacketData $data, IPacketHandler $handler)
{
var_dump('test middleware');
RequestContext::set('middlewareData', 'imi');
return $handler->handle($data, $handler);
}
}
2 changes: 1 addition & 1 deletion tests/unit/UDPServer/MainServer/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'UdpDispatcher' => [
'middlewares' => [
\Imi\Server\UdpServer\Middleware\RouteMiddleware::class,
\Imi\Server\UdpServer\Middleware\ActionMiddleware::class,
\Imi\Test\UDPServer\MainServer\Middleware\Test::class,
],
],
'ConnectContextStore' => [
Expand Down

0 comments on commit 753f5a7

Please sign in to comment.