codec

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2022 License: BSD-3-Clause Imports: 10 Imported by: 0

README

codec的设计

概述

codec是用于处理网络流的数据包编解码

业务层的消息用Packet表示,codec把网络流解码为一个个packet投递给业务层,并且把业务层传递过来的packet编码到网络流发送给对端

设计目标
  • 稳定可扩展,尽量满足常见需求(压缩、加密、序号),并且提供机制方便外层代码修改;
  • 实现不宜过于复杂,尽量简单易于在不同的语言实现
  • Client-Server和Server-Server的协议分开
Client-Server
  • 长度用2个字节表示,也就是限制到64K内;
  • 会话内的每个packet可以由一个序列号标记;
  • 协议检查head和body的校验码;
  • 消息派发可以使用ID,也可以使用字符串;
Server-Server
  • 长度用4字节表示,并限制在16M以内;
  • 会话内的每个packet可以由一个序列号标记;
  • 协议检查head和body的校验码;
  • 消息派发可以使用ID,也可以使用字符串;
  • 需要考虑到路由转发和广播多播的场景,消息协议能够附带其它路由节点信息;

Documentation

Index

Constants

View Source
const (
	V1EncoderVersion    = 1
	V1HeaderSize        = 18     // 包头大小
	V1MaxPayloadBytes   = 0xFFFF // 64K
	V1CompressThreshold = 0x1000 // 压缩阈值(4k)
)
View Source
const (
	V2EncoderVersion    = 1
	V2HeaderSize        = 24        // 包头大小
	V2MaxPayloadBytes   = 0x4000000 // 64M
	V2MaxReferCount     = 1000      //
	V2CompressThreshold = 0x2000    // 压缩阈值(8K)
)

Variables

This section is empty.

Functions

func ReadLenData

func ReadLenData(r io.Reader) ([]byte, error)

读取2字节开头的数据

func Register added in v0.1.12

func Register(v Encoder)

func WriteLenData

func WriteLenData(w io.Writer, data []byte) (int, error)

写入2字节开头的数据

Types

type Encoder added in v0.1.12

type Encoder interface {
	Name() string
	Version() int

	// 把`pkt`编码到`w`,内部除了flag不应该修改pkt的其它字段
	WritePacket(w io.Writer, encrypt cipher.BlockCryptor, pkt fatchoy.IPacket) (int, error)

	// 按协议格式读取head和body
	ReadHeadBody(r io.Reader) ([]byte, []byte, error)

	// 根据head和body解码消息到`pkt`
	UnmarshalPacket(header, body []byte, decrypt cipher.BlockCryptor, pkt fatchoy.IPacket) error

	// 从`r`里读取消息到`pkt`
	ReadPacket(r io.Reader, decrypt cipher.BlockCryptor, pkt fatchoy.IPacket) error
}

消息编/解码接口

func GetEncoder added in v0.1.12

func GetEncoder(name string) Encoder

func NewV1Encoder added in v0.1.12

func NewV1Encoder(threshold int) Encoder

func NewV2Encoder added in v0.1.12

func NewV2Encoder(threshold int) Encoder

type V1Header

type V1Header []byte

func (V1Header) CalcChecksum

func (h V1Header) CalcChecksum(payload []byte) uint32

校验码包含head和body

func (V1Header) Checksum

func (h V1Header) Checksum() uint32

CRC校验码

func (V1Header) Cmd added in v0.1.32

func (h V1Header) Cmd() int32

func (V1Header) Flag

func (h V1Header) Flag() uint16

标记位, V1的协议只保留低8位的flag

func (V1Header) Len

func (h V1Header) Len() uint16

长度包含头部和body

func (V1Header) MD5Sum added in v0.1.8

func (h V1Header) MD5Sum() string

func (V1Header) MsgID added in v0.1.29

func (h V1Header) MsgID() uint32

func (V1Header) Pack

func (h V1Header) Pack(pkt fatchoy.IPacket, size uint16)

func (V1Header) Seq

func (h V1Header) Seq() uint16

session内的唯一序号

func (V1Header) SetChecksum

func (h V1Header) SetChecksum(payload []byte)

type V2Header

type V2Header []byte

func (V2Header) CalcChecksum

func (h V2Header) CalcChecksum(payload []byte) uint32

校验码包含head和body

func (V2Header) Checksum

func (h V2Header) Checksum() uint32

CRC校验码

func (V2Header) Cmd added in v0.1.32

func (h V2Header) Cmd() int32

func (V2Header) Flag

func (h V2Header) Flag() uint16

标记位

func (V2Header) Len

func (h V2Header) Len() uint32

长度包含头部和body

func (V2Header) MD5Sum

func (h V2Header) MD5Sum() string

func (V2Header) MsgID added in v0.1.29

func (h V2Header) MsgID() uint32

func (V2Header) Node

func (h V2Header) Node() fatchoy.NodeID

目标节点

func (V2Header) Pack

func (h V2Header) Pack(pkt fatchoy.IPacket, size uint32)

func (V2Header) Seq

func (h V2Header) Seq() uint16

session内的唯一序号, 当广播的时候,session用来表达refcount

func (V2Header) SetChecksum

func (h V2Header) SetChecksum(payload []byte)

Jump to

Keyboard shortcuts

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