Documentation
¶
Overview ¶
服务端Server的链接模块
Index ¶
- Constants
- Variables
- func NewClient(ip string, port int, opts ...ClientOption) ziface.IClient
- func NewServer(opts ...Option) ziface.IServer
- func NewUserConfServer(config *utils.Config, opts ...Option) ziface.IServer
- type BaseRouter
- type Client
- func (this *Client) AddInterceptor(interceptor ziface.Interceptor)
- func (c *Client) AddRouter(msgID uint32, router ziface.IRouter)
- func (c *Client) Conn() ziface.IConnection
- func (this *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 (this *Client) SetLengthField(field ziface.LengthField)
- 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 uint32) (ziface.IConnection, error)
- func (connMgr *ConnManager) GetAllConnID() []uint32
- 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) GetConnection() net.Conn
- func (c *Connection) GetProperty(key string) (interface{}, error)
- 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) SetProperty(key string, value interface{})
- func (c *Connection) Start()
- func (c *Connection) StartReader()
- func (c *Connection) StartWriter()
- func (c *Connection) Stop()
- type HeartbeatChecker
- type HeatBeatDefaultRouter
- type MsgHandle
- func (this *MsgHandle) AddInterceptor(interceptor ziface.Interceptor)
- func (mh *MsgHandle) AddRouter(msgID uint32, router ziface.IRouter)
- func (mh *MsgHandle) Decode(request ziface.IRequest)
- func (mh *MsgHandle) DoMsgHandler(request ziface.IRequest)
- func (this *MsgHandle) Intercept(chain ziface.Chain) ziface.Response
- 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.Response
- func (r *Request) Goto(step ziface.HandleStep)
- func (r *Request) SetResponse(response ziface.Response)
- type Server
- func (this *Server) AddInterceptor(interceptor ziface.Interceptor)
- func (s *Server) AddRouter(msgID uint32, router ziface.IRouter)
- func (s *Server) GetConnMgr() ziface.IConnManager
- func (this *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 (this *Server) SetLengthField(field ziface.LengthField)
- 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()
Constants ¶
const ( PRE_HANDLE ziface.HandleStep = iota //PreHandle 预处理 HANDLE //Handle 处理 POST_HANDLE //PostHandle 后处理 HANDLE_OVER )
Variables ¶
var AcceptDelay *acceptDelay
Functions ¶
Types ¶
type Client ¶ added in v1.1.0
type Client struct {
//目标链接服务器的IP
Ip string
//目标链接服务器的端口
Port int
//断粘包解码器
LengthField ziface.LengthField
// contains filtered or unexported fields
}
func (*Client) AddInterceptor ¶ added in v1.1.1
func (this *Client) AddInterceptor(interceptor ziface.Interceptor)
func (*Client) Conn ¶ added in v1.1.0
func (c *Client) Conn() ziface.IConnection
func (*Client) GetLengthField ¶ added in v1.1.1
func (this *Client) GetLengthField() ziface.LengthField
func (*Client) GetMsgHandler ¶ added in v1.1.0
func (c *Client) GetMsgHandler() ziface.IMsgHandle
func (*Client) GetOnConnStart ¶ added in v1.1.0
func (c *Client) GetOnConnStart() func(ziface.IConnection)
GetOnConnStart 得到该Server的连接创建时Hook函数
func (*Client) GetOnConnStop ¶ added in v1.1.0
func (c *Client) GetOnConnStop() func(ziface.IConnection)
得到该Server的连接断开时的Hook函数
func (*Client) SetLengthField ¶ added in v1.1.2
func (this *Client) SetLengthField(field ziface.LengthField)
func (*Client) SetOnConnStart ¶ added in v1.1.0
func (c *Client) SetOnConnStart(hookFunc func(ziface.IConnection))
设置该Client的连接创建时Hook函数
func (*Client) SetOnConnStop ¶ added in v1.1.0
func (c *Client) SetOnConnStop(hookFunc func(ziface.IConnection))
设置该Client的连接断开时的Hook函数
func (*Client) StartHeartBeat ¶ added in v1.1.0
启动心跳检测
func (*Client) StartHeartBeatWithOption ¶ added in v1.1.0
func (c *Client) StartHeartBeatWithOption(interval time.Duration, option *ziface.HeartBeatOption)
启动心跳检测(自定义回调)
type ClientOption ¶ added in v1.1.0
type ClientOption func(c *Client)
Client的客户端Option
func WithPacketClient ¶ added in v1.1.0
func WithPacketClient(pack ziface.IDataPack) ClientOption
Client的客户端Option
type ConnManager ¶
type ConnManager struct {
// contains filtered or unexported fields
}
ConnManager 连接管理模块
func (*ConnManager) Get ¶
func (connMgr *ConnManager) Get(connID uint32) (ziface.IConnection, error)
Get 利用ConnID获取链接
func (*ConnManager) GetAllConnID ¶ added in v1.1.0
func (connMgr *ConnManager) GetAllConnID() []uint32
GetAllConnID 获取所有连接的ID
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 ¶ added in v1.0.1
func (c *Connection) Context() context.Context
返回ctx,用于用户自定义的go程获取连接退出状态
func (*Connection) GetConnection ¶ added in v1.1.0
func (c *Connection) GetConnection() net.Conn
func (*Connection) GetProperty ¶
func (c *Connection) GetProperty(key string) (interface{}, error)
GetProperty 获取链接属性
func (*Connection) IsAlive ¶ added in v1.1.0
func (c *Connection) IsAlive() bool
func (*Connection) LocalAddr ¶ added in v1.1.0
func (c *Connection) LocalAddr() net.Addr
LocalAddr 获取链接本地地址信息
func (*Connection) RemoveProperty ¶
func (c *Connection) RemoveProperty(key string)
RemoveProperty 移除链接属性
func (*Connection) Send ¶ added in v1.1.1
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 ¶ added in v1.1.1
func (c *Connection) SendToQueue(data []byte) error
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 ¶ added in v1.1.0
type HeartbeatChecker struct {
// contains filtered or unexported fields
}
func NewHeartbeatCheckerC ¶ added in v1.1.0
func NewHeartbeatCheckerC(interval time.Duration, client ziface.IClient) *HeartbeatChecker
NewHeartbeatCheckerC Client创建心跳检测器
func NewHeartbeatCheckerS ¶ added in v1.1.0
func NewHeartbeatCheckerS(interval time.Duration, server ziface.IServer) *HeartbeatChecker
NewHeartbeatCheckerS Server创建心跳检测器
func (*HeartbeatChecker) BindRouter ¶ added in v1.1.0
func (h *HeartbeatChecker) BindRouter(msgID uint32, router ziface.IRouter)
func (*HeartbeatChecker) SetHeartbeatMsgFunc ¶ added in v1.1.0
func (h *HeartbeatChecker) SetHeartbeatMsgFunc(f ziface.HeartBeatMsgFunc)
func (*HeartbeatChecker) SetOnRemoteNotAlive ¶ added in v1.1.0
func (h *HeartbeatChecker) SetOnRemoteNotAlive(f ziface.OnRemoteNotAlive)
type HeatBeatDefaultRouter ¶ added in v1.1.0
type HeatBeatDefaultRouter struct {
BaseRouter
}
收到remote心跳消息的默认回调路由业务
func (*HeatBeatDefaultRouter) Handle ¶ added in v1.1.0
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 ¶ added in v1.1.1
func (this *MsgHandle) AddInterceptor(interceptor ziface.Interceptor)
func (*MsgHandle) DoMsgHandler ¶
DoMsgHandler 马上以非阻塞方式处理消息
func (*MsgHandle) SendMsgToTaskQueue ¶
SendMsgToTaskQueue 将消息交给TaskQueue,由worker进行处理
func (*MsgHandle) StartOneWorker ¶
StartOneWorker 启动一个Worker工作流程
func (*MsgHandle) StartWorkerPool ¶
func (mh *MsgHandle) StartWorkerPool()
StartWorkerPool 启动worker工作池
type Option ¶ added in v1.0.1
type Option func(s *Server)
Server的服务Option
func WithPacket ¶ added in v1.0.1
只要实现Packet 接口可自由实现数据包解析格式,如果没有则使用默认解析格式
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request 请求
func NewRequest ¶ added in v1.0.2
func NewRequest(conn ziface.IConnection, msg ziface.IMessage) *Request
func (*Request) BindRouter ¶ added in v1.0.2
func (*Request) GetConnection ¶
func (r *Request) GetConnection() ziface.IConnection
GetConnection 获取请求连接信息
func (*Request) GetMessage ¶ added in v1.1.1
GetMessage 获取消息实体
func (*Request) GetResponse ¶ added in v1.1.1
func (*Request) Goto ¶ added in v1.0.2
func (r *Request) Goto(step ziface.HandleStep)
func (*Request) SetResponse ¶ added in v1.1.1
type Server ¶
type Server struct {
//服务器的名称
Name string
//tcp4 or other
IPVersion string
//服务绑定的IP地址
IP string
//服务绑定的端口
Port int
//当前Server的链接管理器
ConnMgr ziface.IConnManager
//断粘包解码器
LengthField ziface.LengthField
// contains filtered or unexported fields
}
Server 接口实现,定义一个Server服务类
func (*Server) AddInterceptor ¶ added in v1.1.1
func (this *Server) AddInterceptor(interceptor ziface.Interceptor)
func (*Server) GetLengthField ¶ added in v1.1.1
func (this *Server) GetLengthField() ziface.LengthField
func (*Server) GetMsgHandler ¶ added in v1.1.0
func (s *Server) GetMsgHandler() ziface.IMsgHandle
func (*Server) GetOnConnStart ¶ added in v1.1.0
func (s *Server) GetOnConnStart() func(ziface.IConnection)
GetOnConnStart 得到该Server的连接创建时Hook函数
func (*Server) GetOnConnStop ¶ added in v1.1.0
func (s *Server) GetOnConnStop() func(ziface.IConnection)
得到该Server的连接断开时的Hook函数
func (*Server) SetLengthField ¶ added in v1.1.2
func (this *Server) SetLengthField(field ziface.LengthField)
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 ¶ added in v1.1.0
StartHeartBeat 启动心跳检测 interval 每次发送心跳的时间间隔 msgID 心跳检测消息的msgID
func (*Server) StartHeartBeatWithOption ¶ added in v1.1.0
func (s *Server) StartHeartBeatWithOption(interval time.Duration, option *ziface.HeartBeatOption)
StartHeartBeatWithFunc 启动心跳检测 msgFunc 心跳检测消息的自定义生成函数,不需要自定义可以传nil notAlive 检测到地方停止心跳的自定义处理函数,不需要自定义可以传nil