Documentation ¶
Index ¶
- Constants
- Variables
- func Encode(typ PacketType, data []byte) ([]byte, error)
- func MsgEncode(m *Message) ([]byte, error)
- func SetDictionary(dict map[string]uint16)
- type Agent
- func (a *Agent) Close() error
- func (a *Agent) GetConn() net.Conn
- func (a *Agent) GetConnID() int
- func (a *Agent) MID() uint
- func (a *Agent) Push(route string, v any) error
- func (a *Agent) RemoteAddr() net.Addr
- func (a *Agent) Response(v any) error
- func (a *Agent) ResponseMID(mid uint, v any) error
- func (a *Agent) SendBuffMsg(data pendingMessage) error
- func (a *Agent) SendMsg(data pendingMessage) error
- func (a *Agent) SendRawMessage(useBuffer bool, data []byte) error
- func (a *Agent) StartWriter()
- func (a *Agent) String() string
- type Decoder
- type Message
- type MsgHandle
- func (h *MsgHandle) DoMsgHandler(request unhandledMessage)
- func (h *MsgHandle) DumpServices()
- func (h *MsgHandle) Handle(iConn tcpface.IConnection)
- func (h *MsgHandle) Register(comp component.Component, opts ...component.Option) error
- func (h *MsgHandle) SendMsgToTaskQueue(request unhandledMessage)
- func (h *MsgHandle) StartOneWorker(workerID int, taskQueue chan unhandledMessage)
- func (h *MsgHandle) StartWorkerPool()
- type MsgType
- type Packet
- type PacketType
- type ProtoNano
Constants ¶
const ( HeadLength = 4 MaxPacketSize = 64 * 1024 )
Codec constants.
const ( Request MsgType = 0x00 Notify = 0x01 Response = 0x02 Push = 0x03 )
Message types
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 ¶
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
var ErrPacketSizeExceed = errors.New("codec: packet size exceed")
ErrPacketSizeExceed is the error used for encode/decode.
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 ¶
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 ¶
SetDictionary set routes map which be used to compress route. TODO(warning): set dictionary in runtime would be a dangerous operation!!!!!!
Types ¶
type Agent ¶
func (*Agent) RemoteAddr ¶
RemoteAddr implementation for session.NetworkEntity interface
func (*Agent) Response ¶
Response Response, implementation for session.NetworkEntity interface Response message to session
func (*Agent) ResponseMID ¶
ResponseMID Response, implementation for session.NetworkEntity interface Response message to session
func (*Agent) SendBuffMsg ¶
SendBuffMsg 发生BuffMsg
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.
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 ¶
MsgDecode unmarshal the bytes slice to a message See ref: https://github.com/lonnng/nano/blob/master/docs/communication_protocol.md
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) SendMsgToTaskQueue ¶
func (h *MsgHandle) SendMsgToTaskQueue(request unhandledMessage)
SendMsgToTaskQueue 将消息交给TaskQueue,由worker进行处理
func (*MsgHandle) StartOneWorker ¶
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.
type PacketType ¶
type PacketType byte
PacketType represents the network packet's type such as: handshake and so on.