Documentation ¶
Index ¶
- Constants
- Variables
- type Packet
- func (p *Packet) BodyValid() bool
- func (p *Packet) Bytes() []byte
- func (p *Packet) CalculateBodyMD5() []byte
- func (p *Packet) CalculateHeaderHash() uint32
- func (p *Packet) ConfirmRequestValid() bool
- func (p *Packet) ConfirmResponseValid() bool
- func (p *Packet) Decrypt(encryptor *encryptor.Encryptor)
- func (p *Packet) Encrypt(encryptor *encryptor.Encryptor, method uint8)
- func (p *Packet) ExpandBody()
- func (p *Packet) Generate()
- func (p *Packet) GenerateBodyMD5()
- func (p *Packet) GenerateHeaderHash()
- func (p *Packet) GetBodyData() []byte
- func (p *Packet) GetBodyDataCopy() []byte
- func (p *Packet) GetBodyMD5() []byte
- func (p *Packet) GetByte(offset int) byte
- func (p *Packet) GetBytes(offset, length int) []byte
- func (p *Packet) GetBytesCopy(offset, length int) []byte
- func (p *Packet) GetDataSize() uint32
- func (p *Packet) GetEncryptor() uint8
- func (p *Packet) GetFlag() uint8
- func (p *Packet) GetHeadHash() uint32
- func (p *Packet) GetPrefix() uint8
- func (p *Packet) GetUint16(offset int) uint16
- func (p *Packet) GetUint24(offset int) uint32
- func (p *Packet) GetUint32(offset int) uint32
- func (p *Packet) GetValue1() uint8
- func (p *Packet) GetValue2() []byte
- func (p *Packet) GetValue2UInt32() uint32
- func (p *Packet) GetVersion() uint8
- func (p *Packet) HeartbeatRequestValid() bool
- func (p *Packet) HeartbeatResponseValid() bool
- func (p *Packet) PushBodyData(data []byte) error
- func (p *Packet) Reserve(n int)
- func (p *Packet) Reset()
- func (p *Packet) ResetBodyLength()
- func (p *Packet) ResetLength()
- func (p *Packet) SetBodyData(data []byte) error
- func (p *Packet) SetBodyMD5(hash []byte)
- func (p *Packet) SetByte(offset int, val byte)
- func (p *Packet) SetBytes(offset, length int, val []byte)
- func (p *Packet) SetDataSize(size uint32)
- func (p *Packet) SetEncryptor(encryptor uint8)
- func (p *Packet) SetFlag(flag uint8)
- func (p *Packet) SetHeadHash(hash uint32)
- func (p *Packet) SetPrefix(prefix uint8)
- func (p *Packet) SetUint16(offset int, val uint16)
- func (p *Packet) SetUint24(offset int, val uint32)
- func (p *Packet) SetUint32(offset int, val uint32)
- func (p *Packet) SetValue1(value uint8)
- func (p *Packet) SetValue2(value []byte)
- func (p *Packet) SetValue2Uint32(value uint32)
- func (p *Packet) SetVersion(version uint8)
- func (p *Packet) Size() int
- func (p *Packet) Verify() error
- func (p *Packet) VerifyBodyMD5() bool
- func (p *Packet) VerifyHeaderHash() bool
Constants ¶
View Source
const ( BodyOffsetStart = 0 BodyOffsetEncryptor = BodyOffsetStart // 数据加密方式 BodyLengthEncryptor = 1 BodyOffsetDataSize = BodyOffsetEncryptor + BodyLengthEncryptor // 数据大小 BodyLengthDataSize = 3 BodyOffsetHashMD5 = BodyOffsetDataSize + BodyLengthDataSize // MD5哈希值 BodyLengthHashMD5 = 16 BodyLength = BodyOffsetHashMD5 + BodyLengthHashMD5 // Body长度 BodyOffsetData = BodyLength // 数据起始位置 BodyLengthDataMax = 64 * 1024 // Body Data 最大长度 (加密前) )
字段偏移
data的加密方式
View Source
const ( /* Heartbeat机制, 用来检查链路是否正常 */ FlagHeartbeatRequest uint8 = 1 << iota // 心跳请求信号, 发送心跳信号并请求心跳响应 FlagHeartbeatResponse // 心跳响应信号, 发送心跳信号 /* Package确认机制, 接收方收到携带此标志的包裹后,需要在一定时间内向发送方发送确认信号 */ FlagConfirmRequest // Package请求确认信号, 接收后需要响应确认信号 FlagConfirmResponse // Package确认信号,发送确认信号 /* Package包含了Body */ FlagBody // 包含Body FlagBodyMD5 // 校验BodyMD5 )
flag标志位
View Source
const ( FlagHeartbeat = FlagHeartbeatRequest | FlagHeartbeatResponse // 心跳 FlagConfirm = FlagConfirmRequest | FlagConfirmResponse // 确认 )
组合flag
View Source
const ( HashOffsetStart = HeadOffsetFlag // Header校验起始偏移 HashOffsetEndNoBody = HeadOffsetEnd // Header校验末尾偏移,不带body HashOffsetEndWithBody = HeadOffsetEnd + BodyOffsetData // Header校验末尾偏移,带body )
View Source
const ( HeadOffsetStart = 0 // Head起始 HeadOffsetPrefix = HeadOffsetStart // 校验魔数/开始标志 HeadLengthPrefix = 1 HeadOffsetVersion = HeadOffsetPrefix + HeadLengthPrefix // 版本号 HeadLengthVersion = 1 HeadOffsetHash = HeadOffsetVersion + HeadLengthVersion // Header校验信息 HeadLengthHash = 4 HeadOffsetFlag = HeadOffsetHash + HeadLengthHash // 标志位 HeadLengthFlag = 1 HeadOffsetValue1 = HeadOffsetFlag + HeadLengthFlag // 第一个数据字段 HeadLengthValue1 = 1 HeadOffsetValue2 = HeadOffsetValue1 + HeadLengthValue1 // 第二个数据字段 HeadLengthValue2 = 4 HeadOffsetEnd = HeadOffsetValue2 + HeadLengthValue2 // Header末尾偏移 HeadLength = HeadOffsetEnd // Header的长度 )
字段偏移
View Source
const BufferMaxSize = HeadLength + BodyLength + BodyLengthDataMax + 64
View Source
const FlagEmpty uint8 = 0
View Source
const PREFIX uint8 = 0x95 // 1001 0101
PREFIX package的起始标志
View Source
const VERSION uint8 = 0x03
VERSION package的版本
Variables ¶
View Source
var ( ErrWriteBodyDataFailed = errors.New("write body data failed") ErrPacketIncomplete = errors.New("package incomplete") ErrVerifyHeaderHashFailed = errors.New("verify header hash failed") ErrVerifyBodyMD5Failed = errors.New("verify body md5 failed") )
View Source
var Pool = newPool()
Functions ¶
This section is empty.
Types ¶
type Packet ¶
type Packet struct {
// contains filtered or unexported fields
}
func (*Packet) Decrypt ¶
Decrypt 解密Data(可能改变BodySize字段)
func (*Packet) Encrypt ¶
Encrypt 加密Data(可能改变BodySize字段)
func (*Packet) GenerateHeaderHash ¶
func (p *Packet) GenerateHeaderHash()
GenerateHeaderHash 生成Header的CRC32
func (*Packet) GetBytes ¶
GetBytes 获取从offset开始的length个byte
func (*Packet) GetBytesCopy ¶
GetBytesCopy 获取从offset开始的length个byte的拷贝
func (*Packet) GetUint16 ¶
GetUint16 从offset位置获取2个byte的uint16
func (*Packet) GetUint24 ¶
GetUint24 从offset位置获取3个byte的uint16
func (*Packet) GetUint32 ¶
GetUint32 从offset位置获取4个byte的uint32
func (*Packet) ResetBodyLength ¶
func (p *Packet) ResetBodyLength()
ResetBodyLength 重置缓冲区为Head+Body的长度
func (*Packet) SetBytes ¶
SetBytes 将val存放在offset开始的length个byte中
func (*Packet) SetUint16 ¶
SetUint16 将val放在offset起始的2个byte中
func (*Packet) SetUint24 ¶
SetUint24 将val放在offset起始的3个byte中
func (*Packet) SetUint32 ¶
SetUint32 将val放在offset起始的4个byte中
Click to show internal directories.
Click to hide internal directories.