MINA SOCKET SERVER学习笔记(一)Writing IoFilter

IoFilter

IoFilter is one of the MINA core constructs that serves a very important role. It filters all I/O events and requests between IoService and IoHandler. If you have an experience with web application programming, you can safely think that it's a cousin of Servlet filter. Many out-of-the-box filters are provided to accelerate network application development pace by simplifying typical cross-cutting concerns using the out-of-the-box filters such as:
IoFilter是MINA核心构造之一,扮演非常重要的角色。它过滤所有的I/O事件和请求,这些事件和请求由IoService最终到达IoHandler。假如你有web应用编程的经验,你确实可以把它比作Servlet filter。许多具有独特创意的filters被开发出来以加速网络应用的开发速度,简单、典型、横切关注点地使用这些filters,例如:

  • LoggingFilter logs all events and requests.
    LoggingFilter日志记录所有的事件和请求。
  • ProtocolCodecFilter converts an incoming ByteBuffer into message POJO and vice versa.
    ProtocolCodecFilter将接收的字节流转换为消息POJO等等。
  • CompressionFilter compresses all data.
    CompressionFilter压缩所有的数据。
  • SSLFilter adds SSL - TLS - StartTLS support.
    SSLFilter加入SSL-TLS-StartTLS支持。
  • and many more!
    还有很多!

It's easy to implement an IoFilter in general, but you might also need to know specifics of MINA internals. Any related internal properties will be explained here.

通常,实现一个IoFilter还是很容易的,但是你可能需要了解MINA的内部细节,以下介绍所有相关的内部内容。

 

Overriding Events Selectively

You can extend IoAdapter instead of implementing IoFilter directly. Unless overriden, any received events will be forward to the next filter immediately:
除了直接实现IoFilter,你也可以扩展IoAdapter。除了重载了的事件,任何接收事件都将立即转发到下一个filter。

Transforming a Write Request

If you are going to transform an incoming write request via IoSession.write(), things can get pretty tricky. For example, let's assume your filter transforms HighLevelMessage to LowLevelMessage when IoSession.write() is invoked with a HighLevelMessage object. You could insert appropriate transformation code to your filter's filterWrite() method and think that's all. However, you have to note that you also need to take care of messageSent event because an IoHandler or any filters next to yours will expect messageSent() method is called with HighLevelMessage as a parameter, because it's irrational for the caller to get notified that LowLevelMessage is sent when the caller actually wrote HighLevelMessage. Consequently, you have to implement both filterWrite() and messageSent() if your filter performs transformation.

 

Please also note that you still need to implement similar mechanism even if the types of the input object and the output object are identical (e.g. CompressionFilter) because the caller of IoSession.write() will expect exactly what he wrote in his or her messageSent() handler method.

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值