知识卡片

普通读书笔记卡 · 1669.a.1.a.1.a · 生成模型:GPT-6

长度字段界定消息

理解等级:中级

TCP 只交付连续字节流,应用用固定头部与长度字段识别完整消息。先确定数字宽度、端序、可接受的最大长度、UTF-8 错误处理和截断语义,才能在短读时保留进度并继续解析。关联 [[WouldBlock保留进度]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 10. Building an Async Server with No Dependencies 数据布局与消息边界 · 查看原书依据

      // Initialize buffers for the fields, using arrays of the appropriate size let mut field1_bytes = [0u8; 4]; let mut field2_bytes = [0u8; 2]; // Read the first field (4 bytes) from the cursor into the buffer. // Do the same for second field. cursor.read_exact(&mut field1_bytes)?; cursor.read_exact(&mut field2_bytes)?; // Convert the byte arrays into the appropriate data types (u32 and u16) let field1 = u32::from_ne_bytes(field1_bytes);

    参考来源

    - 位置:《Async Rust Unleashing the Power of Fearless Concurrency (Maxwell Flitton, Caroline Morton)》第10章《Chapter 10. Building an Async Server with No Dependencies》,数据布局与消息边界(来源项目 book-source,extracted/Async Rust Unleashing the Power of Fearless Concurrency (Maxwell Flitton, Caroline Morton)/_epub-src/OEBPS/ch10.html#id63) - 结论依据:原书按固定宽度读整数,随后按长度读取 UTF-8 字节。 - 原始内容:// Initialize buffers for the fields, using arrays of the appropriate size let mut field1_bytes = [0u8; 4]; let mut field2_bytes = [0u8; 2]; // Read the first field (4 bytes) from the cursor into the buffer. // Do the same for second field. cursor.read_exact(&mut field1_bytes)?; cursor.read_exact(&mut field2_bytes)?; // Convert the byte arrays into the appropriate data types (u32 and u16) let field1 = u32::from_ne_bytes(field1_bytes);