Documentation ¶
Index ¶
- func GetLogger() ziface.ILogger
- func LoadZapLogger(level LogLevel) ziface.ILogger
- func NewDataPack() ziface.Packet
- func NewServer(opts ...Option) ziface.IServer
- func SetLogger(logHandler ziface.ILogger, level LogLevel)
- type BaseRouter
- type ConnManager
- func (connMgr *ConnManager) Add(conn ziface.IConnection)
- func (connMgr *ConnManager) ClearConn()
- func (connMgr *ConnManager) ClearOneConn(connID uint32)
- func (connMgr *ConnManager) Get(connID uint32) (ziface.IConnection, error)
- func (connMgr *ConnManager) Len() int
- func (connMgr *ConnManager) Remove(conn ziface.IConnection)
- type Connection
- func (c *Connection) Context() context.Context
- func (c *Connection) GetConnID() uint32
- func (c *Connection) GetProperty(key string) (interface{}, error)
- func (c *Connection) GetTCPConnection() *net.TCPConn
- func (c *Connection) RemoteAddr() net.Addr
- func (c *Connection) RemoveProperty(key string)
- func (c *Connection) SendBuffMsg(msgID, sn uint8, data []byte) error
- func (c *Connection) SendBuffMsgPackage(msgPackage ziface.IMessage) ([]byte, error)
- func (c *Connection) SendMsg(msgID, sn uint8, data []byte) error
- func (c *Connection) SendMsgPackage(msgPackage ziface.IMessage) ([]byte, error)
- func (c *Connection) SetProperty(key string, value interface{})
- func (c *Connection) Start()
- func (c *Connection) StartReader()
- func (c *Connection) StartWriter()
- func (c *Connection) Stop()
- type DataPack
- type LogLevel
- type Message
- func (msg *Message) GetData() []byte
- func (msg *Message) GetDataLen() uint16
- func (msg *Message) GetExtendData() []byte
- func (msg *Message) GetHeaderData() []byte
- func (msg *Message) GetMagicCode() uint16
- func (msg *Message) GetMsgID() uint8
- func (msg *Message) GetSn() uint8
- func (msg *Message) SetData(data []byte)
- func (msg *Message) SetDataLen(len uint16)
- func (msg *Message) SetExtendData(extendData []byte) error
- func (msg *Message) SetHeaderData(data []byte)
- func (msg *Message) SetMagicCode(magicCode uint16)
- func (msg *Message) SetMsgID(msgID uint8)
- type MsgHandle
- func (mh *MsgHandle) AddRouter(msgID uint8, router ziface.IRouter)
- func (mh *MsgHandle) DoMsgHandler(request ziface.IRequest)
- func (mh *MsgHandle) SendMsgToTaskQueue(request ziface.IRequest)
- func (mh *MsgHandle) StartOneWorker(workerID int, taskQueue chan ziface.IRequest)
- func (mh *MsgHandle) StartWorkerPool()
- type Option
- type Request
- func (r *Request) GetConnection() ziface.IConnection
- func (r *Request) GetData() []byte
- func (r *Request) GetExtendData() []byte
- func (r *Request) GetHeaderData() []byte
- func (r *Request) GetMagicCode() uint16
- func (r *Request) GetMessage() ziface.IMessage
- func (r *Request) GetMsgID() uint8
- func (r *Request) GetSn() uint8
- type Server
- func (s *Server) AddRouter(msgID uint8, router ziface.IRouter)
- func (s *Server) CallOnConnStart(conn ziface.IConnection)
- func (s *Server) CallOnConnStop(conn ziface.IConnection)
- func (s *Server) GetConnMgr() ziface.IConnManager
- func (s *Server) Packet() ziface.Packet
- func (s *Server) Serve()
- func (s *Server) SetOnConnStart(hookFunc func(ziface.IConnection))
- func (s *Server) SetOnConnStop(hookFunc func(ziface.IConnection))
- func (s *Server) Start()
- func (s *Server) Stop()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadZapLogger ¶
LoadZapLogger 加载zap日志处理器 默认使用zap sugar
Types ¶
type ConnManager ¶
type ConnManager struct {
// contains filtered or unexported fields
}
ConnManager 连接管理模块
func (*ConnManager) ClearOneConn ¶
func (connMgr *ConnManager) ClearOneConn(connID uint32)
ClearOneConn 利用ConnID获取一个链接 并且删除
func (*ConnManager) Get ¶
func (connMgr *ConnManager) Get(connID uint32) (ziface.IConnection, error)
Get 利用ConnID获取链接
func (*ConnManager) Remove ¶
func (connMgr *ConnManager) Remove(conn ziface.IConnection)
Remove 删除连接
type Connection ¶
type Connection struct { // 当前Conn属于哪个Server TCPServer ziface.IServer // 当前连接的socket TCP套接字 Conn *net.TCPConn // 当前连接的ID 也可以称作为SessionID,ID全局唯一 ConnID uint32 // 消息管理MsgID和对应处理方法的消息管理模块 MsgHandler ziface.IMsgHandle sync.RWMutex // contains filtered or unexported fields }
Connection 链接
func NewConnection ¶
func NewConnection(server ziface.IServer, conn *net.TCPConn, connID uint32, msgHandler ziface.IMsgHandle) *Connection
NewConnection 创建连接的方法
func (*Connection) Context ¶
func (c *Connection) Context() context.Context
返回ctx,用于用户自定义的go程获取连接退出状态
func (*Connection) GetProperty ¶
func (c *Connection) GetProperty(key string) (interface{}, error)
GetProperty 获取链接属性
func (*Connection) GetTCPConnection ¶
func (c *Connection) GetTCPConnection() *net.TCPConn
GetTCPConnection 从当前连接获取原始的socket TCPConn
func (*Connection) RemoveProperty ¶
func (c *Connection) RemoveProperty(key string)
RemoveProperty 移除链接属性
func (*Connection) SendBuffMsg ¶
func (c *Connection) SendBuffMsg(msgID, sn uint8, data []byte) error
SendBuffMsg 发送BuffMsg
func (*Connection) SendBuffMsgPackage ¶ added in v1.0.5
func (c *Connection) SendBuffMsgPackage(msgPackage ziface.IMessage) ([]byte, error)
SendBuffMsgPackage 发送BuffMsg包
func (*Connection) SendMsg ¶
func (c *Connection) SendMsg(msgID, sn uint8, data []byte) error
SendMsg 直接将Message数据发送数据给远程的TCP客户端
func (*Connection) SendMsgPackage ¶ added in v1.0.5
func (c *Connection) SendMsgPackage(msgPackage ziface.IMessage) ([]byte, error)
SendMsgPackage 发送Message包
func (*Connection) SetProperty ¶
func (c *Connection) SetProperty(key string, value interface{})
SetProperty 设置链接属性
func (*Connection) StartReader ¶
func (c *Connection) StartReader()
StartReader 读消息Goroutine,用于从客户端中读取数据
func (*Connection) StartWriter ¶
func (c *Connection) StartWriter()
StartWriter 写消息Goroutine, 用户将数据发送给客户端
type DataPack ¶
type DataPack struct{}
DataPack 封包拆包类实例,暂时不需要成员
type Message ¶
type Message struct { MagicCode uint16 // 魔数(小安必须) ID uint8 // 消息的ID(命令字,小安必须) Sn uint8 // 序列号(小安必须) DataLen uint16 // 消息的长度(小安必须) HeaderData []byte // 消息头 Data []byte // 消息的内容 ExtendData []byte // 扩展消息 }
Message 消息
func NewMsgPackage ¶
NewMsgPackage 创建一个Message消息包
func (*Message) GetExtendData ¶ added in v1.0.3
GetExtendData 获取扩展消息
func (*Message) GetHeaderData ¶ added in v1.0.5
GetHeaderData 获取消息头内容
func (*Message) GetMagicCode ¶ added in v1.0.3
GetMagicCode 获取魔数
func (*Message) SetExtendData ¶ added in v1.0.3
SetExtendData 设置扩展数据
func (*Message) SetHeaderData ¶ added in v1.0.5
SetHeaderData 设置消息头内容
func (*Message) SetMagicCode ¶ added in v1.0.3
SetMagicCode 设置魔数
type MsgHandle ¶
type MsgHandle struct { Apis map[uint8]ziface.IRouter // 存放每个MsgID 所对应的处理方法的map属性 WorkerPoolSize uint32 // 业务工作Worker池的数量 TaskQueue []chan ziface.IRequest // Worker负责取任务的消息队列 }
MsgHandle -
func (*MsgHandle) DoMsgHandler ¶
DoMsgHandler 处理消息 TODO: 需要探讨这里是否使用goroutine执行
func (*MsgHandle) SendMsgToTaskQueue ¶
SendMsgToTaskQueue 将消息交给TaskQueue,由worker进行处理
func (*MsgHandle) StartOneWorker ¶
StartOneWorker 启动一个Worker工作流程
func (*MsgHandle) StartWorkerPool ¶
func (mh *MsgHandle) StartWorkerPool()
StartWorkerPool 启动worker工作池
type Option ¶
type Option func(s *Server)
func WithPacket ¶
WithPacket 只要实现Packet 接口可自由实现数据包解析格式,如果没有则使用默认解析格式
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request 请求
func (*Request) GetConnection ¶
func (r *Request) GetConnection() ziface.IConnection
GetConnection 获取请求连接信息
func (*Request) GetExtendData ¶ added in v1.0.6
GetExtendData 获取扩展数据
func (*Request) GetHeaderData ¶ added in v1.0.6
GetHeaderData 获取消息头数据
func (*Request) GetMagicCode ¶ added in v1.0.6
GetMagicCode 获取消息魔数
func (*Request) GetMessage ¶ added in v1.0.6
GetMessage 获取客户端请求数据
type Server ¶
type Server struct { // 服务器的名称 Name string // tcp4 or other IPVersion string // 服务绑定的IP地址 IP string // 服务绑定的端口 Port int // 当前Server的链接管理器 ConnMgr ziface.IConnManager // 该Server的连接创建时Hook函数 OnConnStart func(conn ziface.IConnection) // 该Server的连接断开时的Hook函数 OnConnStop func(conn ziface.IConnection) // contains filtered or unexported fields }
Server 接口实现,定义一个Server服务类
func (*Server) CallOnConnStart ¶
func (s *Server) CallOnConnStart(conn ziface.IConnection)
CallOnConnStart 调用连接OnConnStart Hook函数
func (*Server) CallOnConnStop ¶
func (s *Server) CallOnConnStop(conn ziface.IConnection)
CallOnConnStop 调用连接OnConnStop Hook函数
func (*Server) SetOnConnStart ¶
func (s *Server) SetOnConnStart(hookFunc func(ziface.IConnection))
SetOnConnStart 设置该Server的连接创建时Hook函数
func (*Server) SetOnConnStop ¶
func (s *Server) SetOnConnStop(hookFunc func(ziface.IConnection))
SetOnConnStop 设置该Server的连接断开时的Hook函数