package io.netty.inboundAndoutboundHandler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;
public class MyByteToLongDecoder extends ByteToMessageDecoder {
/**
*
* 会根据接收到数据被调用多次,直到确定没有新的元素被添加到list
* bytebuf没有更多的可读字节
* @param channelHandlerContext
* @param byteBuf 入栈客户端传来的数据
* @param list 将解码后的数据 放到list中 然后传递到InBound中处理 对应的inbuoundChannel也会被调用多次
* @throws Exception
*/
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
System.out.println("MyByteToLongDecoder#decode");
//将客户端传过来的byteBufer 转换为long 8个字节
if(byteBuf.readableBytes()>=8){
list.add(byteBuf.readLong());
}
}
}
package io.netty.inboundAndoutboundHandler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import java.util.List;
public class MyByteToLongDecoder2 extends ReplayingDecoder<Void> {
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
System.out.println("MyByteToLongDecoder2#decode");
//将客户端传过来的byteBufer 转换为long 8个字节
/**
* 此处无序判断byteBuf 数据数据足够处理
*/
list.add(byteBuf.readLong());
}
}
package io.netty.inboundAndoutboundHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
public class MyClient {
public static void main(String[] args) throws Exception{
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class)
.handler(new MyClientInitializer());
ChannelFuture channelFuture = bootstrap.connect("localhost", 7000);
channelFuture.channel().closeFuture().sync();
}finally {
group.shutdownGracefully();
}
}
}
package io.netty.inboundAndoutboundHandler;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
public class MyClientHandler extends SimpleChannelInboundHandler<Long> {
/**
* 读取服务端的数据
* @param channelHandlerContext
* @param aLong
* @throws Exception
*/
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Long aLong) throws Exception {
System.out.println("MyClientHandler#channelRead0 接收到服务端的请求"+channelHandlerContext.channel().remoteAddress()+"--"+aLong);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("MyClientHandler$channelActive发送数据");
/**
* MyByteToLongDecoder#decode
* MyServerHandler#channelRead0
* /127.0.0.1:49929读取到一个long:123456
*/
ctx.writeAndFlush(123456L);
/**
* MyByteToLongDecoder#decode
* MyServerHandler#channelRead0
* /127.0.0.1:49929读取到一个long:7017280452178371428
* MyByteToLongDecoder#decode
* MyServerHandler#channelRead0
* /127.0.0.1:49929读取到一个long:7017280452178371428
*
* 该处理器的前一个handler是MyLongToByteEnCoder
*
* public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
* ByteBuf buf = null;
*
* try {
* if (this.acceptOutboundMessage(msg)) {
* I cast = msg;
* buf = this.allocateBuffer(ctx, msg, this.preferDirect);
*
* try {
* this.encode(ctx, cast, buf);
* } finally {
* ReferenceCountUtil.release(msg);
* }
*
* if (buf.isReadable()) {
* ctx.write(buf, promise);
* } else {
* buf.release();
* ctx.write(Unpooled.EMPTY_BUFFER, promise);
* }
*
* buf = null;
* } else {
* ctx.write(msg, promise);
* }
* 所以会导致 io.netty.inboundAndoutboundHandler.MyLongToByteEnCoder#encode(io.netty.channel.ChannelHandlerContext, java.lang.Long, io.netty.buffer.ByteBuf)
* 没有被调用
*/
// ctx.writeAndFlush(Unpooled.copiedBuffer("abcdabcdabcdabcd", CharsetUtil.UTF_8));
}
}
package io.netty.inboundAndoutboundHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
public class MyClientInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
//,加入一个出栈handler 需要对其进行编码,
pipeline.addLast(new MyLongToByteEnCoder());
//入栈的解码器
pipeline.addLast(new MyByteToLongDecoder());
// //入栈的解码器
// pipeline.addLast(new MyByteToLongDecoder2());
//然后在加入一个自定义的handler 处理业务逻辑
pipeline.addLast(new MyClientHandler());
}
}
package io.netty.inboundAndoutboundHandler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
/**
* 将LOng转换为byte
*/
public class MyLongToByteEnCoder extends MessageToByteEncoder<Long> {
/**
*
* @param channelHandlerContext
* @param aLong
* @param byteBuf
* @throws Exception
*/
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, Long aLong, ByteBuf byteBuf) throws Exception {
System.out.println("MyLongToByteEnCoder#encode 触发"+aLong);
byteBuf.writeLong(aLong);
}
}
package io.netty.inboundAndoutboundHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class MyServer {
public static void main(String[] args) throws Exception{
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workGroup).channel(NioServerSocketChannel.class).
childHandler(new MyServerInitializer());
ChannelFuture channelFuture = serverBootstrap.bind(7000).sync();
channelFuture.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
}
package io.netty.inboundAndoutboundHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class MyServerHandler extends SimpleChannelInboundHandler<Long> {
/**
*
* @param channelHandlerContext
* @param aLong
* @throws Exception
*/
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Long aLong) throws Exception {
System.out.println("MyServerHandler#channelRead0");
System.out.println(channelHandlerContext.channel().remoteAddress()+"读取到一个long:"+aLong);
//给客户端发送一个long
channelHandlerContext.writeAndFlush(9999L);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println(cause.getMessage());
ctx.channel().close();
}
}
package io.netty.inboundAndoutboundHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
public class MyServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
//入栈的handler针对客户端的数据进行解码
//MyByteToLongDecoder
pipeline.addLast(new MyByteToLongDecoder());
//pipeline.addLast(new MyByteToLongDecoder2());
pipeline.addLast(new MyLongToByteEnCoder());
pipeline.addLast(new MyServerHandler());
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/c46b15dc9a075ed725e6343a36f73aac.png)