Grizzly NIO框架 理论篇 【2】

系统 1676 0

Transports and Connections

这两个东西,是灰熊框架的核心结构~Transports工具包含有TCP或UDP的协议,合并各种组件资源(Thread-Pool 线程池、Memory Manager 内存管理器等等。
""
从结构来说,没什么东西,主要还是使用上
1、使用Future模式建立Connection
  1. Future < Connection > clientConnectionFuture = tcpNioTransport . connect ( "grizzly.java.net" , 80 );
  2. Connection clientConnection = clientConnectionFuture . get ();
 
2、可以针对Connection添加CloseHandler等事件处理
  1. Connection conn ;
  2. conn . addCloseListener ( new CloseListener < Closeable , ICloseType >() {
  3. @Override
  4. public void onClosed ( Closeable closeable , ICloseType type )
  5. throws IOException {
  6. // TODO Auto-generated method stub
  7. }
  8. });

FilterChains and Filters

过滤器给予了Grizzly无限扩展性。FilterChains是Filter的过程封装,可以对Filter的逐步处理。
比如要扩展成http、Filter组合如下:
""
比如要给把http修改成https,那么只需要增加SSLFilter:
 
""
其中,Transport Filter是所有处理的基础,用于和Grizzly的Selector交互;
另外我认需要认识下BaseFilter,所有Filter都继承于它:
  1. /**
  2. * 读流程,读取网络数据,或上一个Filter read之后的message
  3. */
  4. public NextAction handleRead ( FilterChainContext ctx ) throws IOException ;
  5. /**
  6. * 写流程,写如网络数据,或者处理上一个Filter write之后的message
  7. */
  8. public NextAction handleWrite ( FilterChainContext ctx ) throws IOException ;
  9. /**
  10. * 新加入的连接
  11. */
  12. public NextAction handleConnect ( FilterChainContext ctx ) throws IOException ;
  13. /**
  14. * 同handleConnect
  15. */
  16. public NextAction handleAccept ( FilterChainContext ctx ) throws IOException ;
  17. /**
  18. * 连接断开之后调用
  19. */
  20. public NextAction handleClose ( FilterChainContext ctx ) throws IOException ;
两个Filter之间如何衔接起来,我们需要了解NextAction是怎么工作,并有哪些类型:

StopAction

如果要停止Filter当前工作流程,就直接返回
return ctx.getStopAction();
如果需要中断工作,并把当前没有处理的数据流放到下一次调用,可以加入参数:

return ctx.getStopAction(incompleteChunk);

InvokeAction

如果是继续下一次Filter流程,就返回InvokeAction

return ctx.getInvokeAction();

如果数据流里面有粘包(两个数据包一起发来,我们需要一个个处理),同样可以添加参数:

return ctx.getInvokeAction(incompleteChunk);

RerunFilterAction

return ctx.getRerunFilterAction()

这个就是当前Filter的action多执行一次。

SuspendAction

return ctx.getSuspendAction();

这个是暂停。。。。中断线程,并且通过另外一个线程调用

ctx.resume() :

ForkAction (was SuspendStopAction)

return ctx.getForkAction();

和上一个类似。





Grizzly NIO框架 理论篇 【2】


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论