Documentation ¶
Overview ¶
服务端Server的链接模块
Index ¶
- Constants
- Variables
- func NewClient(ip string, port int, opts ...ClientOption) ziface.IClient
- func NewHeartbeatChecker(interval time.Duration) ziface.IHeartbeatChecker
- func NewRequest(conn ziface.IConnection, msg ziface.IMessage) ziface.IRequest
- func NewServer(opts ...Option) ziface.IServer
- func NewTLSClient(ip string, port int, opts ...ClientOption) ziface.IClient
- func NewUserConfServer(config *zconf.Config, opts ...Option) ziface.IServer
- func NewWsClient(ip string, port int, opts ...ClientOption) ziface.IClient
- type BaseRouter
- type Client
- func (c *Client) AddInterceptor(interceptor ziface.IInterceptor)
- func (c *Client) AddRouter(msgID uint32, router ziface.IRouter)
- func (c *Client) Conn() ziface.IConnection
- func (c *Client) GetLengthField() *ziface.LengthField
- func (c *Client) GetMsgHandler() ziface.IMsgHandle
- func (c *Client) GetOnConnStart() func(ziface.IConnection)
- func (c *Client) GetOnConnStop() func(ziface.IConnection)
- func (c *Client) GetPacket() ziface.IDataPack
- func (c *Client) SetDecoder(decoder ziface.IDecoder)
- func (c *Client) SetOnConnStart(hookFunc func(ziface.IConnection))
- func (c *Client) SetOnConnStop(hookFunc func(ziface.IConnection))
- func (c *Client) SetPacket(packet ziface.IDataPack)
- func (c *Client) Start()
- func (c *Client) StartHeartBeat(interval time.Duration)
- func (c *Client) StartHeartBeatWithOption(interval time.Duration, option *ziface.HeartBeatOption)
- func (c *Client) Stop()
- type ClientOption
- type ConnManager
- func (connMgr *ConnManager) Add(conn ziface.IConnection)
- func (connMgr *ConnManager) ClearConn()
- func (connMgr *ConnManager) Get(connID uint64) (ziface.IConnection, error)
- func (connMgr *ConnManager) GetAllConnID() []uint64
- func (connMgr *ConnManager) Len() int
- func (connMgr *ConnManager) Range(cb func(uint64, ziface.IConnection, interface{}) error, args interface{}) (err error)
- func (connMgr *ConnManager) Remove(conn ziface.IConnection)
- type Connection
- func (c *Connection) Context() context.Context
- func (c *Connection) GetConnID() uint64
- func (c *Connection) GetConnection() net.Conn
- func (c *Connection) GetProperty(key string) (interface{}, error)
- func (c *Connection) GetTCPConnection() net.Conndeprecated
- func (c *Connection) GetWsConn() *websocket.Conn
- func (c *Connection) IsAlive() bool
- func (c *Connection) LocalAddr() net.Addr
- func (c *Connection) RemoteAddr() net.Addr
- func (c *Connection) RemoveProperty(key string)
- func (c *Connection) Send(data []byte) error
- func (c *Connection) SendBuffMsg(msgID uint32, data []byte) error
- func (c *Connection) SendMsg(msgID uint32, data []byte) error
- func (c *Connection) SendToQueue(data []byte) error
- func (c *Connection) SetHeartBeat(checker ziface.IHeartbeatChecker)
- 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 HeartbeatChecker
- func (h *HeartbeatChecker) BindConn(conn ziface.IConnection)
- func (h *HeartbeatChecker) BindRouter(msgID uint32, router ziface.IRouter)
- func (h *HeartbeatChecker) Clone() ziface.IHeartbeatChecker
- func (h *HeartbeatChecker) MsgID() uint32
- func (h *HeartbeatChecker) Router() ziface.IRouter
- func (h *HeartbeatChecker) SendHeartBeatMsg() error
- func (h *HeartbeatChecker) SetHeartbeatFunc(beatFunc ziface.HeartBeatFunc)
- func (h *HeartbeatChecker) SetHeartbeatMsgFunc(f ziface.HeartBeatMsgFunc)
- func (h *HeartbeatChecker) SetOnRemoteNotAlive(f ziface.OnRemoteNotAlive)
- func (h *HeartbeatChecker) Start()
- func (h *HeartbeatChecker) Stop()
- type HeatBeatDefaultRouter
- type MsgHandle
- func (mh *MsgHandle) AddInterceptor(interceptor ziface.IInterceptor)
- func (mh *MsgHandle) AddRouter(msgID uint32, router ziface.IRouter)
- func (mh *MsgHandle) Execute(request ziface.IRequest)
- func (mh *MsgHandle) Intercept(chain ziface.IChain) ziface.IcResp
- 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) Abort()
- func (r *Request) BindRouter(router ziface.IRouter)
- func (r *Request) Call()
- func (r *Request) GetConnection() ziface.IConnection
- func (r *Request) GetData() []byte
- func (r *Request) GetMessage() ziface.IMessage
- func (r *Request) GetMsgID() uint32
- func (r *Request) GetResponse() ziface.IcResp
- func (r *Request) Goto(step ziface.HandleStep)
- func (r *Request) SetResponse(response ziface.IcResp)
- type Server
- func (s *Server) AddInterceptor(interceptor ziface.IInterceptor)
- func (s *Server) AddRouter(msgID uint32, router ziface.IRouter)
- func (s *Server) GetConnMgr() ziface.IConnManager
- func (s *Server) GetHeartBeat() ziface.IHeartbeatChecker
- func (s *Server) GetLengthField() *ziface.LengthField
- func (s *Server) GetMsgHandler() ziface.IMsgHandle
- func (s *Server) GetOnConnStart() func(ziface.IConnection)
- func (s *Server) GetOnConnStop() func(ziface.IConnection)
- func (s *Server) GetPacket() ziface.IDataPack
- func (s *Server) Serve()
- func (s *Server) SetDecoder(decoder ziface.IDecoder)
- func (s *Server) SetOnConnStart(hookFunc func(ziface.IConnection))
- func (s *Server) SetOnConnStop(hookFunc func(ziface.IConnection))
- func (s *Server) SetPacket(packet ziface.IDataPack)
- func (s *Server) Start()
- func (s *Server) StartHeartBeat(interval time.Duration)
- func (s *Server) StartHeartBeatWithOption(interval time.Duration, option *ziface.HeartBeatOption)
- func (s *Server) Stop()
- type WsConnection
- func (c *WsConnection) Context() context.Context
- func (c *WsConnection) GetConnID() uint64
- func (c *WsConnection) GetConnection() net.Conn
- func (c *WsConnection) GetProperty(key string) (interface{}, error)
- func (c *WsConnection) GetTCPConnection() net.Conndeprecated
- func (c *WsConnection) GetWsConn() *websocket.Conn
- func (c *WsConnection) IsAlive() bool
- func (c *WsConnection) LocalAddr() net.Addr
- func (c *WsConnection) RemoteAddr() net.Addr
- func (c *WsConnection) RemoveProperty(key string)
- func (c *WsConnection) Send(data []byte) error
- func (c *WsConnection) SendBuffMsg(msgID uint32, data []byte) error
- func (c *WsConnection) SendMsg(msgID uint32, data []byte) error
- func (c *WsConnection) SendToQueue(data []byte) error
- func (c *WsConnection) SetHeartBeat(checker ziface.IHeartbeatChecker)
- func (c *WsConnection) SetProperty(key string, value interface{})
- func (c *WsConnection) Start()
- func (c *WsConnection) StartReader()
- func (c *WsConnection) StartWriter()
- func (c *WsConnection) Stop()
Constants ¶
const ( PRE_HANDLE ziface.HandleStep = iota //PreHandle 预处理 HANDLE //Handle 处理 POST_HANDLE //PostHandle 后处理 HANDLE_OVER )
Variables ¶
var AcceptDelay *acceptDelay
Functions ¶
func NewHeartbeatChecker ¶
func NewHeartbeatChecker(interval time.Duration) ziface.IHeartbeatChecker
NewHeartbeatChecker 创建心跳检测器
func NewRequest ¶
func NewTLSClient ¶
func NewTLSClient(ip string, port int, opts ...ClientOption) ziface.IClient
func NewUserConfServer ¶
NewServer 创建一个服务器句柄
func NewWsClient ¶
func NewWsClient(ip string, port int, opts ...ClientOption) ziface.IClient
Types ¶
type Client ¶
type Client struct { //目标链接服务器的IP Ip string //目标链接服务器的端口 Port int // errChan ErrChan chan error // contains filtered or unexported fields }
func (*Client) AddInterceptor ¶
func (c *Client) AddInterceptor(interceptor ziface.IInterceptor)
func (*Client) Conn ¶
func (c *Client) Conn() ziface.IConnection
func (*Client) GetLengthField ¶
func (c *Client) GetLengthField() *ziface.LengthField
func (*Client) GetMsgHandler ¶
func (c *Client) GetMsgHandler() ziface.IMsgHandle
func (*Client) GetOnConnStart ¶
func (c *Client) GetOnConnStart() func(ziface.IConnection)
GetOnConnStart 得到该Server的连接创建时Hook函数
func (*Client) GetOnConnStop ¶
func (c *Client) GetOnConnStop() func(ziface.IConnection)
得到该Server的连接断开时的Hook函数
func (*Client) SetDecoder ¶
func (*Client) SetOnConnStart ¶
func (c *Client) SetOnConnStart(hookFunc func(ziface.IConnection))
设置该Client的连接创建时Hook函数
func (*Client) SetOnConnStop ¶
func (c *Client) SetOnConnStop(hookFunc func(ziface.IConnection))
设置该Client的连接断开时的Hook函数
func (*Client) StartHeartBeat ¶
StartHeartBeat 启动心跳检测 interval 每次发送心跳的时间间隔
func (*Client) StartHeartBeatWithOption ¶
func (c *Client) StartHeartBeatWithOption(interval time.Duration, option *ziface.HeartBeatOption)
启动心跳检测(自定义回调)
type ClientOption ¶
Client的客户端Option
type ConnManager ¶
type ConnManager struct {
// contains filtered or unexported fields
}
ConnManager 连接管理模块
func (*ConnManager) Get ¶
func (connMgr *ConnManager) Get(connID uint64) (ziface.IConnection, error)
Get 利用ConnID获取链接
func (*ConnManager) GetAllConnID ¶
func (connMgr *ConnManager) GetAllConnID() []uint64
GetAllConnID 获取所有连接的ID
func (*ConnManager) Range ¶
func (connMgr *ConnManager) Range(cb func(uint64, ziface.IConnection, interface{}) error, args interface{}) (err error)
Range 遍历所有连接
func (*ConnManager) Remove ¶
func (connMgr *ConnManager) Remove(conn ziface.IConnection)
Remove 删除连接
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection Tcp连接模块 用于处理Tcp连接的读写业务 一个连接对应一个Connection
func (*Connection) Context ¶
func (c *Connection) Context() context.Context
返回ctx,用于用户自定义的go程获取连接退出状态
func (*Connection) GetConnection ¶
func (c *Connection) GetConnection() net.Conn
func (*Connection) GetProperty ¶
func (c *Connection) GetProperty(key string) (interface{}, error)
GetProperty 获取链接属性
func (*Connection) GetTCPConnection
deprecated
func (c *Connection) GetTCPConnection() net.Conn
Deprecated: use GetConnection instead
func (*Connection) GetWsConn ¶
func (c *Connection) GetWsConn() *websocket.Conn
func (*Connection) IsAlive ¶
func (c *Connection) IsAlive() bool
func (*Connection) RemoveProperty ¶
func (c *Connection) RemoveProperty(key string)
RemoveProperty 移除链接属性
func (*Connection) Send ¶
func (c *Connection) Send(data []byte) error
func (*Connection) SendBuffMsg ¶
func (c *Connection) SendBuffMsg(msgID uint32, data []byte) error
SendBuffMsg 发生BuffMsg
func (*Connection) SendMsg ¶
func (c *Connection) SendMsg(msgID uint32, data []byte) error
SendMsg 直接将Message数据发送数据给远程的TCP客户端
func (*Connection) SendToQueue ¶
func (c *Connection) SendToQueue(data []byte) error
func (*Connection) SetHeartBeat ¶
func (c *Connection) SetHeartBeat(checker ziface.IHeartbeatChecker)
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 HeartbeatChecker ¶
type HeartbeatChecker struct {
// contains filtered or unexported fields
}
func (*HeartbeatChecker) BindConn ¶
func (h *HeartbeatChecker) BindConn(conn ziface.IConnection)
BindConn 绑定一个链接
func (*HeartbeatChecker) BindRouter ¶
func (h *HeartbeatChecker) BindRouter(msgID uint32, router ziface.IRouter)
func (*HeartbeatChecker) Clone ¶
func (h *HeartbeatChecker) Clone() ziface.IHeartbeatChecker
CloneTo 克隆到一个指定的链接上
func (*HeartbeatChecker) MsgID ¶
func (h *HeartbeatChecker) MsgID() uint32
func (*HeartbeatChecker) Router ¶
func (h *HeartbeatChecker) Router() ziface.IRouter
func (*HeartbeatChecker) SendHeartBeatMsg ¶
func (h *HeartbeatChecker) SendHeartBeatMsg() error
func (*HeartbeatChecker) SetHeartbeatFunc ¶
func (h *HeartbeatChecker) SetHeartbeatFunc(beatFunc ziface.HeartBeatFunc)
func (*HeartbeatChecker) SetHeartbeatMsgFunc ¶
func (h *HeartbeatChecker) SetHeartbeatMsgFunc(f ziface.HeartBeatMsgFunc)
func (*HeartbeatChecker) SetOnRemoteNotAlive ¶
func (h *HeartbeatChecker) SetOnRemoteNotAlive(f ziface.OnRemoteNotAlive)
type HeatBeatDefaultRouter ¶
type HeatBeatDefaultRouter struct {
BaseRouter
}
收到remote心跳消息的默认回调路由业务
func (*HeatBeatDefaultRouter) Handle ¶
func (r *HeatBeatDefaultRouter) Handle(req ziface.IRequest)
Handle -
type MsgHandle ¶
type MsgHandle struct { Apis map[uint32]ziface.IRouter // 存放每个MsgID 所对应的处理方法的map属性 WorkerPoolSize uint32 // 业务工作Worker池的数量 TaskQueue []chan ziface.IRequest // Worker负责取任务的消息队列 // contains filtered or unexported fields }
MsgHandle 对消息的处理回调模块
func (*MsgHandle) AddInterceptor ¶
func (mh *MsgHandle) AddInterceptor(interceptor ziface.IInterceptor)
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)
Server的服务Option
func WithPacket ¶
只要实现Packet 接口可自由实现数据包解析格式,如果没有则使用默认解析格式
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request 请求
func (*Request) BindRouter ¶
func (*Request) GetConnection ¶
func (r *Request) GetConnection() ziface.IConnection
GetConnection 获取请求连接信息
func (*Request) GetResponse ¶
func (*Request) Goto ¶
func (r *Request) Goto(step ziface.HandleStep)
func (*Request) SetResponse ¶
type Server ¶
type Server struct { //服务器的名称 Name string //tcp4 or other IPVersion string //服务绑定的IP地址 IP string //服务绑定的端口 Port int //当前Server的链接管理器 ConnMgr ziface.IConnManager // contains filtered or unexported fields }
Server 接口实现,定义一个Server服务类
func (*Server) AddInterceptor ¶
func (s *Server) AddInterceptor(interceptor ziface.IInterceptor)
func (*Server) GetHeartBeat ¶
func (s *Server) GetHeartBeat() ziface.IHeartbeatChecker
func (*Server) GetLengthField ¶
func (s *Server) GetLengthField() *ziface.LengthField
func (*Server) GetMsgHandler ¶
func (s *Server) GetMsgHandler() ziface.IMsgHandle
func (*Server) GetOnConnStart ¶
func (s *Server) GetOnConnStart() func(ziface.IConnection)
GetOnConnStart 得到该Server的连接创建时Hook函数
func (*Server) GetOnConnStop ¶
func (s *Server) GetOnConnStop() func(ziface.IConnection)
得到该Server的连接断开时的Hook函数
func (*Server) SetDecoder ¶
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函数
func (*Server) StartHeartBeat ¶
StartHeartBeat 启动心跳检测 interval 每次发送心跳的时间间隔
func (*Server) StartHeartBeatWithOption ¶
func (s *Server) StartHeartBeatWithOption(interval time.Duration, option *ziface.HeartBeatOption)
StartHeartBeatWithFunc 启动心跳检测 option 心跳检测的配置
type WsConnection ¶
type WsConnection struct {
// contains filtered or unexported fields
}
WsConnection Websocket连接模块 用于处理 Websocket 连接的读写业务 一个连接对应一个Connection
func (*WsConnection) Context ¶
func (c *WsConnection) Context() context.Context
返回ctx,用于用户自定义的go程获取连接退出状态
func (*WsConnection) GetConnection ¶
func (c *WsConnection) GetConnection() net.Conn
func (*WsConnection) GetProperty ¶
func (c *WsConnection) GetProperty(key string) (interface{}, error)
GetProperty 获取链接属性
func (*WsConnection) GetTCPConnection
deprecated
func (c *WsConnection) GetTCPConnection() net.Conn
Deprecated: use GetConnection instead
func (*WsConnection) GetWsConn ¶
func (c *WsConnection) GetWsConn() *websocket.Conn
func (*WsConnection) IsAlive ¶
func (c *WsConnection) IsAlive() bool
func (*WsConnection) RemoteAddr ¶
func (c *WsConnection) RemoteAddr() net.Addr
RemoteAddr 获取链接远程地址信息
func (*WsConnection) RemoveProperty ¶
func (c *WsConnection) RemoveProperty(key string)
RemoveProperty 移除链接属性
func (*WsConnection) Send ¶
func (c *WsConnection) Send(data []byte) error
func (*WsConnection) SendBuffMsg ¶
func (c *WsConnection) SendBuffMsg(msgID uint32, data []byte) error
SendBuffMsg 发生BuffMsg
func (*WsConnection) SendMsg ¶
func (c *WsConnection) SendMsg(msgID uint32, data []byte) error
SendMsg 直接将Message数据发送数据给远程的TCP客户端
func (*WsConnection) SendToQueue ¶
func (c *WsConnection) SendToQueue(data []byte) error
func (*WsConnection) SetHeartBeat ¶
func (c *WsConnection) SetHeartBeat(checker ziface.IHeartbeatChecker)
func (*WsConnection) SetProperty ¶
func (c *WsConnection) SetProperty(key string, value interface{})
SetProperty 设置链接属性
func (*WsConnection) StartReader ¶
func (c *WsConnection) StartReader()
StartReader 读消息Goroutine,用于从客户端中读取数据
func (*WsConnection) StartWriter ¶
func (c *WsConnection) StartWriter()
StartWriter 写消息Goroutine, 用户将数据发送给客户端