pomeloMessage

package
v1.3.12 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: MIT Imports: 7 Imported by: 2

Documentation

Index

Constants

View Source
const (
	RouteCompressMask = 0x01 // 启用路由压缩 00000001
	MsgHeadLength     = 0x02 // 消息头的长度 00000010
	TypeMask          = 0x07 // 获取消息类型 00000111
	GZIPMask          = 0x10 // data compressed gzip mark
	ErrorMask         = 0x20 // 响应错误标识 00100000
)

掩码定义用来操作flag(1byte)

Variables

This section is empty.

Functions

func Encode

func Encode(m *Message) ([]byte, error)

Encode marshals message to binary format. Different message types is corresponding to different message header, message types is identified by 2-4 bit of flag field. The relationship between message types and message header is presented as follows: ------------------------------------------ | type | flag | other | |----------|--------|--------------------| | request |----000-|<message id>|<route>| | notify |----001-|<route> | | response |----010-|<message id> | | push |----011-|<route> | ------------------------------------------ The figure above indicates that the bit does not affect the type of message. See ref: https://github.com/lonnng/nano/blob/master/docs/communication_protocol.md See ref: https://github.com/NetEase/pomelo/wiki/%E5%8D%8F%E8%AE%AE%E6%A0%BC%E5%BC%8F

func GetCode

func GetCode(route string) (uint16, bool)

func GetDictionary

func GetDictionary() map[string]uint16

GetDictionary gets the routes map which is used to compress route.

func GetRoute

func GetRoute(code uint16) (route string, found bool)

func InvalidType

func InvalidType(t Type) bool

func IsDataCompression

func IsDataCompression() bool

func Routable

func Routable(t Type) bool

func SetDataCompression

func SetDataCompression(compression bool)

func SetDictionary

func SetDictionary(dict map[string]uint16)

SetDictionary set routes map which be used to compress route.

Types

type Message

type Message struct {
	Type  Type   // message type 4中消息类型
	ID    uint   // unique id, zero while notify mode 消息id(request response)
	Route string // route for locating service 消息路由
	Data  []byte // payload  消息体的原始数据

	Error bool // response error
	// contains filtered or unexported fields
}

Message represents a unmarshaled message or a message which to be marshaled message协议的主要作用是封装消息头,包括route和消息类型两部分, 不同的消息类型有着不同的消息头,在消息头里面可能要打入message id(即requestId)和route信息。 由于可能会有route压缩,而且对于服务端push的消息,message id为空,对于客户端请求的响应,route为空 消息头分为三部分,flag,message id,route。 如下图所示: flag(1byte) + message id(0~5byte) + route(0~256bytes) flag位是必须的,占用一个byte,它决定了后面的消息类型和内容的格式; message id和route则是可选的。 其中message id采用varints 128变长编码方式,根据值的大小,长度在0~5byte之间。 route则根据消息类型以及内容的大小,长度在0~255byte之间。

flag占用message头的第一个byte,其内容如下 preserved(4bits) + message type(3 bits) + route(1bit) 现在只用到了其中的4个bit,这四个bit包括两部分,占用3个bit的message type字段和占用1个bit的route标识,其中: message type用来标识消息类型,范围为0~7,

消息类型: 不同类型的消息,对应不同消息头,消息类型通过flag字段的第2-4位来确定,其对应关系以及相应的消息头如下图:

现在消息共有四类,request,notify,response,push,值的范围是0~3。 不同的消息类型有着不同的消息内容,下面会有详细分析。 最后一位的route表示route是否压缩,影响route字段的长度。 这两部分之间相互独立,互不影响。 request ----000- <message id> <route> notify ----001- <route> response ----010- <message id> push ----011- <route>

路由压缩标志 上图是不同的flag标志对应的route字段的内容: flag的最后一位为1时,表示路由压缩,需要通过查询字典来获取route; flag最后一位为0是,后面route则由一个uInt8的byte,用来表示route的字节长度。 之后是通过utf8编码后的route字 符串,其长度就是前面一位byte的uInt8的值,因此route的长度最大支持256B。

func Decode

func Decode(data []byte) (Message, error)

Decode unmarshal the bytes slice to a message See ref: https://github.com/lonnng/nano/blob/master/docs/communication_protocol.md

func New

func New() Message

func (*Message) String

func (t *Message) String() string

type Route

type Route struct {
	// contains filtered or unexported fields
}

Route struct

func DecodeRoute

func DecodeRoute(route string) (*Route, error)

DecodeRoute decodes the route

func NewRoute

func NewRoute(nodeType, handleName, method string) *Route

NewRoute create a new route

func (*Route) HandleName

func (r *Route) HandleName() string

func (*Route) Method

func (r *Route) Method() string

func (*Route) NodeType

func (r *Route) NodeType() string

func (*Route) String

func (r *Route) String() string

String transforms the route into a string

type Type

type Type byte

Type represents the type of message, which could be Request/Notify/Response/Push

const (
	Request  Type = 0x00 // ----000-
	Notify   Type = 0x01 // ----001-
	Response Type = 0x02 // ----010-
	Push     Type = 0x03 // ----011-
)

Message types

func (*Type) String

func (t *Type) String() string

Jump to

Keyboard shortcuts

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