nano

package
v0.0.0-...-79e39e7 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2022 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeadLength    = 4
	MaxPacketSize = 64 * 1024
)

Codec constants.

View Source
const (
	Request  MsgType = 0x00
	Notify           = 0x01
	Response         = 0x02
	Push             = 0x03
)

Message types

View Source
const (

	// Handshake represents a handshake: request(client) <====> handshake response(server)
	Handshake = 0x01

	// HandshakeAck represents a handshake ack from client to server
	HandshakeAck = 0x02

	// Heartbeat represents a heartbeat
	Heartbeat = 0x03

	// Data represents a common data packet
	Data = 0x04

	// Kick represents a kick off packet
	Kick = 0x05 // disconnect message from server
)

Variables

View Source
var (
	ErrWrongMessageType  = errors.New("wrong message type")
	ErrInvalidMessage    = errors.New("invalid message")
	ErrRouteInfoNotFound = errors.New("route info not found in dictionary")
)

Errors that could be occurred in message codec

View Source
var ErrPacketSizeExceed = errors.New("codec: packet size exceed")

ErrPacketSizeExceed is the error used for encode/decode.

View Source
var ErrWrongPacketType = errors.New("wrong packet type")

ErrWrongPacketType represents a wrong packet type.

Functions

func Encode

func Encode(typ PacketType, data []byte) ([]byte, error)

Encode create a packet.Packet from the raw bytes slice and then encode to network bytes slice Protocol refs: https://github.com/NetEase/pomelo/wiki/Communication-Protocol

-<type>-|--------<length>--------|-<data>- --------|------------------------|-------- 1 byte packet type, 3 bytes packet data length(big end), and data segment

func MsgEncode

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

MsgEncode 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

func SetDictionary

func SetDictionary(dict map[string]uint16)

SetDictionary set routes map which be used to compress route. TODO(warning): set dictionary in runtime would be a dangerous operation!!!!!!

Types

type Agent

type Agent struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewAgent

func NewAgent(server tcpface.IServer, conn net.Conn, connId int) *Agent

func (*Agent) Close

func (a *Agent) Close() error

func (*Agent) GetConn

func (a *Agent) GetConn() net.Conn

func (*Agent) GetConnID

func (a *Agent) GetConnID() int

func (*Agent) MID

func (a *Agent) MID() uint

func (*Agent) Push

func (a *Agent) Push(route string, v interface{}) error

Push Push, implementation for session.NetworkEntity interface

func (*Agent) RemoteAddr

func (a *Agent) RemoteAddr() net.Addr

RemoteAddr implementation for session.NetworkEntity interface

func (*Agent) Response

func (a *Agent) Response(v interface{}) error

Response Response, implementation for session.NetworkEntity interface Response message to session

func (*Agent) ResponseMID

func (a *Agent) ResponseMID(mid uint, v interface{}) error

ResponseMID Response, implementation for session.NetworkEntity interface Response message to session

func (*Agent) SendBuffMsg

func (a *Agent) SendBuffMsg(data pendingMessage) error

SendBuffMsg 发生BuffMsg

func (*Agent) SendMsg

func (a *Agent) SendMsg(data pendingMessage) error

SendMsg 直接将Message数据发送数据给远程的TCP客户端

func (*Agent) SendRawMessage

func (a *Agent) SendRawMessage(useBuffer bool, data []byte) error

func (*Agent) StartWriter

func (a *Agent) StartWriter()

写消息Goroutine, 用户将数据发送给客户端

func (*Agent) String

func (a *Agent) String() string

String, implementation for Stringer interface

type Decoder

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

A Decoder reads and decodes network data slice

func NewDecoder

func NewDecoder() *Decoder

NewDecoder returns a new decoder that used for decode network bytes slice.

func (*Decoder) Decode

func (c *Decoder) Decode(data []byte) ([]*Packet, error)

Decode decode the network bytes slice to packet.Packet(s)

type Message

type Message struct {
	Type  MsgType // message type
	ID    uint    // unique id, zero while notify mode
	Route string  // route for locating service
	Data  []byte  // payload
	// contains filtered or unexported fields
}

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

func MsgDecode

func MsgDecode(data []byte) (*Message, error)

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

func NewMessage

func NewMessage() *Message

NewMessage returns a new message instance

func (*Message) Encode

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

Encode marshals message to binary format.

func (*Message) String

func (m *Message) String() string

String, implementation of fmt.Stringer interface

type MsgHandle

type MsgHandle struct {
	WorkerPoolSize int                     //业务工作Worker池的数量
	Serializer     serialize.Serializer    //序列化对象
	TaskQueue      []chan unhandledMessage //Worker负责取任务的消息队列
	Cfg            config.ConnectorConf    //配置
	// contains filtered or unexported fields
}

func NewMsgHandle

func NewMsgHandle() *MsgHandle

func (*MsgHandle) DoMsgHandler

func (h *MsgHandle) DoMsgHandler(request unhandledMessage)

DoMsgHandler 马上以非阻塞方式处理消息

func (*MsgHandle) DumpServices

func (h *MsgHandle) DumpServices()

DumpServices outputs all registered services

func (*MsgHandle) Handle

func (h *MsgHandle) Handle(iConn tcpface.IConnection)

func (*MsgHandle) Register

func (h *MsgHandle) Register(comp component.Component, opts ...component.Option) error

func (*MsgHandle) SendMsgToTaskQueue

func (h *MsgHandle) SendMsgToTaskQueue(request unhandledMessage)

SendMsgToTaskQueue 将消息交给TaskQueue,由worker进行处理

func (*MsgHandle) StartOneWorker

func (h *MsgHandle) StartOneWorker(workerID int, taskQueue chan unhandledMessage)

StartOneWorker 启动一个Worker工作流程

func (*MsgHandle) StartWorkerPool

func (h *MsgHandle) StartWorkerPool()

StartWorkerPool 启动worker工作池

type MsgType

type MsgType byte

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

type Packet

type Packet struct {
	Type   PacketType
	Length int
	Data   []byte
}

Packet represents a network packet.

func NewPacket

func NewPacket() *Packet

NewPacket create a Packet instance.

func (*Packet) String

func (p *Packet) String() string

String represents the Packet's in text mode.

type PacketType

type PacketType byte

PacketType represents the network packet's type such as: handshake and so on.

type ProtoNano

type ProtoNano struct {
	Client map[string]string `json:"client"`
	Server map[string]string `json:"server"`
}

func LoadProtobuf

func LoadProtobuf(filename string) (*ProtoNano, error)

Jump to

Keyboard shortcuts

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