Documentation ¶
Index ¶
- Constants
- Variables
- func HeatBeatDefaultHandle(req IRequest)
- func PathExists(path string) (bool, error)
- func PrintLogo()
- func RouterRecovery(request IRequest)
- func RouterTime(request IRequest)
- func UserConfToGlobal(config *Config)
- type Chain
- type Client
- func (c *Client) AddInterceptor(interceptor IInterceptor)
- func (c *Client) AddRouter(msgID uint32, handlers ...RouterHandler)
- func (c *Client) Conn() IConnection
- func (c *Client) GetErrChan() chan error
- func (c *Client) GetLengthField() *LengthField
- func (c *Client) GetMsgHandler() IMsgHandle
- func (c *Client) GetName() string
- func (c *Client) GetOnConnStart() func(IConnection)
- func (c *Client) GetOnConnStop() func(IConnection)
- func (c *Client) GetPacket() IDataPack
- func (c *Client) Restart()
- func (c *Client) SetDecoder(decoder IDecoder)
- func (c *Client) SetName(name string)
- func (c *Client) SetOnConnStart(hookFunc func(IConnection))
- func (c *Client) SetOnConnStop(hookFunc func(IConnection))
- func (c *Client) SetPacket(packet IDataPack)
- func (c *Client) Start()
- func (c *Client) StartHeartBeatWithOption(interval time.Duration, option *HeartbeatOption)
- func (c *Client) StartHeartbeat(interval time.Duration)
- func (c *Client) Stop()
- type ClientOption
- type Config
- type ConnManager
- func (connMgr *ConnManager) Add(conn IConnection)
- func (connMgr *ConnManager) ClearConn()
- func (connMgr *ConnManager) Get(connID uint64) (IConnection, error)
- func (connMgr *ConnManager) GetAllConnID() []uint64
- func (connMgr *ConnManager) Len() int
- func (connMgr *ConnManager) Range(cb func(uint64, IConnection, interface{}) error, args interface{}) (err error)
- func (connMgr *ConnManager) Remove(conn IConnection)
- type Connection
- func (c *Connection) Context() context.Context
- func (c *Connection) GetConnID() uint64
- func (c *Connection) GetConnection() net.Conn
- func (c *Connection) GetMsgHandler() IMsgHandle
- func (c *Connection) GetName() string
- func (c *Connection) GetProperty(key string) (any, error)
- func (c *Connection) GetWorkerID() uint32
- func (c *Connection) GetWsConn() *websocket.Conn
- func (c *Connection) IsAlive() bool
- func (c *Connection) LocalAddr() net.Addr
- func (c *Connection) LocalAddrString() string
- func (c *Connection) RemoteAddr() net.Addr
- func (c *Connection) RemoteAddrString() string
- 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 IHeartbeatChecker)
- func (c *Connection) SetProperty(key string, value any)
- func (c *Connection) Start()
- func (c *Connection) StartReader()
- func (c *Connection) StartWriter()
- func (c *Connection) Stop()
- type DataPack
- type DataPackLtv
- type FrameDecoder
- type HeartbeatChecker
- func (h *HeartbeatChecker) BindConn(conn IConnection)
- func (h *HeartbeatChecker) BindRouter(msgID uint32, handlers ...RouterHandler)
- func (h *HeartbeatChecker) Clone() IHeartbeatChecker
- func (h *HeartbeatChecker) MsgID() uint32
- func (h *HeartbeatChecker) RouterHandlers() []RouterHandler
- func (h *HeartbeatChecker) SendHeartbeatMsg() error
- func (h *HeartbeatChecker) SetHeartbeatFunc(beatFunc HeartbeatFunc)
- func (h *HeartbeatChecker) SetHeartbeatMsgFunc(f HeartbeatMsgFunc)
- func (h *HeartbeatChecker) SetOnRemoteNotAlive(f OnRemoteNotAlive)
- func (h *HeartbeatChecker) Start()
- func (h *HeartbeatChecker) Stop()
- type HeartbeatFunc
- type HeartbeatMsgFunc
- type HeartbeatOption
- type IChain
- type IClient
- type IConnManager
- type IConnection
- type IDataPack
- type IDecoder
- type IFrameDecoder
- type IFuncRequest
- type IHeartbeatChecker
- type IInterceptor
- type IMessage
- type IMsgHandle
- type IRequest
- type IRouter
- type IServer
- type IcReq
- type IcResp
- type LengthField
- type LtvLittleDecoder
- type Message
- func (msg *Message) GetData() []byte
- func (msg *Message) GetDataLen() uint32
- func (msg *Message) GetMsgID() uint32
- func (msg *Message) GetRawData() []byte
- func (msg *Message) Init(ID uint32, data []byte)
- func (msg *Message) SetData(data []byte)
- func (msg *Message) SetDataLen(len uint32)
- func (msg *Message) SetMsgID(msgID uint32)
- type MsgHandle
- func (mh *MsgHandle) AddInterceptor(interceptor IInterceptor)
- func (mh *MsgHandle) AddRouter(msgId uint32, handler ...RouterHandler) IRouter
- func (mh *MsgHandle) Execute(request IRequest)
- func (mh *MsgHandle) Intercept(chain IChain) IcResp
- func (mh *MsgHandle) SendMsgToTaskQueue(request IRequest)
- func (mh *MsgHandle) StartOneWorker(workerID int, taskQueue chan IRequest)
- func (mh *MsgHandle) StartWorkerPool()
- func (mh *MsgHandle) Use(handlers ...RouterHandler) IRouter
- func (mh *MsgHandle) UseGateway(handler RouterHandler)
- type NetDataPack
- type NetDecoder
- type OnRemoteNotAlive
- type Option
- type PackFactory
- type Request
- func (r *Request) Abort()
- func (r *Request) BindRouter(handlers []RouterHandler)
- func (r *Request) GetConnection() IConnection
- func (r *Request) GetData() []byte
- func (r *Request) GetMessage() IMessage
- func (r *Request) GetMsgID() uint32
- func (r *Request) GetResponse() IcResp
- func (r *Request) RouterNext()
- func (r *Request) SetResponse(response IcResp)
- type Router
- type RouterHandler
- type Server
- func (s *Server) AddInterceptor(interceptor IInterceptor)
- func (s *Server) AddRouter(msgID uint32, router ...RouterHandler) IRouter
- func (s *Server) GetConnMgr() IConnManager
- func (s *Server) GetHeartbeat() IHeartbeatChecker
- func (s *Server) GetLengthField() *LengthField
- func (s *Server) GetMsgHandler() IMsgHandle
- func (s *Server) GetOnConnStart() func(IConnection)
- func (s *Server) GetOnConnStop() func(IConnection)
- func (s *Server) GetPacket() IDataPack
- func (s *Server) ListenTcpConn()
- func (s *Server) ListenWebsocketConn()
- func (s *Server) Serve()
- func (s *Server) ServerName() string
- func (s *Server) SetDecoder(decoder IDecoder)
- func (s *Server) SetOnConnStart(hookFunc func(IConnection))
- func (s *Server) SetOnConnStop(hookFunc func(IConnection))
- func (s *Server) SetPacket(packet IDataPack)
- func (s *Server) SetWebsocketAuth(f func(r *http.Request) error)
- func (s *Server) Start()
- func (s *Server) StartConn(conn IConnection)
- func (s *Server) StartHeartbeat(interval time.Duration)
- func (s *Server) StartHeartbeatWithOption(interval time.Duration, option *HeartbeatOption)
- func (s *Server) Stop()
- func (s *Server) Use(Handlers ...RouterHandler) IRouter
- func (s *Server) UseGateway(handler RouterHandler)
- type TLVDecoder
- type WsConnection
- func (c *WsConnection) Context() context.Context
- func (c *WsConnection) GetConnID() uint64
- func (c *WsConnection) GetConnection() net.Conn
- func (c *WsConnection) GetMsgHandler() IMsgHandle
- func (c *WsConnection) GetName() string
- func (c *WsConnection) GetProperty(key string) (any, error)
- func (c *WsConnection) GetWorkerID() uint32
- func (c *WsConnection) GetWsConn() *websocket.Conn
- func (c *WsConnection) IsAlive() bool
- func (c *WsConnection) LocalAddr() net.Addr
- func (c *WsConnection) LocalAddrString() string
- func (c *WsConnection) RemoteAddr() net.Addr
- func (c *WsConnection) RemoteAddrString() string
- 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 IHeartbeatChecker)
- func (c *WsConnection) SetProperty(key string, value any)
- func (c *WsConnection) Start()
- func (c *WsConnection) StartReader()
- func (c *WsConnection) StartWriter()
- func (c *WsConnection) Stop()
Constants ¶
const ( ServerModeTcp = "tcp" ServerModeWebsocket = "websocket" WorkerModeBind = "Bind" // 为每个连接分配一个worker )
const ( StackBegin = 3 // 开始追踪堆栈信息的层数 StackEnd = 5 // 追踪到最后的层数 )
const (
HeartbeatDefaultMsgID uint32 = 99999
)
const LtvHeaderSize = 8 //表示TLV空包长度
const TlvHeaderSize = 8 // 表示TLV空包长度
const ( // WorkerIDWithoutWorkerPool (如果不启动Worker协程池,则会给MsgHandler分配一个虚拟的WorkerID,这个workerID为0, 便于指标统计 WorkerIDWithoutWorkerPool int = 0 )
Variables ¶
var AcceptDelay *acceptDelay
Functions ¶
func HeatBeatDefaultHandle ¶
func HeatBeatDefaultHandle(req IRequest)
func RouterRecovery ¶
func RouterRecovery(request IRequest)
RouterRecovery 用来存放一些RouterSlicesMode下的路由可用的默认中间件 如果使用NewDefaultRouterSlicesServer方法初始化的获得的server将自带这个函数 作用是接收业务执行上产生的panic并且尝试记录现场信息
func UserConfToGlobal ¶
func UserConfToGlobal(config *Config)
UserConfToGlobal 注意如果使用UserConf应该调用方法同步至 GlobalConfObject 因为其他参数是调用的此结构体参数
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
func (*Chain) ProceedWithIMessage ¶
ProceedWithIMessage Next 通过IMessage和解码后数据进入下一个责任链任务
func (*Chain) ShouldIRequest ¶
ShouldIRequest 判断是否是IRequest
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) AddInterceptor ¶
func (c *Client) AddInterceptor(interceptor IInterceptor)
func (*Client) AddRouter ¶
func (c *Client) AddRouter(msgID uint32, handlers ...RouterHandler)
func (*Client) Conn ¶
func (c *Client) Conn() IConnection
func (*Client) GetErrChan ¶
func (*Client) GetLengthField ¶
func (c *Client) GetLengthField() *LengthField
func (*Client) GetMsgHandler ¶
func (c *Client) GetMsgHandler() IMsgHandle
func (*Client) GetOnConnStart ¶
func (c *Client) GetOnConnStart() func(IConnection)
func (*Client) GetOnConnStop ¶
func (c *Client) GetOnConnStop() func(IConnection)
func (*Client) SetDecoder ¶
func (*Client) SetOnConnStart ¶
func (c *Client) SetOnConnStart(hookFunc func(IConnection))
func (*Client) SetOnConnStop ¶
func (c *Client) SetOnConnStop(hookFunc func(IConnection))
func (*Client) StartHeartBeatWithOption ¶
func (c *Client) StartHeartBeatWithOption(interval time.Duration, option *HeartbeatOption)
StartHeartBeatWithOption 启动心跳检测(自定义回调)
func (*Client) StartHeartbeat ¶
StartHeartbeat 启动心跳检测, interval: 每次发送心跳的时间间隔
type ClientOption ¶
type ClientOption func(c IClient)
ClientOption Options for Client
func WithNameClient ¶
func WithNameClient(name string) ClientOption
func WithPacketClient ¶
func WithPacketClient(pack IDataPack) ClientOption
type Config ¶
type Config struct { Host string // 当前服务器主机IP TCPPort int // 当前服务器主机监听端口号 WsPort int // 当前服务器主机websocket监听端口 Name string // 当前服务器名称 Version string // 当前版本号 MaxPacketSize uint32 // 读写数据包的最大值 MaxConn int // 当前服务器主机允许的最大链接个数 WorkerPoolSize uint32 // 业务工作Worker池的数量 MaxWorkerTaskLen uint32 // 业务工作Worker对应负责的任务队列最大任务存储数量 WorkerMode string // 为链接分配worker的方式 MaxMsgChanLen uint32 // SendBuffMsg发送消息的缓冲最大长度 IOReadBuffSize uint32 // 每次IO最大的读取长度 Mode string // "tcp":tcp监听, "websocket":websocket 监听 为空时同时开启 LogDir string // 日志所在文件夹 默认"./log" LogFile string // 日志文件名称 默认"" --如果没有设置日志文件,打印信息将打印至stderr LogSaveDays int // 日志最大保留天数 LogFileSize int64 // 日志单个日志最大容量 默认 64MB,单位:字节,记得一定要换算成MB(1024 * 1024) LogCons bool // 日志标准输出 默认 false LogIsolationLevel int // 日志隔离级别 -- 0:全开 1:关debug 2:关debug/info 3:关debug/info/warn ... HeartbeatMax int // 最长心跳检测间隔时间(单位:秒),超过改时间间隔,则认为超时,从配置文件读取 CertFile string // 证书文件名称 默认"" PrivateKeyFile string // 私钥文件名称 默认"" --如果没有设置证书和私钥文件,则不启用TLS加密 }
Config
存储一切有关框架的全局参数,供其他模块使用 一些参数也可以通过 用户根据 xnet.json来配置
var GlobalConfig *Config
func (*Config) HeartbeatMaxDuration ¶
type ConnManager ¶
type ConnManager struct {
// contains filtered or unexported fields
}
func (*ConnManager) Add ¶
func (connMgr *ConnManager) Add(conn IConnection)
func (*ConnManager) ClearConn ¶
func (connMgr *ConnManager) ClearConn()
func (*ConnManager) Get ¶
func (connMgr *ConnManager) Get(connID uint64) (IConnection, error)
func (*ConnManager) GetAllConnID ¶
func (connMgr *ConnManager) GetAllConnID() []uint64
func (*ConnManager) Len ¶
func (connMgr *ConnManager) Len() int
func (*ConnManager) Range ¶
func (connMgr *ConnManager) Range(cb func(uint64, IConnection, interface{}) error, args interface{}) (err error)
func (*ConnManager) Remove ¶
func (connMgr *ConnManager) Remove(conn IConnection)
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection (用于处理Tcp连接的读写业务 一个连接对应一个Connection)
func (*Connection) Context ¶
func (c *Connection) Context() context.Context
func (*Connection) GetConnID ¶
func (c *Connection) GetConnID() uint64
func (*Connection) GetConnection ¶
func (c *Connection) GetConnection() net.Conn
func (*Connection) GetMsgHandler ¶
func (c *Connection) GetMsgHandler() IMsgHandle
func (*Connection) GetName ¶
func (c *Connection) GetName() string
func (*Connection) GetProperty ¶
func (c *Connection) GetProperty(key string) (any, error)
func (*Connection) GetWorkerID ¶
func (c *Connection) GetWorkerID() uint32
func (*Connection) GetWsConn ¶
func (c *Connection) GetWsConn() *websocket.Conn
func (*Connection) IsAlive ¶
func (c *Connection) IsAlive() bool
func (*Connection) LocalAddr ¶
func (c *Connection) LocalAddr() net.Addr
func (*Connection) LocalAddrString ¶
func (c *Connection) LocalAddrString() string
func (*Connection) RemoteAddr ¶
func (c *Connection) RemoteAddr() net.Addr
func (*Connection) RemoteAddrString ¶
func (c *Connection) RemoteAddrString() string
func (*Connection) RemoveProperty ¶
func (c *Connection) RemoveProperty(key string)
func (*Connection) Send ¶
func (c *Connection) Send(data []byte) error
func (*Connection) SendBuffMsg ¶
func (c *Connection) SendBuffMsg(msgID uint32, data []byte) error
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 IHeartbeatChecker)
func (*Connection) SetProperty ¶
func (c *Connection) SetProperty(key string, value any)
func (*Connection) StartReader ¶
func (c *Connection) StartReader()
StartReader (读消息Goroutine,用于从客户端中读取数据)
func (*Connection) StartWriter ¶
func (c *Connection) StartWriter()
StartWriter 写消息Goroutine, 用户将数据发送给客户端
type FrameDecoder ¶
type FrameDecoder struct { LengthField //从ILengthField集成的基础属性 LengthFieldEndOffset int //长度字段结束位置的偏移量 LengthFieldOffset+LengthFieldLength // contains filtered or unexported fields }
func (*FrameDecoder) Decode ¶
func (d *FrameDecoder) Decode(buff []byte) [][]byte
type HeartbeatChecker ¶
type HeartbeatChecker struct {
// contains filtered or unexported fields
}
func (*HeartbeatChecker) BindConn ¶
func (h *HeartbeatChecker) BindConn(conn IConnection)
func (*HeartbeatChecker) BindRouter ¶
func (h *HeartbeatChecker) BindRouter(msgID uint32, handlers ...RouterHandler)
func (*HeartbeatChecker) Clone ¶
func (h *HeartbeatChecker) Clone() IHeartbeatChecker
Clone 克隆到一个指定的链接上
func (*HeartbeatChecker) MsgID ¶
func (h *HeartbeatChecker) MsgID() uint32
func (*HeartbeatChecker) RouterHandlers ¶
func (h *HeartbeatChecker) RouterHandlers() []RouterHandler
func (*HeartbeatChecker) SendHeartbeatMsg ¶
func (h *HeartbeatChecker) SendHeartbeatMsg() error
func (*HeartbeatChecker) SetHeartbeatFunc ¶
func (h *HeartbeatChecker) SetHeartbeatFunc(beatFunc HeartbeatFunc)
func (*HeartbeatChecker) SetHeartbeatMsgFunc ¶
func (h *HeartbeatChecker) SetHeartbeatMsgFunc(f HeartbeatMsgFunc)
func (*HeartbeatChecker) SetOnRemoteNotAlive ¶
func (h *HeartbeatChecker) SetOnRemoteNotAlive(f OnRemoteNotAlive)
func (*HeartbeatChecker) Start ¶
func (h *HeartbeatChecker) Start()
func (*HeartbeatChecker) Stop ¶
func (h *HeartbeatChecker) Stop()
type HeartbeatMsgFunc ¶
type HeartbeatMsgFunc func(IConnection) []byte
HeartbeatMsgFunc 用户自定义的心跳检测消息处理方法
type HeartbeatOption ¶
type HeartbeatOption struct { MakeMsg HeartbeatMsgFunc // 用户自定义的心跳检测消息处理方法 OnRemoteNotAlive OnRemoteNotAlive // 用户自定义的远程连接不存活时的处理方法 HeartbeatMsgID uint32 // 用户自定义的心跳检测消息ID RouterHandlers []RouterHandler // 路由处理函数的集合 }
type IChain ¶
type IClient ¶
type IClient interface { Restart() Start() Stop() AddRouter(uint32, ...RouterHandler) Conn() IConnection // SetOnConnStart 设置该Client的连接创建时Hook函数 SetOnConnStart(func(IConnection)) // SetOnConnStop 设置该Client的连接断开时的Hook函数 SetOnConnStop(func(IConnection)) // GetOnConnStart 获取该Client的连接创建时Hook函数 GetOnConnStart() func(IConnection) // GetOnConnStop 设置该Client的连接断开时的Hook函数 GetOnConnStop() func(IConnection) // GetPacket 获取Client绑定的数据协议封包方式 GetPacket() IDataPack // SetPacket 设置Client绑定的数据协议封包方式 SetPacket(IDataPack) // GetMsgHandler 获取Client绑定的消息处理模块 GetMsgHandler() IMsgHandle // StartHeartbeat Start 启动心跳检测 StartHeartbeat(time.Duration) // StartHeartBeatWithOption 自定义回调 StartHeartBeatWithOption(time.Duration, *HeartbeatOption) // GetLengthField Get the length field of this Client GetLengthField() *LengthField // SetDecoder 设置解码器 SetDecoder(IDecoder) // AddInterceptor 添加拦截器 AddInterceptor(IInterceptor) // GetErrChan 获取客户端错误管道 GetErrChan() chan error // SetName 设置客户端Client名称 SetName(string) // GetName 获取客户端Client名称 GetName() string }
func NewTLSClient ¶
func NewTLSClient(ip string, port int, opts ...ClientOption) IClient
func NewWsClient ¶
func NewWsClient(ip string, port int, opts ...ClientOption) IClient
type IConnManager ¶
type IConnManager interface { Add(IConnection) // Add connection Remove(IConnection) // Remove connection Get(uint64) (IConnection, error) // Get a connection by ConnID Len() int // Get current number of connections ClearConn() // Remove and stop all connections GetAllConnID() []uint64 // Get all connection IDs Range(func(uint64, IConnection, interface{}) error, interface{}) error // Traverse all connections }
type IConnection ¶
type IConnection interface { Start() // Start 启动连接,让当前连接开始工作 Stop() // Stop 停止连接,结束当前连接状态 Context() context.Context // Context 返回ctx,用于用户自定义的go程获取连接退出状态 GetName() string // 获取当前连接名称 GetConnection() net.Conn // 从当前连接获取原始的socket GetWsConn() *websocket.Conn // 从当前连接中获取原始的websocket连接 GetConnID() uint64 // 获取当前连接ID GetMsgHandler() IMsgHandle // 获取消息处理器 GetWorkerID() uint32 // 获取workerId RemoteAddr() net.Addr // 获取链接远程地址信息 LocalAddr() net.Addr // 获取链接本地地址信息 RemoteAddrString() string // 获取链接远程地址信息 LocalAddrString() string // 获取链接本地地址信息 Send([]byte) error // Send 直接发送数据 SendToQueue([]byte) error // Send 发送到队列 SendMsg(uint32, []byte) error // 直接将Message数据发送数据给远程的TCP客户端(无缓冲) SendBuffMsg(uint32, []byte) error // 直接将Message数据发送给远程的TCP客户端(有缓冲) SetProperty(string, any) // Set connection property GetProperty(string) (any, error) // Get connection property RemoveProperty(string) // Remove connection property IsAlive() bool // 判断当前连接是否存活 SetHeartbeat(checker IHeartbeatChecker) // 设置心跳检测器 }
type IDataPack ¶
type IDecoder ¶
type IDecoder interface { IInterceptor GetLengthField() *LengthField }
func NewLTVLittleDecoder ¶
func NewLTVLittleDecoder() IDecoder
func NewTLVDecoder ¶
func NewTLVDecoder() IDecoder
type IFrameDecoder ¶
func NewFrameDecoder ¶
func NewFrameDecoder(lf LengthField) IFrameDecoder
func NewFrameDecoderByParams ¶
func NewFrameDecoderByParams(maxFrameLength uint64, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip int) IFrameDecoder
type IFuncRequest ¶
type IFuncRequest interface {
CallFunc()
}
type IHeartbeatChecker ¶
type IHeartbeatChecker interface { SetOnRemoteNotAlive(OnRemoteNotAlive) SetHeartbeatMsgFunc(HeartbeatMsgFunc) SetHeartbeatFunc(HeartbeatFunc) BindRouter(uint32, ...RouterHandler) Start() Stop() SendHeartbeatMsg() error BindConn(IConnection) Clone() IHeartbeatChecker MsgID() uint32 RouterHandlers() []RouterHandler }
func NewHeartbeatChecker ¶
func NewHeartbeatChecker(interval time.Duration) IHeartbeatChecker
type IInterceptor ¶
type IMsgHandle ¶
type IMsgHandle interface { AddRouter(uint32, ...RouterHandler) IRouter Use(...RouterHandler) IRouter UseGateway(RouterHandler) StartWorkerPool() SendMsgToTaskQueue(IRequest) // 将消息交给TaskQueue,由worker进行处理 Execute(IRequest) // 执行责任链上的拦截器方法 AddInterceptor(IInterceptor) // 注册责任链任务入口,每个拦截器处理完后,数据都会传递至下一个拦截器,使得消息可以层层处理层层传递,顺序取决于注册顺序 }
type IRequest ¶
type IRequest interface { GetConnection() IConnection // 获取请求连接信息 GetData() []byte // 获取请求消息的数据 GetMsgID() uint32 // 获取请求的消息ID GetMessage() IMessage // 获取请求消息的原始数据 GetResponse() IcResp // 获取解析完后序列化数据 SetResponse(IcResp) // 设置解析完后序列化数据 Abort() // 终止处理函数的运行 但调用此方法的函数会执行完毕 BindRouter([]RouterHandler) // 绑定这次请求由哪个路由处理 RouterNext() // 执行下一个函数 }
func NewRequest ¶
func NewRequest(conn IConnection, msg IMessage) IRequest
type IRouter ¶
type IRouter interface { Use(...RouterHandler) // 添加全局组件 AddHandler(uint32, ...RouterHandler) // 添加业务处理器集合 GetHandlers(uint32) ([]RouterHandler, bool) // 获得当前的所有注册在MsgId的处理器集合 }
type IServer ¶
type IServer interface { Start() // 启动服务器方法 Stop() // 停止服务器方法 Serve() // 开启业务服务方法 AddRouter(uint32, ...RouterHandler) IRouter // 路由功能 Use(...RouterHandler) IRouter // 公共组件管理 UseGateway(RouterHandler) // 使用网关 GetConnMgr() IConnManager // 得到链接管理 SetOnConnStart(func(IConnection)) // 设置该Server的连接创建时Hook函数 SetOnConnStop(func(IConnection)) // 设置该Server的连接断开时的Hook函数 GetOnConnStart() func(IConnection) // 得到该Server的连接创建时Hook函数 GetOnConnStop() func(IConnection) // 得到该Server的连接断开时的Hook函数 GetPacket() IDataPack // 获取Server绑定的数据协议封包方式 GetMsgHandler() IMsgHandle // 获取Server绑定的消息处理模块 SetPacket(IDataPack) // 设置Server绑定的数据协议封包方式 StartHeartbeat(time.Duration) // 启动心跳检测 StartHeartbeatWithOption(time.Duration, *HeartbeatOption) // 启动心跳检测(自定义回调) GetHeartbeat() IHeartbeatChecker // 获取心跳检测器 GetLengthField() *LengthField // SetDecoder(IDecoder) // AddInterceptor(IInterceptor) // SetWebsocketAuth(func(r *http.Request) error) // 添加websocket认证方法 ServerName() string // 获取服务器名称 }
type LengthField ¶
type LengthField struct { /* 大端模式:是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中,地址由小向大增加,而数据从高位往低位放; 小端模式:是指数据的高字节保存在内存的高地址中,而数据的低字节保存在内存的低地址中,高地址部分权值高,低地址部分权值低,和我们的日常逻辑方法一致。 */ Order binary.ByteOrder // 大小端 MaxFrameLength uint64 // 最大帧长度 LengthFieldOffset int // 长度字段偏移量 LengthFieldLength int // 长度域字段的字节数 LengthAdjustment int // 长度调整 InitialBytesToStrip int // 需要跳过的字节数 }
LengthField 具备的基础属性
type LtvLittleDecoder ¶
func (*LtvLittleDecoder) GetLengthField ¶
func (ltv *LtvLittleDecoder) GetLengthField() *LengthField
func (*LtvLittleDecoder) Intercept ¶
func (ltv *LtvLittleDecoder) Intercept(chain IChain) IcResp
type Message ¶
type Message struct { DataLen uint32 // Length of the message ID uint32 // ID of the message Data []byte // Content of the message // contains filtered or unexported fields }
Message structure for messages
func NewMessage ¶
func NewMsgPackage ¶
func (*Message) GetDataLen ¶
func (*Message) GetRawData ¶
func (*Message) SetDataLen ¶
type MsgHandle ¶
type MsgHandle struct {
// contains filtered or unexported fields
}
MsgHandle 对消息的处理回调模块
func (*MsgHandle) AddInterceptor ¶
func (mh *MsgHandle) AddInterceptor(interceptor IInterceptor)
func (*MsgHandle) AddRouter ¶
func (mh *MsgHandle) AddRouter(msgId uint32, handler ...RouterHandler) IRouter
AddRouter 路由添加
func (*MsgHandle) SendMsgToTaskQueue ¶
SendMsgToTaskQueue 将消息交给TaskQueue,由worker进行处理
func (*MsgHandle) StartOneWorker ¶
StartOneWorker 启动一个Worker工作流程
func (*MsgHandle) StartWorkerPool ¶
func (mh *MsgHandle) StartWorkerPool()
StartWorkerPool starts the worker pool
func (*MsgHandle) Use ¶
func (mh *MsgHandle) Use(handlers ...RouterHandler) IRouter
func (*MsgHandle) UseGateway ¶
func (mh *MsgHandle) UseGateway(handler RouterHandler)
type OnRemoteNotAlive ¶
type OnRemoteNotAlive func(IConnection)
OnRemoteNotAlive 用户自定义的远程连接不存活时的处理方法
type Option ¶
type Option func(s *Server)
Option Server的服务Option
func WithPacket ¶
WithPacket 只要实现Packet 接口可自由实现数据包解析格式,如果没有则使用默认解析格式
type PackFactory ¶
type PackFactory struct{}
func (*PackFactory) NewDecoder ¶
func (*PackFactory) NewDecoder(kind NetDecoder) IDecoder
NewDecoder 创建一个解码对象
func (*PackFactory) NewPack ¶
func (*PackFactory) NewPack(kind NetDataPack) IDataPack
NewPack 创建一个具体的拆包解包对象
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
func (*Request) BindRouter ¶
func (r *Request) BindRouter(handlers []RouterHandler)
func (*Request) GetConnection ¶
func (r *Request) GetConnection() IConnection
func (*Request) GetMessage ¶
func (*Request) GetResponse ¶
func (*Request) RouterNext ¶
func (r *Request) RouterNext()
func (*Request) SetResponse ¶
type Router ¶
func (*Router) AddHandler ¶
func (r *Router) AddHandler(msgID uint32, handlers ...RouterHandler)
func (*Router) GetHandlers ¶
func (r *Router) GetHandlers(msgID uint32) ([]RouterHandler, bool)
func (*Router) Use ¶
func (r *Router) Use(handlers ...RouterHandler)
type RouterHandler ¶
type RouterHandler func(request IRequest)
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server 接口实现,定义一个Server服务类
func (*Server) AddInterceptor ¶
func (s *Server) AddInterceptor(interceptor IInterceptor)
func (*Server) AddRouter ¶
func (s *Server) AddRouter(msgID uint32, router ...RouterHandler) IRouter
func (*Server) GetConnMgr ¶
func (s *Server) GetConnMgr() IConnManager
func (*Server) GetHeartbeat ¶
func (s *Server) GetHeartbeat() IHeartbeatChecker
func (*Server) GetLengthField ¶
func (s *Server) GetLengthField() *LengthField
func (*Server) GetMsgHandler ¶
func (s *Server) GetMsgHandler() IMsgHandle
func (*Server) GetOnConnStart ¶
func (s *Server) GetOnConnStart() func(IConnection)
func (*Server) GetOnConnStop ¶
func (s *Server) GetOnConnStop() func(IConnection)
func (*Server) ListenTcpConn ¶
func (s *Server) ListenTcpConn()
func (*Server) ListenWebsocketConn ¶
func (s *Server) ListenWebsocketConn()
func (*Server) ServerName ¶
func (*Server) SetDecoder ¶
func (*Server) SetOnConnStart ¶
func (s *Server) SetOnConnStart(hookFunc func(IConnection))
func (*Server) SetOnConnStop ¶
func (s *Server) SetOnConnStop(hookFunc func(IConnection))
func (*Server) StartConn ¶
func (s *Server) StartConn(conn IConnection)
func (*Server) StartHeartbeat ¶
StartHeartbeat 启动心跳检测 interval 每次发送心跳的时间间隔
func (*Server) StartHeartbeatWithOption ¶
func (s *Server) StartHeartbeatWithOption(interval time.Duration, option *HeartbeatOption)
StartHeartbeatWithOption 启动心跳检测 option 心跳检测的配置
func (*Server) Use ¶
func (s *Server) Use(Handlers ...RouterHandler) IRouter
func (*Server) UseGateway ¶
func (s *Server) UseGateway(handler RouterHandler)
type TLVDecoder ¶
func (*TLVDecoder) GetLengthField ¶
func (tlv *TLVDecoder) GetLengthField() *LengthField
func (*TLVDecoder) Intercept ¶
func (tlv *TLVDecoder) Intercept(chain IChain) IcResp
type WsConnection ¶
type WsConnection struct {
// contains filtered or unexported fields
}
WsConnection Websocket连接模块, 用于处理 Websocket 连接的读写业务 一个连接对应一个Connection
func (*WsConnection) Context ¶
func (c *WsConnection) Context() context.Context
Context 返回ctx,用于用户自定义的go程获取连接退出状态
func (*WsConnection) GetConnID ¶
func (c *WsConnection) GetConnID() uint64
func (*WsConnection) GetConnection ¶
func (c *WsConnection) GetConnection() net.Conn
func (*WsConnection) GetMsgHandler ¶
func (c *WsConnection) GetMsgHandler() IMsgHandle
func (*WsConnection) GetName ¶
func (c *WsConnection) GetName() string
func (*WsConnection) GetProperty ¶
func (c *WsConnection) GetProperty(key string) (any, error)
func (*WsConnection) GetWorkerID ¶
func (c *WsConnection) GetWorkerID() uint32
func (*WsConnection) GetWsConn ¶
func (c *WsConnection) GetWsConn() *websocket.Conn
func (*WsConnection) IsAlive ¶
func (c *WsConnection) IsAlive() bool
func (*WsConnection) LocalAddr ¶
func (c *WsConnection) LocalAddr() net.Addr
func (*WsConnection) LocalAddrString ¶
func (c *WsConnection) LocalAddrString() string
func (*WsConnection) RemoteAddr ¶
func (c *WsConnection) RemoteAddr() net.Addr
func (*WsConnection) RemoteAddrString ¶
func (c *WsConnection) RemoteAddrString() string
func (*WsConnection) RemoveProperty ¶
func (c *WsConnection) RemoveProperty(key string)
func (*WsConnection) Send ¶
func (c *WsConnection) Send(data []byte) error
func (*WsConnection) SendBuffMsg ¶
func (c *WsConnection) SendBuffMsg(msgID uint32, data []byte) error
SendBuffMsg sends 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 IHeartbeatChecker)
func (*WsConnection) SetProperty ¶
func (c *WsConnection) SetProperty(key string, value any)
func (*WsConnection) StartReader ¶
func (c *WsConnection) StartReader()
StartReader 读消息Goroutine,用于从客户端中读取数据
func (*WsConnection) StartWriter ¶
func (c *WsConnection) StartWriter()
StartWriter 写消息Goroutine, 用户将数据发送给客户端
Source Files ¶
- accept_delay.go
- chain.go
- chain_builder.go
- client.go
- config.go
- conn_manager.go
- connection.go
- data_pack.go
- data_pack_ltv_little_endian.go
- data_pack_tlv_big_endian.go
- decoder.go
- decoder_ltv_little.go
- decoder_tlv_big.go
- default_router_func.go
- frame_decoder.go
- heartbeat.go
- interceptor.go
- length_field.go
- logo.go
- message.go
- msg_handler.go
- option.go
- pack_factory.go
- request.go
- router.go
- server.go
- ws_connection.go