Netty自定义协议半包编解码

1.自定义协议客户端与服务器收发逻辑

Netty中自带了多种编解码器,在项目中更常用的是自定义协议进行通信,此时需要自行处理

半包问题,通过继承ByteToMessageDecoder可以方便的解决这个问题。

服务器代码:

Server main:

public class SocketServer {
public static void main(String[] args) throws InterruptedException {
    EventLoopGroup parentGroup = new NioEventLoopGroup();
    EventLoopGroup childGroup = new NioEventLoopGroup();

    try {
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(parentGroup, childGroup)
                .channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>(){
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new SelfDefineEncodeHandler());
                        pipeline.addLast(new BusinessServerHandler());
                    }
                });

        ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
        channelFuture.channel().closeFuture().sync();
    }
    finally {
        parentGroup.shutdownGracefully();
        childGroup.shutdownGracefully();
    }
}
}

自定义解码器:SelfDefineEncodeHandler

/**
 * 定长消息数据格式
 * 
 * | length |  msg  | 头部length用4字节存储,存储的长度为消息体msg的总长度
 * 
 * 
 * */
public class SelfDefineEncodeHandler extends ByteToMessageDecoder {
    @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf bufferIn, List<Object> out) throws Exception {
    if (bufferIn.readableBytes() < 4) {
        return;
    }

    //返回当前buff中readerIndex索引
    int beginIndex = bufferIn.readerIndex();
    //在当前readerIndex基础上读取4字节并返回,同时增加readIndex
    int length = bufferIn.readInt();

    /**
     * 1.当可读数据小于length,说明包还没有接收完全
     * 2.开始可读为beginindex,此时读完readInt后需要重置readerindex
     * 3.重置readerindex后继续等待下一个读事件到来
     * */
    if (bufferIn.readableBytes() < length) {
        //重置当前的readerindex为beginindex
        bufferIn.readerIndex(beginIndex);
        return;
    }

    //4字节存放length,这里整个消息长度为4+length,跳过当前消息,增大bufferIn的readindex,bufferIn中数组可复用
    bufferIn.readerIndex(beginIndex + 4 + length);

    //Returns a slice of this buffer's sub-region.
    //取出当前的整条消息并存入otherByteBufRef中
    ByteBuf otherByteBufRef = bufferIn.slice(beginIndex, 4 + length);


    /**
     * 1.每一个bytebuf都有一个计数器,每次调用计数器减1,当计数器为0时则不可用。
     * 2.当前bytebuf中数据包含多条消息,本条信息会通过out返回被继续封装成一个新的bytebuf返回下一个hander处理
     * 3.retain方法是将当前的bytebuf计数器加1
     * */
    otherByteBufRef.retain();

    out.add(otherByteBufRef);
}

}

消息处理handler

public class BusinessServerHandler extends ChannelInboundHandlerAdapter {

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    /**
     * 1.读数据,这里收到的是一个完整的消息数据,在decoder的out传递到当前逻辑
     * 2.对消息进一步的解码
     * */
    ByteBuf buf = (ByteBuf)msg;
    int length = buf.readInt();
    assert length == (8);

    byte[] head = new byte[4];
    buf.readBytes(head);
    String headString = new String(head);
    assert  "head".equals(headString);

    byte[] body = new byte[4];
    buf.readBytes(body);
    String bodyString = new String(body);
    assert  "body".equals(bodyString);
}
}

客户端逻辑:SocketClient

public class SocketClient {
public static void main(String[] args) throws InterruptedException {
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();

    try {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(eventLoopGroup)
                .channel(NioSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .handler(new ChannelInitializer<SocketChannel>(){
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new SocketClientHandler());
        }});

        ChannelFuture channelFuture = bootstrap.connect("localhost", 8899).sync();
        channelFuture.channel().closeFuture().sync();
    }
    finally {
        eventLoopGroup.shutdownGracefully();
    }
}
}

客户端消息发送:

public class SocketClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    }

    /**
     * 1.写数据的逻辑,先写入消息的总长度
     * 2.分别写入消息体的内容
     * 3.以bytebuf的方式发送数据
     * */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
        ByteBuf buffer = allocator.buffer(20);
        buffer.writeInt(8);
        buffer.writeBytes("head".getBytes());
        buffer.writeBytes("body".getBytes());

        ctx.writeAndFlush(buffer);
    }
}

2.使用protobuf可以使用netty自带的编解码器:

(1)启动server

