netty(一):NIO——处理消息边界

处理消息边界

为什么要处理边界

因为会存在半包和粘包的问题

1.客户端和服务端约定一个固定长度

  • 优点:简单

  • 缺点:可能造成浪费

2.客户端与服务端约定一个固定分割符

*缺点 效率低

3.先发送长度,再发送数据

TLV格式: type类型,length长度,Value数据,类型和长度已知的情况下,就可以方便获取消息大小

http1.1是TLV格式

http2.0是LTV格式

4.自动扩容解决消息边界问题

第一次read事件未能读完全部的输入,那么会产生第二个读事件,那么在第一次读的时候进行扩容,

并复制之前的内容至新的buffer中,

在第二个读事件触发以后使用扩容后的buffer,读取剩余的数据

buffer应当和各自的channel绑定,如何绑定,需要用到附件attachment,

attachment需要在注册时放到selectionKey中。

复制代码
// 绑定附件
SelectionKey scKey = channel.register(selector,0,byteBuffer);

// 获取附件
scKey.attachment();

// 指定新的附件(覆盖附件)
scKey.attach(bytebuffer);

示例代码:

复制代码
package com.ysf;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;

public class BorderSelectorServer {

    /**
     * 读取到 '\n'时打印
     * @param byteBuffer 缓冲字节
     */
    public static void handle(ByteBuffer byteBuffer) {
        byteBuffer.flip();
        for (int i = 0; i < byteBuffer.limit(); i++) {
            if (byteBuffer.get(i) == '\n') {
                int length = i + 1 - byteBuffer.position();
                ByteBuffer allocate = ByteBuffer.allocate(length);
                for (int j=0;j<length;j++){
                    allocate.put(byteBuffer.get());
                }
                allocate.flip();
                System.out.println(Charset.defaultCharset().decode(allocate));
            }
        }
        byteBuffer.compact();
    }

    public static void main(String[] args) throws IOException {
        // 声明一个选择器
        Selector selector = Selector.open();

        // 声明一个server
        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.bind(new InetSocketAddress(11027));
        ssc.configureBlocking(false);

        // 注册这个server到selector
        SelectionKey sscKey = ssc.register(selector, 0, null);
        // 添加sscKey关心的事件,因为是serverChannel,所以应当关心accept事件
        sscKey.interestOps(SelectionKey.OP_ACCEPT);
        while (true) {
            selector.select();
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                iterator.remove();

                if (key.isAcceptable()) {
                    ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
                    SocketChannel socketChannel = serverChannel.accept();
                    socketChannel.configureBlocking(false);
                    // 声明一个buffer缓冲区和socketChannel绑定
                    ByteBuffer byteBuffer = ByteBuffer.allocate(16);
                    SelectionKey scKey = socketChannel.register(selector, 0, byteBuffer);
                    scKey.interestOps(SelectionKey.OP_READ);
                } else if (key.isReadable()) {
                    // 当客户端异常断开链接是需要处理IOException
                    try {
                        SocketChannel channel = (SocketChannel) key.channel();
                        ByteBuffer byteBuffer = (ByteBuffer) key.attachment();
                        int read = channel.read(byteBuffer);
                        if (read == -1) {
                            // 客户端close()了
                            key.cancel();
                        }else{
                            // 调用处理逻辑
                            handle(byteBuffer);
                            if (byteBuffer.position() == byteBuffer.limit()){
                                // buffer满了,需要扩容
                                ByteBuffer bufferEx = ByteBuffer.allocate(byteBuffer.capacity() * 2);
                                byteBuffer.flip();
                                bufferEx.put(byteBuffer);
                                key.attach(bufferEx);
                            }
                        }

                    }catch (IOException e){
//                        e.printStackTrace();
                        key.cancel();
                    }
                }
            }
        }
    }
}
相关推荐
2301_780789661 小时前
跨平台适配!SDK 游戏盾兼容全终端游戏场景
java·网络·ddos
学长毕设2 小时前
基于springboot的食品仓库管理系统
java·spring boot·后端
步行cgn3 小时前
FormHttpMessageConverter 详解:Spring MVC 中表单数据的读写转换器
java·spring·mvc
龙亘川3 小时前
构建城市公共安全数字底座:亘川智城智慧安防系统全维度解读
java·大数据·开发语言·人工智能·信息可视化
金銀銅鐵3 小时前
[Java] 用 CLI 展示 EnumMap 增删键值对时的内部变化
java·源码·ai编程
秋天的怀念_David3 小时前
第二十二夜 · 数字迷雾
java·程序人生·算法
Raas1003 小时前
AI网关部署方案?从部署到上线全流程
java·开发语言·人工智能·gateway·ai网关·企业级ai网关·企业级ai产品
niucloud-admin4 小时前
NIUCLOUD PHP SAAS上门家政使用文档:城市管理
java·前端·php
霸道流氓气质4 小时前
智能客服组件:分层意图识别、RAG Advisor 与人工转接状态机的 Vue3 + Spring AI 生产级实战
java·人工智能·spring
captain3765 小时前
MyBatis操作数据库(入门2)
java·数据库·spring boot·后端·spring·mybatis