java netty之ChannelInboundByteHandlerAdapter
来源:互联网 发布:flash cs3 for mac 编辑:程序博客网 时间:2024/06/05 14:17
这篇文章分析另外一个表重要比较基础的handler : ChannelInboundByteHandlerAdapter
当我们编写的服务器程序需要直接处理byte的时候,可以直接扩展它来实现inboundhandler,用于处理传送过来的byte数据。。。例如举一个典型的用法:
public class MyChannelHandler extends ChannelInboundByteHandlerAdapter {@Overrideprotected void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in)throws Exception {// TODO Auto-generated method stub//while (in.isReadable()) {//System.out.print((char)in.readByte());//}final String name = ctx.name();ByteBuf b = ctx.alloc().buffer(); //创建一个buffer用于存放数据b.writeBytes("aaa".getBytes());ctx.pipeline().write(b); //调用pipeline的write方法,将数据发送出去ctx.pipeline().flush().addListener(new ChannelFutureListener(){@Overridepublic void operationComplete(ChannelFuture future)throws Exception {// TODO Auto-generated method stubSystem.out.println(name);future.channel().close().sync();}});}}
该类直接继承自ChannelInboundByteHandlerAdapter,然后实现了inboundBufferUpdated方法用于处理读取进来的数据。。。将这个类的对象加入到channel的pipeline上面之后,当这个channel已经有数据读进来之后会调用inboundBufferUpdated方法来处理。。。
我们首先还是来看一下ChannelInboundByteHandlerAdapter的继承体系:
ChannelInboundByteHandlerAdapter直接继承了ChannelStateHandlerAdapter,这个类型在上面的一篇文章中也有说明,它继承了stathandler接口,然后实现了里面的方法,不过这些方法实现的都很简单,无非是从当前pipeline上面向下寻找到另一个stathandler,调用相应的方法。。。
然后ChannelInboundByteHandlerAdapter还实现了ChannelInboundByteHandler接口,这个接口中多定义了一个方法,那就是discardInboundReadBytes。。。
好了,接下来我们来看看ChannelInboundByteHandlerAdapter的定义吧:
public abstract class ChannelInboundByteHandlerAdapter extends ChannelStateHandlerAdapter implements ChannelInboundByteHandler { /** * Create a new unpooled {@link ByteBuf} by default. Sub-classes may override this to offer a more * optimized implementation. */ @Override //用于构创建新的buffer public ByteBuf newInboundBuffer(ChannelHandlerContext ctx) throws Exception { return ChannelHandlerUtil.allocate(ctx); } @Override //抛弃已经读取的数据 public void discardInboundReadBytes(ChannelHandlerContext ctx) throws Exception { ctx.inboundByteBuffer().discardSomeReadBytes(); } @Override //当有新的收读取到时候,会调用过这个方法来处理这些读取到的数据 public final void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception { inboundBufferUpdated(ctx, ctx.inboundByteBuffer()); } /** * Callback which will get notifed once the given {@link ByteBuf} received more data to read. What will be done * with the data at this point is up to the implementation. * Implementations may choose to read it or just let it in the buffer to read it later. */ //用户自己实现这个方法,真正的用于处理读取到的数据 protected abstract void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) throws Exception;}
实现代码还是很简单,首先重载了newInboundBuffer方法,至于inboundBufferUpdated方法,其实是用来调用用户自定义的inboundBufferUpdated方法来来处理新进来的数据。。
因此用户编码的时候,只需要继承ChannelInboundByteHandlerAdapter类,然后实现inboundBufferUpdated方法就可以了。。。。
- java netty之ChannelInboundByteHandlerAdapter
- netty ChannelInboundByteHandlerAdapter
- java netty之AbstractNioChannel
- Java netty之NioServerSocketChannel
- java netty之NioSocketChannel
- java netty之ChannelPipeline
- java netty之DefaultChannelHandlerContext
- java netty之DefaultChannelPipeline
- java netty之ChannelInitializer
- java netty之ByteToMessageDecoder
- java netty之MessageToByteEncoder
- java netty之ByteToMessageDecoder
- java netty之ChannelInitializer
- Netty之java序列化
- Netty 之 netty源码学习之大话java NIO
- java netty之ServerBootstrap的启动
- java netty handler之ChannelInboundHander与ChannelOutboundHandler
- Netty 权威指南之java 序列化
- Oracle 回收站功能,彻底删除表(purge recyclebin)
- ConditionVariable线程控制变量的使用
- 数据结构读书笔记(三)(C语言)
- .net中Cache的应用
- wsdl
- java netty之ChannelInboundByteHandlerAdapter
- 简单理解PROC
- cookie 和session 的区别详解
- 李嘉诚百句创业名言
- 为什么学C++比C#的长期风险低
- Oracle中用户scott中表的详解
- ubuntu 12.04装matplotlib错误
- Deprecated: mysql_connect(): The mysqlextension is deprecated and will be removed in the future: use
- impala1.0.1安装