zinx

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: 15 Imported by: 0

Documentation

Index

Constants

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

Codec constants.

Variables

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

ErrPacketSizeExceed is the error used for encode/decode.

Functions

func Encode

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

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.

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) RemoteAddr

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

RemoteAddr implementation for session.NetworkEntity interface

func (*Agent) SendBuffMsg

func (a *Agent) SendBuffMsg(msgID int32, data []byte) error

SendBuffMsg 发生BuffMsg

func (*Agent) SendMsg

func (a *Agent) SendMsg(msgID int32, data []byte) error

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

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 {
	Id   int32  //消息的
	Data []byte //消息的内容
}

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 ReadMsg

func ReadMsg(conn net.Conn) (*Message, error)

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 {
	Apis           map[int32]Router //存放每个MsgId 所对应的处理方法的map属性
	WorkerPoolSize int              //业务工作Worker池的数量
	TaskQueue      []chan *Request  //Worker负责取任务的消息队列
}

func NewMsgHandle

func NewMsgHandle() *MsgHandle

func (*MsgHandle) AddRouter

func (h *MsgHandle) AddRouter(msgId int32, router Router)

AddRouter 为消息添加具体的处理逻辑

func (*MsgHandle) DoMsgHandler

func (h *MsgHandle) DoMsgHandler(request *Request)

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

func (*MsgHandle) Handle

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

func (*MsgHandle) SendMsgToTaskQueue

func (h *MsgHandle) SendMsgToTaskQueue(request *Request)

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

func (*MsgHandle) StartOneWorker

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

StartOneWorker 启动一个Worker工作流程

func (*MsgHandle) StartWorkerPool

func (h *MsgHandle) StartWorkerPool()

StartWorkerPool 启动worker工作池

type Packet

type Packet struct {
	Length int    //消息的长度
	Data   []byte //消息内容
}

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 Request

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

func (*Request) GetConnID

func (r *Request) GetConnID() int

func (*Request) GetConnection

func (r *Request) GetConnection() tcpface.IConnection

func (*Request) GetMsgData

func (r *Request) GetMsgData() []byte

func (*Request) GetMsgID

func (r *Request) GetMsgID() int32

func (*Request) GetServerID

func (r *Request) GetServerID() string

type Router

type Router func(req *Request)

Jump to

Keyboard shortcuts

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