packet

package
v0.3.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2024 License: GPL-2.0 Imports: 11 Imported by: 0

Documentation

Index

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 最大长度 (加密前)
)

字段偏移

View Source
const (
	EncryptorNone uint8 = iota // 不加密
	EncryptorXor               // 异或加密
	EncryptorAes               // AES加密
)

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 NewEmpty

func NewEmpty() *Packet

func ParseBuffer

func ParseBuffer(buffer *bytes.Buffer) (*Packet, error)

func (*Packet) BodyValid

func (p *Packet) BodyValid() bool

func (*Packet) Bytes

func (p *Packet) Bytes() []byte

Bytes 获取packet数据

func (*Packet) CalculateBodyMD5

func (p *Packet) CalculateBodyMD5() []byte

func (*Packet) CalculateHeaderHash

func (p *Packet) CalculateHeaderHash() uint32

func (*Packet) ConfirmRequestValid

func (p *Packet) ConfirmRequestValid() bool

func (*Packet) ConfirmResponseValid

func (p *Packet) ConfirmResponseValid() bool

func (*Packet) Decrypt

func (p *Packet) Decrypt(encryptor *encryptor.Encryptor)

Decrypt 解密Data(可能改变BodySize字段)

func (*Packet) Encrypt

func (p *Packet) Encrypt(encryptor *encryptor.Encryptor, method uint8)

Encrypt 加密Data(可能改变BodySize字段)

func (*Packet) ExpandBody

func (p *Packet) ExpandBody()

ExpandBody 为Body预留长度

func (*Packet) Generate

func (p *Packet) Generate()

func (*Packet) GenerateBodyMD5

func (p *Packet) GenerateBodyMD5()

GenerateBodyMD5 生成Body(加密后)的MD5

func (*Packet) GenerateHeaderHash

func (p *Packet) GenerateHeaderHash()

GenerateHeaderHash 生成Header的CRC32

func (*Packet) GetBodyData

func (p *Packet) GetBodyData() []byte

func (*Packet) GetBodyDataCopy

func (p *Packet) GetBodyDataCopy() []byte

func (*Packet) GetBodyMD5

func (p *Packet) GetBodyMD5() []byte

func (*Packet) GetByte

func (p *Packet) GetByte(offset int) byte

GetByte 获取offset位置的byte

func (*Packet) GetBytes

func (p *Packet) GetBytes(offset, length int) []byte

GetBytes 获取从offset开始的length个byte

func (*Packet) GetBytesCopy

func (p *Packet) GetBytesCopy(offset, length int) []byte

GetBytesCopy 获取从offset开始的length个byte的拷贝

func (*Packet) GetDataSize

func (p *Packet) GetDataSize() uint32

func (*Packet) GetEncryptor

func (p *Packet) GetEncryptor() uint8

func (*Packet) GetFlag

func (p *Packet) GetFlag() uint8

func (*Packet) GetHeadHash

func (p *Packet) GetHeadHash() uint32

func (*Packet) GetPrefix

func (p *Packet) GetPrefix() uint8

func (*Packet) GetUint16

func (p *Packet) GetUint16(offset int) uint16

GetUint16 从offset位置获取2个byte的uint16

func (*Packet) GetUint24

func (p *Packet) GetUint24(offset int) uint32

GetUint24 从offset位置获取3个byte的uint16

func (*Packet) GetUint32

func (p *Packet) GetUint32(offset int) uint32

GetUint32 从offset位置获取4个byte的uint32

func (*Packet) GetValue1

func (p *Packet) GetValue1() uint8

func (*Packet) GetValue2

func (p *Packet) GetValue2() []byte

func (*Packet) GetValue2UInt32

func (p *Packet) GetValue2UInt32() uint32

func (*Packet) GetVersion

func (p *Packet) GetVersion() uint8

func (*Packet) HeartbeatRequestValid

func (p *Packet) HeartbeatRequestValid() bool

func (*Packet) HeartbeatResponseValid

func (p *Packet) HeartbeatResponseValid() bool

func (*Packet) PushBodyData

func (p *Packet) PushBodyData(data []byte) error

func (*Packet) Reserve

func (p *Packet) Reserve(n int)

Reserve 增加n个byte的长度

func (*Packet) Reset

func (p *Packet) Reset()

Reset 重置Packet

func (*Packet) ResetBodyLength

func (p *Packet) ResetBodyLength()

ResetBodyLength 重置缓冲区为Head+Body的长度

func (*Packet) ResetLength

func (p *Packet) ResetLength()

ResetLength 重置缓冲区为Head的长度

func (*Packet) SetBodyData

func (p *Packet) SetBodyData(data []byte) error

func (*Packet) SetBodyMD5

func (p *Packet) SetBodyMD5(hash []byte)

func (*Packet) SetByte

func (p *Packet) SetByte(offset int, val byte)

SetByte 设置offset位置的byte

func (*Packet) SetBytes

func (p *Packet) SetBytes(offset, length int, val []byte)

SetBytes 将val存放在offset开始的length个byte中

func (*Packet) SetDataSize

func (p *Packet) SetDataSize(size uint32)

func (*Packet) SetEncryptor

func (p *Packet) SetEncryptor(encryptor uint8)

func (*Packet) SetFlag

func (p *Packet) SetFlag(flag uint8)

func (*Packet) SetHeadHash

func (p *Packet) SetHeadHash(hash uint32)

func (*Packet) SetPrefix

func (p *Packet) SetPrefix(prefix uint8)

func (*Packet) SetUint16

func (p *Packet) SetUint16(offset int, val uint16)

SetUint16 将val放在offset起始的2个byte中

func (*Packet) SetUint24

func (p *Packet) SetUint24(offset int, val uint32)

SetUint24 将val放在offset起始的3个byte中

func (*Packet) SetUint32

func (p *Packet) SetUint32(offset int, val uint32)

SetUint32 将val放在offset起始的4个byte中

func (*Packet) SetValue1

func (p *Packet) SetValue1(value uint8)

func (*Packet) SetValue2

func (p *Packet) SetValue2(value []byte)

func (*Packet) SetValue2Uint32

func (p *Packet) SetValue2Uint32(value uint32)

func (*Packet) SetVersion

func (p *Packet) SetVersion(version uint8)

func (*Packet) Size

func (p *Packet) Size() int

Size 获取packet大小

func (*Packet) Verify

func (p *Packet) Verify() error

Verify 验证Packet是否完整

func (*Packet) VerifyBodyMD5

func (p *Packet) VerifyBodyMD5() bool

func (*Packet) VerifyHeaderHash

func (p *Packet) VerifyHeaderHash() bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL