java netty decoder与encoder的使用

在前面的文章中,对netty的使用都是基于byte类型的,但是在实际情况,直接处理byte数据是很少的,一般都是将这些数据转换成自己定义的一些类型。

也就是说在实际情况下decoder与encoder都是用到比较多的handler类型,想要了解他们是怎么实现的,我觉得首先应该知道他们是怎么使用的,那么就用官方说明的UnixTime作为例子来简单的说民一下decoder与encoder是怎么使用的吧。。。


首先先来看看client端的代码吧,由于代码比较少,这里代码就没有弄注释了,反正也比较的简单,一看就能明白是什么意思,另外没有分好几个类来写,直接在一个类中使用内部类来实现了,这样免得麻烦。。好吧,看代码:

[java]  view plain copy
  1. import io.netty.bootstrap.Bootstrap;  
  2. import io.netty.buffer.ByteBuf;  
  3. import io.netty.buffer.MessageBuf;  
  4. import io.netty.channel.ChannelFuture;  
  5. import io.netty.channel.ChannelHandlerContext;  
  6. import io.netty.channel.ChannelInboundMessageHandlerAdapter;  
  7. import io.netty.channel.ChannelInitializer;  
  8. import io.netty.channel.EventLoopGroup;  
  9. import io.netty.channel.nio.NioEventLoopGroup;  
  10. import io.netty.channel.socket.nio.NioSocketChannel;  
  11. import io.netty.handler.codec.ByteToMessageDecoder;  
  12.   
  13. public class Client {  
  14.       
  15.     public void run() throws InterruptedException {  
  16.         EventLoopGroup worker = new NioEventLoopGroup();  
  17.         try {  
  18.             Bootstrap b = new Bootstrap();  
  19.             b.group(worker);  
  20.             b.channel(NioSocketChannel.class);  
  21.             b.handler(new ChannelInitializer<NioSocketChannel>(){  
  22.   
  23.                 @Override  
  24.                 protected void initChannel(NioSocketChannel ch)  
  25.                         throws Exception {  
  26.                     // TODO Auto-generated method stub  
  27.                     ch.pipeline().addLast(new TimeDecoder(), new ClientHandler());  
  28.                 }  
  29.                   
  30.             });  
  31.             ChannelFuture f = b.connect("127.0.0.1"80).sync();  
  32.             f.channel().closeFuture().sync();  
  33.         } finally {  
  34.             worker.shutdownGracefully();  
  35.         }  
  36.     }  
  37.       
  38.     public final class TimeDecoder extends ByteToMessageDecoder{  
  39.   
  40.         @Override  
  41.         protected void decode(ChannelHandlerContext ctx, ByteBuf in,  
  42.                 MessageBuf<Object> out) throws Exception {  
  43.             // TODO Auto-generated method stub  
  44.             while (in.readableBytes() >= 4) {  
  45.                 out.add(new UnixTime(in.readInt()));  
  46.             }  
  47.         }  
  48.           
  49.     }  
  50.       
  51.     public final class ClientHandler extends ChannelInboundMessageHandlerAdapter{  
  52.   
  53.         @Override  
  54.         public void messageReceived(ChannelHandlerContext ctx, Object msg)  
  55.                 throws Exception {  
  56.             // TODO Auto-generated method stub  
  57.             UnixTime time = (UnixTime)msg;  
  58.             System.out.println(time);  
  59.         }  
  60.           
  61.     }  
  62.       
  63.     public static void main(String args[]) throws InterruptedException {  
  64.         new Client().run();  
  65.     }  
  66. }  

首先从类型的定义看起吧,TimeDecoder类型直接继承自ByteToMessageDecoder类型,这里需要自己实现decode方法,说白了就是将读进来的字节类型转化为定义的java类型对象。。。

然后才是真正处理读进来的数据的handler,ClientHnadler,它继承自ChannelInboundMessageHandlerAdapter,需要自己实现messageReceived方法,其实这里传进来的msg对象就是刚刚通过读进来的byte数据生成的。。。

好了两个handler的定义就差不多了,这里需要注意的代码:

[java]  view plain copy
  1. ch.pipeline().addLast(new TimeDecoder(), new ClientHandler());  
在初始化函数中,为当前的pipeline添加了刚刚说明的两个handler,由于调用的是addLast函数,所以这两个handler的次序如下图:


当然这里对pipeline的描述还是不够正确,因为在最前面和最后面还分别有一个默认的handler。。就不细说了。。

我们在前面的文章中有提到对于读取数据的事件,netty会从pipeline的前面向后寻找相应的inboundhandler来处理,因此我们可以知道这里handler的调用顺序就是先是decoder然后才是ClientHandler。。。其实基本上也都知道decoder是怎么实现的了,,,因为前面的分析可以知道netty的pipeline上面,handler可以访问别的handler的context的buffer。。。至于具体是怎么实现的,就留给接下来的文章吧。。。


好了,接下来来看Server部分的代码:

[java]  view plain copy
  1. import newfjs.UnixTime;  
  2. import io.netty.bootstrap.ServerBootstrap;  
  3. import io.netty.buffer.ByteBuf;  
  4. import io.netty.channel.ChannelFuture;  
  5. import io.netty.channel.ChannelFutureListener;  
  6. import io.netty.channel.ChannelHandlerContext;  
  7. import io.netty.channel.ChannelInboundByteHandlerAdapter;  
  8. import io.netty.channel.ChannelInitializer;  
  9. import io.netty.channel.EventLoopGroup;  
  10. import io.netty.channel.nio.NioEventLoopGroup;  
  11. import io.netty.channel.socket.SocketChannel;  
  12. import io.netty.channel.socket.nio.NioServerSocketChannel;  
  13. import io.netty.channel.socket.nio.NioSocketChannel;  
  14. import io.netty.handler.codec.MessageToByteEncoder;  
  15.   
  16. public class Server {  
  17.       
  18.       
  19.       
  20.     public void run() throws InterruptedException {  
  21.         EventLoopGroup boss = new NioEventLoopGroup();  
  22.         EventLoopGroup worker = new NioEventLoopGroup();  
  23.           
  24.         try {  
  25.             ServerBootstrap b = new ServerBootstrap();  
  26.             b.group(boss, worker);  
  27.             b.channel(NioServerSocketChannel.class);     
  28.             b.childHandler(new ChannelInitializer<NioSocketChannel>(){  
  29.   
  30.                 @Override  
  31.                 protected void initChannel(NioSocketChannel ch)  
  32.                         throws Exception {  
  33.                     // TODO Auto-generated method stub  
  34.                     ch.pipeline().addLast(new TimeServerHandler());  
  35.                     ch.pipeline().addFirst(new TimeEncoder());  
  36.                 }  
  37.                   
  38.             });  
  39.             ChannelFuture f = b.bind(80).sync();  
  40.             f.channel().closeFuture().sync();  
  41.         } finally {  
  42.             boss.shutdownGracefully();  
  43.             worker.shutdownGracefully();  
  44.         }  
  45.     }  
  46.       
  47.     private final class TimeEncoder extends MessageToByteEncoder<UnixTime>{  
  48.   
  49.         @Override  
  50.         protected void encode(ChannelHandlerContext ctx, UnixTime msg,  
  51.                 ByteBuf out) throws Exception {  
  52.             // TODO Auto-generated method stub  
  53.             out.writeInt(msg.value());  
  54.         }  
  55.           
  56.     }  
  57.       
  58.     private final class TimeServerHandler extends ChannelInboundByteHandlerAdapter{  
  59.   
  60.         @Override  
  61.         public void channelActive(ChannelHandlerContext ctx) throws Exception {  
  62.             ctx.pipeline().write(new UnixTime());  
  63.             ctx.pipeline().write(new UnixTime());  
  64.             ctx.pipeline().write(new UnixTime());  
  65.             ctx.pipeline().write(new UnixTime()).channel().pipeline().flush().addListener(new ChannelFutureListener(){  
  66.   
  67.                 @Override  
  68.                 public void operationComplete(ChannelFuture future)  
  69.                         throws Exception {  
  70.                     // TODO Auto-generated method stub  
  71.                     future.channel().pipeline().close();  
  72.                 }  
  73.                   
  74.             });  
  75.         }  
  76.           
  77.           
  78.         @Override  
  79.         protected void inboundBufferUpdated(ChannelHandlerContext ctx,  
  80.                 ByteBuf in) throws Exception {  
  81.             // TODO Auto-generated method stub  
  82.             in.discardReadBytes();  
  83.         }  
  84.           
  85.     }  
  86.       
  87.     public static void main(String args[]) throws InterruptedException{  
  88.         new Server().run();  
  89.     }  
  90. }  

在server部分定义了两个handler,其实这两个handler还分别是两种不同的类型,先来是TimeEncoder,它最终还是一个outboundhandler,也就是当调用wirte一类方法的时候会经过这个handler,这个handler用于将用户定义的数据转化成byte类型。

然后另外一个handler是TimeServerHandler,它重写了channelActive方法,也就是说当这个channel已经连接好了,那么就会调用这个方法,它直接写4个用户定义的类型。。。

至于handler初始化代码:

[java]  view plain copy
  1. protected void initChannel(NioSocketChannel ch)  
  2.                         throws Exception {  
  3.                     // TODO Auto-generated method stub  
  4.                     ch.pipeline().addLast(new TimeServerHandler());  
  5.                     ch.pipeline().addFirst(new TimeEncoder());  
  6.                 }  
那么最后他们的样子是:


不过其实由于这两个handler是两种不同的类型,讨论他们的位置关系也没有什么意义。。但是我们通过前面的文章可以知道,当调用write方法的时候,netty会在pipeline上面从后向前的寻找outboundhandler,然后调用相应的方法,而且我们知道在pipeline的最前面还有一个默认的outboundhandler,它将会最终用于处理对外的一些操作。。其实也能猜出来encoder之后会发生什么样子的事情。。还是留个以后分析吧。。

这样,server与client就ok了,首先启动server,然后启动client,当client连接上server的时候,server就会向client发送4个自定义的time,然后再关闭当前的channel,当client接受之后,先将byte类型转化为具体的类型,然后在输出。。

以后的文章分析decoder与encoder两种handler是具体怎么实现的。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值