public class Server {

// 实例一个连接容器保存连接
public static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

// 再搞个map保存与用户的映射关系
public static ConcurrentMap<Integer, ChannelId> userSocketMap = new ConcurrentHashMap<Integer, ChannelId>();

private int port;

public ChatServer(int port) {
    this.port = port;
}

public void run() throws Exception {

    EventLoopGroup bossGroup = new NioEventLoopGroup();// boss线程池
    EventLoopGroup workerGroup = new NioEventLoopGroup();// worker线程池
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)// 使用TCP
                .childHandler(new ChatServerInitializer())// 初始化配置的处理器
                .option(ChannelOption.SO_BACKLOG, 128)// BACKLOG用于构造服务端套接字ServerSocket对象,标识当服务器请求处理线程全满时,用于临时存放已完成三次握手的请求的队列的最大长度。如果未设置或所设置的值小于1,Java将使用默认值50。
                .childOption(ChannelOption.SO_KEEPALIVE, true);// 是否启用心跳保活机制。在双方TCP套接字建立连接后(即都进入ESTABLISHED状态)并且在两个小时左右上层没有任何数据传输的情况下,这套机制才会被激活。

        System.out.println("[ChatServer 启动了]");

        // 绑定端口,开始接收进来的连接
        ChannelFuture f = b.bind(port).sync();

        // 等待服务器 socket 关闭 。
        // 这不会发生,可以优雅地关闭服务器。
        f.channel().closeFuture().sync();

    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();

        System.out.println("[ChatServer 关闭了]");
    }
}
}

(2)解码器

public class ChatServerInitializer extends ChannelInitializer<SocketChannel> {

Timer timer;

public ChatServerInitializer() {
    timer = new HashedWheelTimer();
}

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    // ----Protobuf处理器,这里的配置是关键----
    pipeline.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());// 用于decode前解决半包和粘包问题(利用包头中的包含数组长度来识别半包粘包)
    //配置Protobuf解码处理器,消息接收到了就会自动解码,ProtobufDecoder是netty自带的,Message是自己定义的Protobuf类
    pipeline.addLast("protobufDecoder",new ProtobufDecoder(Message.getDefaultInstance()));
    // 用于在序列化的字节数组前加上一个简单的包头,只包含序列化的字节长度。
    pipeline.addLast("frameEncoder",new ProtobufVarint32LengthFieldPrepender());
    //配置Protobuf编码器,发送的消息会先经过编码
    pipeline.addLast("protobufEncoder", new ProtobufEncoder());
    // ----Protobuf处理器END----

    pipeline.addLast("handler", new ChatServerHandler());//自己定义的消息处理器,接收消息会在这个类处理
    pipeline.addLast("ackHandler", new AckServerHandler());//处理ACK
    pipeline.addLast("timeout", new IdleStateHandler(100, 0, 0,TimeUnit.SECONDS));// //此两项为添加心跳机制,60秒查看一次在线的客户端channel是否空闲
    pipeline.addLast(new HeartBeatServerHandler());// 心跳处理handler

}

// pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
// 2 * 1024, Delimiters.lineDelimiter()));
// pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
// pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
}

(3)消息处理handler

public class ChatServerHandler extends SimpleChannelInboundHandler<Message> {

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    Channel incoming = ctx.channel();
    System.out.println("[SERVER] - " + incoming.remoteAddress() + " 连接过来\n");       
}

@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    Channel incoming = ctx.channel();
    ChatServer.channels.remove(incoming); 
    System.out.println("[SERVER] - " + incoming.remoteAddress() + " 离开\n"); 
    // A closed Channel is automatically removed from ChannelGroup,
    // so there is no need to do "channels.remove(ctx.channel());"
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, Message msg)
        throws Exception {      
    //消息会在这个方法接收到,msg就是经过解码器解码后得到的消息,框架自动帮你做好了粘包拆包和解码的工作        
    //处理消息逻辑    
    ctx.fireChannelRead(msg);//把消息交给下一个处理器      
}

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception { // (5)
    Channel incoming = ctx.channel();
    System.out.println("ChatClient:" + incoming.remoteAddress() + "上线");
}

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { // (6)
    Channel incoming = ctx.channel();
    System.out.println("ChatClient:" + incoming.remoteAddress() + "掉线"); 
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (7)
    Channel incoming = ctx.channel();
    // 当出现异常就关闭连接
    System.out.println("ChatClient:" + incoming.remoteAddress()
            + "异常,已被服务器关闭");
    cause.printStackTrace();        
    ctx.close();
}
}

(4)心跳handler

public class HeartBeatServerHandler extends ChannelInboundHandlerAdapter {

private int loss_connect_time = 0;

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
        throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            loss_connect_time++;
            System.out.println("[60 秒没有接收到客户端" + ctx.channel().id()
                    + "的信息了]");
            if (loss_connect_time > 2) {
                // 超过20秒没有心跳就关闭这个连接
                System.out.println("[关闭这个不活跃的channel:" + ctx.channel().id()
                        + "]");
                ctx.channel().close();
            }
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值