Documentation ¶
Index ¶
- func CallBackToClient(conn *net.TCPConn, data []byte, cnt int) error
- func NewServer() tiface.IServer
- type BaseRouter
- type ConnManager
- type Connection
- 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 uint32, data []byte) error
- func (c *Connection) SendMsg(msgId uint32, data []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 Message
- type MsgHandle
- func (mh *MsgHandle) AddRouter(msgId uint32, router tiface.IRouter)
- func (mh *MsgHandle) DoMsgHandler(request tiface.IRequest)
- func (mh *MsgHandle) SendMsgToTaskQueue(request tiface.IRequest)
- func (mh *MsgHandle) StartOneWorker(workerID int, taskQueue chan tiface.IRequest)
- func (mh *MsgHandle) StartWorkerPool()
- type Request
- type Server
- func (s *Server) AddRouter(msgId uint32, router tiface.IRouter)
- func (s *Server) CallOnConnStart(conn tiface.IConnection)
- func (s *Server) CallOnConnStop(conn tiface.IConnection)
- func (s *Server) GetConnMgr() tiface.IConnManager
- func (s *Server) Serve()
- func (s *Server) SetOnConnStart(hookFunc func(tiface.IConnection))
- func (s *Server) SetOnConnStop(hookFunc func(tiface.IConnection))
- func (s *Server) Start()
- func (s *Server) Stop()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CallBackToClient ¶
============== 定义当前客户端链接的handle api ===========
Types ¶
type BaseRouter ¶
type BaseRouter struct{}
实现router时,先嵌入这个BaseRouter基类,然后用户根据需要对这个基类的方法进行重写(类似Beego框架的实现方式)
func (*BaseRouter) PostHandle ¶
func (br *BaseRouter) PostHandle(req tiface.IRequest)
处理conn业务之后的钩子方法
func (*BaseRouter) PreHandle ¶
func (br *BaseRouter) PreHandle(req tiface.IRequest)
在处理conn业务之前的钩子方法
type ConnManager ¶
type ConnManager struct {
// contains filtered or unexported fields
}
连接管理模块
func (*ConnManager) Get ¶
func (connMgr *ConnManager) Get(connID uint32) (tiface.IConnection, error)
利用ConnID获取链接
type Connection ¶
type Connection struct { // 当前Conn属于哪个Server TcpServer tiface.IServer // 当前连接的socket TCP套接字 Conn *net.TCPConn // 当前连接的ID 也可以称作为SessionID,ID全局唯一 ConnID uint32 // V0.6 消息MsgId和对应业务处理api的消息管理模块 MsgHandler tiface.IMsgHandle // 告知该链接已经退出/停止的channel(由Reader告知Writer退出) ExitBuffChan chan bool // contains filtered or unexported fields }
创建连接的方法
func NewConnection ¶
func NewConnection(server tiface.IServer, conn *net.TCPConn, connID uint32, msgHandler tiface.IMsgHandle) *Connection
func (*Connection) GetProperty ¶
func (c *Connection) GetProperty(key string) (interface{}, error)
获取链接属性
func (*Connection) GetTCPConnection ¶
func (c *Connection) GetTCPConnection() *net.TCPConn
从当前连接获取原始的socket TCPConn
func (*Connection) SendBuffMsg ¶
func (c *Connection) SendBuffMsg(msgId uint32, data []byte) error
将数据发送给缓冲队列,通过专门从缓冲队列读数据的go routine写给客户端
func (*Connection) SendMsg ¶
func (c *Connection) SendMsg(msgId uint32, data []byte) error
将要发送给客户端的数据,先进行封包,再发送给远程的TCP客户端
func (*Connection) SetProperty ¶
func (c *Connection) SetProperty(key string, value interface{})
设置链接属性
func (*Connection) StartWriter ¶
func (c *Connection) StartWriter()
写消息Goroutine,监控管道msgChan并将数据发送给客户端
type MsgHandle ¶
type MsgHandle struct { // 存放每个MsgId 所对应的处理方法 Apis map[uint32]tiface.IRouter // 业务工作Worker池的worker数量 WorkerPoolSize uint32 // Worker取任务的消息队列 TaskQueue []chan tiface.IRequest }
消息处理模块的实现
func (*MsgHandle) DoMsgHandler ¶
马上以非阻塞方式处理消息,调度/执行对应的Router消息处理方法
func (*MsgHandle) SendMsgToTaskQueue ¶
将消息交给TaskQueue, 由worker进行处理
func (*MsgHandle) StartOneWorker ¶
启动一个worker
func (*MsgHandle) StartWorkerPool ¶
func (mh *MsgHandle) StartWorkerPool()
启动worker工作池(只执行一次,因为一个框架只能有一个工作池)
type Server ¶
type Server struct { //服务器的名称 Name string //tcp4 or other IPVersion string //服务绑定的IP地址 IP string //服务绑定的端口 Port int //当前Server的链接管理器 ConnMgr tiface.IConnManager // 该Server的连接创建时Hook函数 OnConnStart func(conn tiface.IConnection) // 该Server的连接断开时的Hook函数 OnConnStop func(conn tiface.IConnection) // contains filtered or unexported fields }
iServer 接口实现,定义一个Server服务类
func (*Server) CallOnConnStart ¶
func (s *Server) CallOnConnStart(conn tiface.IConnection)
调用连接OnConnStart Hook函数
func (*Server) CallOnConnStop ¶
func (s *Server) CallOnConnStop(conn tiface.IConnection)
调用连接OnConnStop Hook函数
func (*Server) SetOnConnStart ¶
func (s *Server) SetOnConnStart(hookFunc func(tiface.IConnection))
设置该Server的连接创建时Hook函数
func (*Server) SetOnConnStop ¶
func (s *Server) SetOnConnStop(hookFunc func(tiface.IConnection))
设置该Server的连接断开时的Hook函数
Click to show internal directories.
Click to hide internal directories.