message

package
Version: v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Unknown uint16 = iota
	JSON
	Protobuf
	RawString
)

Variables

View Source
var (
	// Routes is a map from route to code
	Routes = make(map[string]uint16)
	// Codes is a map from code to route
	Codes = make(map[uint16]string)
)
View Source
var (
	ErrWrongMessageType   = errors.New("wrong message type")
	ErrInvalidMessage     = errors.New("invalid message")
	ErrRouteInfoNotFound  = errors.New("route info not found in dictionary")
	ErrInvalidRouteLength = errors.New("invalid route length")
)

Errors that could be occurred in message codec

View Source
var (
	// Serializers is a map from route to serializer
	Serializers = make(map[string]serialize.Serializer)
)

Functions

func Encode

func Encode(m *Message, routes map[string]uint16) ([]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: 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

func GetSerializer

func GetSerializer(typ uint16) serialize.Serializer

func GetSerializerType

func GetSerializerType(s serialize.Serializer) uint16

func ParseDictionary

func ParseDictionary(dict map[string]uint16) (map[string]uint16, map[uint16]string)

ParseDictionary parses dictionary into routes and codes independently

func ReadDictionary

func ReadDictionary() (map[string]uint16, map[uint16]string)

ReadDictionary returns dictionary for compressed route.

func ReadSerializers

func ReadSerializers() map[string]serialize.Serializer

ReadSerializers returns serializers for compressed route.

func RouteSerialize

func RouteSerialize(serializers map[string]serialize.Serializer, route string, v interface{}) ([]byte, error)

func Serialize

func Serialize(v interface{}) ([]byte, error)

func ShortVersion

func ShortVersion(version string) uint32

func WriteDictionary

func WriteDictionary(dict map[string]uint16) (map[string]uint16, map[uint16]string)

WriteDictionary is to set dictionary when new route dictionary is found.

func WriteDictionaryItem

func WriteDictionaryItem(route string, code uint16) (map[string]uint16, map[uint16]string)

WriteDictionaryItem is to set dictionary item when server registers.

func WriteSerializerItem

func WriteSerializerItem(route string, typ uint16) map[string]serialize.Serializer

WriteSerializerItem is to set serializer item when server registers.

func WriteSerializers

func WriteSerializers(serializers map[string]uint16) map[string]serialize.Serializer

WriteSerializers is to set serializers when new serializer dictionary is found.

Types

type Message

type Message struct {
	Type       Type   // message type
	ShortVer   uint32 // message short version
	ID         uint64 // unique id, zero while notify mode
	Route      string // route for locating service
	Data       []byte // payload
	Compressed bool   // is message compressed
}

Message represents a unmarshaled message or a message which to be marshaled

func Decode

func Decode(data []byte, codes map[uint16]string) (*Message, bool, error)

Decode unmarshal the bytes slice to a message

func New

func New() *Message

New returns a new message instance

func (*Message) String

func (m *Message) String() string

String, implementation of fmt.Stringer interface

type Type

type Type byte

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

const (
	Request Type = iota
	Notify
	Response
	Push
)

Message types

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL