feat(frame): go-hua/common 帧协议独立成包并修复两处缺陷

- SetData 超长时 fmt.Errorf 结果未 return,超 65535 字节静默放行 → 改为返回错误
- ReadFrame 原实现 conn.Read 直读帧头(短读错位)且一次性 bufio.Reader 会丢弃
  缓冲中的后续帧字节 → 统一改 io.ReadFull 精确读取,新增连续两帧回归测试
- 补齐单元测试(往返/流水线/超长/Marshal/RawHeader)、examples/frame 例程与 README
This commit is contained in:
2026-09-20 12:20:51 +08:00
parent 1c9c717579
commit 9d7a512e18
4 changed files with 374 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
// frame 包示例:应用层帧协议(8 字节头 + 数据)在 net.Pipe 上的读写往返
package main
import (
"fmt"
"net"
"git.zeroonesoft.cn/golib/zogo/frame"
)
func main() {
c1, c2 := net.Pipe()
defer c1.Close()
defer c2.Close()
// 组帧:版本 + 命令字 + 流 ID + 数据
f := frame.NewFrame(frame.FrameVersion, 0x10, 42)
if err := f.Marshal(map[string]any{"msg": "hello"}); err != nil {
fmt.Println("Marshal 失败:", err)
return
}
fmt.Println("帧信息:", f)
// 写帧 / 读帧
go func() {
if err := frame.WriteFrame(c1, f); err != nil {
fmt.Println("WriteFrame 失败:", err)
}
}()
got, err := frame.ReadFrame(c2)
if err != nil {
fmt.Println("ReadFrame 失败:", err)
return
}
var data map[string]any
if err := got.Unmarshal(&data); err != nil {
fmt.Println("Unmarshal 失败:", err)
return
}
fmt.Printf("收到帧: %s 数据: %v\n", got.String(), data)
}