Documentation ¶
Index ¶
- func NewTcpServer() hiface.ITcpServer
- type BaseTcpRouter
- type ConnTcpManager
- type Package
- func (p *Package) GetHandlerRouter() string
- func (p *Package) GetPkg() []byte
- func (p *Package) GetPkgBody() []byte
- func (p *Package) GetPkgHeadLen() int16
- func (p *Package) Pack() ([]byte, error)
- func (p *Package) Unpack(conn io.Reader) (hiface.ITcpPkg, error)
- func (p *Package) UnpackBody(binaryData []byte) (*Package, error)
- func (p *Package) UnpackHead(binaryData []byte) (*Package, error)
- type PkgHandle
- func (p *PkgHandle) AddRouter(handlerRouter string, router hiface.ITcpRouter)
- func (p *PkgHandle) DoPkgHandler(request hiface.ITcpRequest)
- func (p *PkgHandle) SendPkgToTaskQueue(request hiface.ITcpRequest)
- func (p *PkgHandle) StartOneWorker(workerID int, taskQueue chan hiface.ITcpRequest)
- func (p *PkgHandle) StartWorkerPool()
- type TcpConnection
- func (c *TcpConnection) GetConnID() int64
- func (c *TcpConnection) GetProperty(key string) (interface{}, error)
- func (c *TcpConnection) GetTCPConnection() *net.TCPConn
- func (c *TcpConnection) RemoteAddr() net.Addr
- func (c *TcpConnection) RemoveProperty(key string)
- func (c *TcpConnection) SendTcpPkg(pkgBodyType byte, pkg interface{}, userBuf ...bool) error
- func (c *TcpConnection) SetProperty(key string, value interface{})
- func (c *TcpConnection) Start()
- func (c *TcpConnection) StartReader()
- func (c *TcpConnection) StartWriter()
- func (c *TcpConnection) Stop()
- type TcpRequest
- type TcpServer
- func (s *TcpServer) AddRouter(handlerRouter string, router hiface.ITcpRouter)
- func (s *TcpServer) CallOnConnStart(conn hiface.ITcpConnection)
- func (s *TcpServer) CallOnConnStop(conn hiface.ITcpConnection)
- func (s *TcpServer) GetConnMgr() hiface.ITcpConnManager
- func (s *TcpServer) Serve()
- func (s *TcpServer) SetOnConnStart(hookFunc func(hiface.ITcpConnection))
- func (s *TcpServer) SetOnConnStop(hookFunc func(hiface.ITcpConnection))
- func (s *TcpServer) Start()
- func (s *TcpServer) Stop()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BaseTcpRouter ¶
type BaseTcpRouter struct{}
实现ITcpRouter接口,自定义接口只需继承即可,或者继承后重写
func (*BaseTcpRouter) Handle ¶
func (br *BaseTcpRouter) Handle(req hiface.ITcpRequest)
func (*BaseTcpRouter) PostHandle ¶
func (br *BaseTcpRouter) PostHandle(req hiface.ITcpRequest)
func (*BaseTcpRouter) PreHandle ¶
func (br *BaseTcpRouter) PreHandle(req hiface.ITcpRequest)
type ConnTcpManager ¶
type ConnTcpManager struct {
// contains filtered or unexported fields
}
ConnTcpManager 连接管理模块
func (*ConnTcpManager) Add ¶
func (connMgr *ConnTcpManager) Add(conn hiface.ITcpConnection)
Add 添加连接
func (*ConnTcpManager) Get ¶
func (connMgr *ConnTcpManager) Get(connID int64) (hiface.ITcpConnection, error)
Get 利用ConnID获取连接
func (*ConnTcpManager) Remove ¶
func (connMgr *ConnTcpManager) Remove(conn hiface.ITcpConnection)
Remove 删除连接
type Package ¶
type Package struct { //--------------------------消息头Head------------------------------ // ----版本数据长度2B + 密钥长度2B + 加密数据长度2B + 签名数据长度2B ---- VersionLength int16 // 版本数据长度,2B KeyLength int16 // 加密密钥长度,2B DataLength int16 // 数据部分长度,2B SignLength int16 // 签名数据长度,2B //--------------------------消息体Body---------------------=-------- Version []byte // Tcp协议版本 Data []byte // 加密数据 // --------------------------签名--------------------------------- Timestamp int32 // 时间戳,4B Signature []byte // 加密数据签名,数据 + 时间进行签名。 // contains filtered or unexported fields }
Package 数据包
func (*Package) GetHandlerRouter ¶
GetHandlerRouter 获取路由(即消息处理器)
func (*Package) GetPkgBody ¶
func (*Package) UnpackBody ¶
Unpack 拆包body(解压数据)
type PkgHandle ¶
type PkgHandle struct { Apis map[string]hiface.ITcpRouter // 存放每个HandlerRouter所对应的处理器 WorkerPoolSize int64 // 业务工作Worker池的数量 TaskQueue []chan hiface.ITcpRequest // Worker负责取任务的消息队列,一个Worker对应一个Queue }
func NewPkgHandle ¶
func NewPkgHandle() *PkgHandle
func (*PkgHandle) AddRouter ¶
func (p *PkgHandle) AddRouter(handlerRouter string, router hiface.ITcpRouter)
AddRouter 为消息添加具体的处理逻辑
func (*PkgHandle) DoPkgHandler ¶
func (p *PkgHandle) DoPkgHandler(request hiface.ITcpRequest)
DoPkgHandler 以非阻塞方式处理消息
func (*PkgHandle) SendPkgToTaskQueue ¶
func (p *PkgHandle) SendPkgToTaskQueue(request hiface.ITcpRequest)
SendPkgToTaskQueue 将消息交给TaskQueue,由worker进行处理
func (*PkgHandle) StartOneWorker ¶
func (p *PkgHandle) StartOneWorker(workerID int, taskQueue chan hiface.ITcpRequest)
StartOneWorker 启动一个Worker工作协程
func (*PkgHandle) StartWorkerPool ¶
func (p *PkgHandle) StartWorkerPool()
StartWorkerPool 启动worker工作池
type TcpConnection ¶
type TcpConnection struct { // 当前Conn属于哪个Server TcpServer hiface.ITcpServer // 当前连接的socket TCP套接字 Conn *net.TCPConn // 当前连接的ID 也可以称作为SessionID,ID全局唯一 ConnID int64 // 消息管理MsgId和对应处理方法的消息管理模块 PkgHandler hiface.ITcpPkgHandle sync.RWMutex // contains filtered or unexported fields }
func NewTcpConnection ¶
func NewTcpConnection(server hiface.ITcpServer, conn *net.TCPConn, connID int64, pkgHandler hiface.ITcpPkgHandle) *TcpConnection
创建连接的方法
func (*TcpConnection) GetProperty ¶
func (c *TcpConnection) GetProperty(key string) (interface{}, error)
GetProperty 获取连接属性
func (*TcpConnection) GetTCPConnection ¶
func (c *TcpConnection) GetTCPConnection() *net.TCPConn
GetTCPConnection 从当前连接获取原始的socket TCPConn
func (*TcpConnection) RemoteAddr ¶
func (c *TcpConnection) RemoteAddr() net.Addr
RemoteAddr 获取远程客户端地址信息
func (*TcpConnection) RemoveProperty ¶
func (c *TcpConnection) RemoveProperty(key string)
RemoveProperty 移除连接属性
func (*TcpConnection) SendTcpPkg ¶
func (c *TcpConnection) SendTcpPkg(pkgBodyType byte, pkg interface{}, userBuf ...bool) error
SendTcpPkg 发送tcp数据包
@pkgBodyType 数据类型 @pkg 数据内容(自定义结构体对象) @userBuf 是否启动缓冲(默认关闭)
func (*TcpConnection) SetProperty ¶
func (c *TcpConnection) SetProperty(key string, value interface{})
SetProperty 设置连接属性
func (*TcpConnection) StartReader ¶
func (c *TcpConnection) StartReader()
StartReader 读消息协程,用于从客户端中读取数据
func (*TcpConnection) StartWriter ¶
func (c *TcpConnection) StartWriter()
StartWriter 写消息协程,用户将数据发送给客户端
type TcpRequest ¶
type TcpRequest struct {
// contains filtered or unexported fields
}
func (*TcpRequest) GetConnection ¶
func (r *TcpRequest) GetConnection() hiface.ITcpConnection
获取请求连接信息
type TcpServer ¶
type TcpServer struct { Name string // 服务器的名称 IPVersion string // tcp4或other IP string // 服务绑定的IP地址 Port int // 服务绑定的端口 ConnMgr hiface.ITcpConnManager // 当前服务的连接管理器 OnConnStart func(conn hiface.ITcpConnection) // 服务创建连接时Hook函数 OnConnStop func(conn hiface.ITcpConnection) // 服务断开连接时的Hook函数 // contains filtered or unexported fields }
TcpServer ITcpServer接口实现,定义一个TcpServer服务类
func (*TcpServer) AddRouter ¶
func (s *TcpServer) AddRouter(handlerRouter string, router hiface.ITcpRouter)
AddRouter 路由功能:给当前服务注册一个路由业务方法,供客户端连接处理使用
func (*TcpServer) CallOnConnStart ¶
func (s *TcpServer) CallOnConnStart(conn hiface.ITcpConnection)
CallOnConnStart 调用连接OnConnStart Hook函数
func (*TcpServer) CallOnConnStop ¶
func (s *TcpServer) CallOnConnStop(conn hiface.ITcpConnection)
CallOnConnStop 调用连接OnConnStop Hook函数
func (*TcpServer) GetConnMgr ¶
func (s *TcpServer) GetConnMgr() hiface.ITcpConnManager
GetConnMgr 获取连接管理
func (*TcpServer) SetOnConnStart ¶
func (s *TcpServer) SetOnConnStart(hookFunc func(hiface.ITcpConnection))
SetOnConnStart 设置该Server的连接创建时Hook函数
func (*TcpServer) SetOnConnStop ¶
func (s *TcpServer) SetOnConnStop(hookFunc func(hiface.ITcpConnection))
SetOnConnStop 设置该Server的连接断开时的Hook函数