message

package
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

View Source
const (
	MagicNumber uint8 = 0x45
	// Call 表示这是一条调用的消息
	Call uint8 = 0x10
	// Return 表示这是一条调用返回消息
	Return uint8 = 0x18
	// ContextCancel 用户服务端接收的context.Context的取消API
	ContextCancel uint8 = 0x24
	// Ping Ping消息
	Ping uint8 = 0x33
	// Pong Pong消息
	Pong uint8 = 0x35

	// BaseLen 的基本长度
	BaseLen = _ScopeLength + 4 + 8
	// DefaultPacker TODO: 将Encoder改为Packer
	DefaultPacker string = "text" // encoding == text
	DefaultCodec  string = "json" // codec == text

	ErrorCode    string = "code"
	ErrorMessage string = "message"
	ErrorMore    string = "bin"
	ContextId    string = "context-id"
	CodecScheme  string = "codec"
	// PackerScheme TODO: 将Encoder改为Packer
	PackerScheme string = "packer"
)

Variables

This section is empty.

Functions

func MarshaToMux

func MarshaToMux(msg *Message, payloads *container.Slice[byte]) error

MarshaToMux 此API只会序列化Mux功能需要的数据

func Marshal

func Marshal(msg *Message, p *container.Slice[byte]) error

Marshal 根据Msg Header编码出对应的字节Slice *[]byte是为了提供更好的内存复用语义

func ResetMsg

func ResetMsg(msg *Message, resetOther, freeMetaData, usePayload bool, useSize int)

ResetMsg 指定策略的复用,对内存重用更加友好 resetOther指示是否释放|Scope|NameLayout|InstanceName|MethodName|MsgId|Timestamp freeMetaData指示是否要释放存放元数据对应的map[string]sting usePayload指示是否要复用载荷数据 useSize指示复用的slice类型长度的上限,即使指定了usePayload payload数据超过这个长度还是会被释放

func Unmarshal

func Unmarshal(p container.Slice[byte], msg *Message) error

Unmarshal 从字节Slice中解码出Message,并返回载荷数据的起始地址

func UnmarshalFromMux

func UnmarshalFromMux(data container.Slice[byte], msg *Message) error

UnmarshalFromMux 此API之后反序列化Mux功能所需要的数据

Types

type Getter

type Getter interface {
	First() uint8
	BaseLength() int
	Length() uint32
	GetAndSetLength() uint32
	GetMsgType() uint8
	GetServiceName() string
	GetMsgId() uint64
	Payloads() container2.Slice[byte]
	PayloadsIterator() *container2.Iterator[[]byte]
	MinMux() int
}

type GetterSetter

type GetterSetter interface {
	Getter
	Setter
}

type Message

type Message struct {

	// NOTE:
	//	有效载荷和元数据的范围
	//	在Mux模式中MetaData及其Desc必须能在一个MuxBlock下被装下,否则将会丢失数据
	// 元数据的布局
	//	NMetaData(1 Byte)|Key-Size(4 Byte)|Value-Size(4 Byte)|Key|Size
	// Example :
	//	"hello":"world","world:hello"
	// OutPut:
	//	0x00000002|0x00000005|0x00000005|hello|world|0x00000005|0x00000005|world|hello
	MetaData *container2.SliceMap[string, string]
	// contains filtered or unexported fields
}

Message 是对一次RPC调用传递的数据的描述 封装的方法均不是线程安全的 DecodeHeader会修改一些内部值,调用时需要注意顺序 为了使用一致性的API,在访问内部一些简单的属性时,请使用Getxx方法 在设置一些值的过程中可能需要调整其它值,所以请使用Setxx方法

func New

func New() *Message

func (*Message) AppendPayloads

func (m *Message) AppendPayloads(p []byte)

func (*Message) BaseLength

func (m *Message) BaseLength() int

BaseLength 获取基本数据的长度防止输入过短的数据导致panic

func (*Message) First

func (m *Message) First() uint8

func (*Message) GetAndSetLength

func (m *Message) GetAndSetLength() uint32

func (*Message) GetMsgId

func (m *Message) GetMsgId() uint64

func (*Message) GetMsgType

func (m *Message) GetMsgType() uint8

func (*Message) GetServiceName

func (m *Message) GetServiceName() string

func (*Message) Length

func (m *Message) Length() uint32

Length 根据结构计算序列化之后的数据长度, 长度存在时直接返回结果 不会设置m.payloadLength

func (*Message) MinMux

func (m *Message) MinMux() int

func (*Message) Payloads

func (m *Message) Payloads() container2.Slice[byte]

func (*Message) PayloadsIterator

func (m *Message) PayloadsIterator() *container2.Iterator[[]byte]

func (*Message) ReWritePayload

func (m *Message) ReWritePayload(p []byte)

func (*Message) Reset

func (m *Message) Reset()

Reset 给内存复用的操作提供一致性的语义

func (*Message) SetMsgId

func (m *Message) SetMsgId(msgId uint64)

func (*Message) SetMsgType

func (m *Message) SetMsgType(msgTyp uint8)

func (*Message) SetPayloads

func (m *Message) SetPayloads(payloads []byte)

func (*Message) SetServiceName

func (m *Message) SetServiceName(s string)

type Setter

type Setter interface {
	GetAndSetLength() uint32
	SetMsgType(msgTyp uint8)
	SetServiceName(serviceName string)
	SetMsgId(msgId uint64)
	AppendPayloads(p []byte)
	ReWritePayload(p []byte)
	SetPayloads(payloads []byte)
	Reset()
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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