You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package unpacker
// Unpacker 拆包器接口
type Unpacker interface {
// Unpack 拆包
// data: 原始数据
// 返回: 完整的消息列表, 剩余数据, 本次从输入data中消耗的字节数, 错误
// consumed: 本次从输入data中消耗的字节数用于准确计算从gnet buffer中需要discard的数据量
Unpack(data []byte) ([][]byte, []byte, int, error)
// Pack 打包
// data: 要打包的数据
// 返回: 打包后的数据, 错误
Pack(data []byte) ([]byte, error)
}
// 默认最大buffer大小10MB
const DefaultMaxBufferSize = 10 * 1024 * 1024
// FixedLengthUnpacker 固定长度拆包器
type FixedLengthUnpacker struct {
Length int
MaxBufferSize int // 最大buffer大小0表示使用默认值
}
// LengthFieldUnpacker 长度字段拆包器
type LengthFieldUnpacker struct {
LengthFieldOffset int // 长度字段偏移
LengthFieldLength int // 长度字段长度1, 2, 4, 8
LengthAdjustment int // 长度调整(长度字段值 + 调整值 = 实际长度)
InitialBytesToStrip int // 需要跳过的初始字节数
MaxBufferSize int // 最大buffer大小0表示使用默认值
}
// DelimiterUnpacker 分隔符拆包器
type DelimiterUnpacker struct {
Delimiter []byte
MaxBufferSize int // 最大buffer大小0表示使用默认值
}
// FrameHeaderUnpacker 帧头拆包器
type FrameHeaderUnpacker struct {
HeaderLength int // 帧头长度
GetLength func(header []byte) int // 从帧头获取消息长度
MaxBufferSize int // 最大buffer大小0表示使用默认值
}