在前面的文章中,对netty的使用都是基于byte类型的,但是在实际情况,直接处理byte数据是很少的,一般都是将这些数据转换成自己定义的一些类型。
也就是说在实际情况下decoder与encoder都是用到比较多的handler类型,想要了解他们是怎么实现的,我觉得首先应该知道他们是怎么使用的,那么就用官方说明的UnixTime作为例子来简单的说民一下decoder与encoder是怎么使用的吧。。。
首先先来看看client端的代码吧,由于代码比较少,这里代码就没有弄注释了,反正也比较的简单,一看就能明白是什么意思,另外没有分好几个类来写,直接在一个类中使用内部类来实现了,这样免得麻烦。。好吧,看代码:
import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.MessageBuf; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundMessageHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.ByteToMessageDecoder; public class Client { public void run() throws InterruptedException { EventLoopGroup worker = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(worker); b.channel(NioSocketChannel.class); b.handler(new ChannelInitializer<NioSocketChannel>(){ @Override protected void initChannel(NioSocketChannel ch) throws Exception { // TODO Auto-generated method stub ch.pipeline().addLast(new TimeDecoder(), new ClientHandler()); } }); ChannelFuture f = b.connect("127.0.0.1", 80).sync(); f.channel().closeFuture().sync(); } finally { worker.shutdownGracefully(); } } public final class TimeDecoder extends ByteToMessageDecoder{ @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, MessageBuf<Object> out) throws Exception { // TODO Auto-generated method stub while (in.readableBytes() >= 4) { out.add(new UnixTime(in.readInt())); } } } public final class ClientHandler extends ChannelInboundMessageHandlerAdapter{ @Override public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception { // TODO Auto-generated method stub UnixTime time = (UnixTime)msg; System.out.println(time); } } public static void main(String args[]) throws InterruptedException { new Client().run(); } }
首先从类型的定义看起吧,TimeDecoder类型直接继承自ByteToMessageDecoder类型,这里需要自己实现decode方法,说白了就是将读进来的字节类型转化为定义的java类型对象。。。
然后才是真正处理读进来的数据的handler,ClientHnadler,它继承自ChannelInboundMessageHandlerAdapter,需要自己实现messageReceived方法,其实这里传进来的msg对象就是刚刚通过读进来的byte数据生成的。。。
好了两个handler的定义就差不多了,这里需要注意的代码:
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部分的代码:
import newfjs.UnixTime; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundByteHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.MessageToByteEncoder; public class Server { public void run() throws InterruptedException { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(boss, worker); b.channel(NioServerSocketChannel.class); b.childHandler(new ChannelInitializer<NioSocketChannel>(){ @Override protected void initChannel(NioSocketChannel ch) throws Exception { // TODO Auto-generated method stub ch.pipeline().addLast(new TimeServerHandler()); ch.pipeline().addFirst(new TimeEncoder()); } }); ChannelFuture f = b.bind(80).sync(); f.channel().closeFuture().sync(); } finally { boss.shutdownGracefully(); worker.shutdownGracefully(); } } private final class TimeEncoder extends MessageToByteEncoder<UnixTime>{ @Override protected void encode(ChannelHandlerContext ctx, UnixTime msg, ByteBuf out) throws Exception { // TODO Auto-generated method stub out.writeInt(msg.value()); } } private final class TimeServerHandler extends ChannelInboundByteHandlerAdapter{ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.pipeline().write(new UnixTime()); ctx.pipeline().write(new UnixTime()); ctx.pipeline().write(new UnixTime()); ctx.pipeline().write(new UnixTime()).channel().pipeline().flush().addListener(new ChannelFutureListener(){ @Override public void operationComplete(ChannelFuture future) throws Exception { // TODO Auto-generated method stub future.channel().pipeline().close(); } }); } @Override protected void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) throws Exception { // TODO Auto-generated method stub in.discardReadBytes(); } } public static void main(String args[]) throws InterruptedException{ new Server().run(); } }
在server部分定义了两个handler,其实这两个handler还分别是两种不同的类型,先来是TimeEncoder,它最终还是一个outboundhandler,也就是当调用wirte一类方法的时候会经过这个handler,这个handler用于将用户定义的数据转化成byte类型。
然后另外一个handler是TimeServerHandler,它重写了channelActive方法,也就是说当这个channel已经连接好了,那么就会调用这个方法,它直接写4个用户定义的类型。。。
至于handler初始化代码:
protected void initChannel(NioSocketChannel ch) throws Exception { // TODO Auto-generated method stub ch.pipeline().addLast(new TimeServerHandler()); ch.pipeline().addFirst(new TimeEncoder()); }那么最后他们的样子是:
不过其实由于这两个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是具体怎么实现的。。。