Documentation
¶
Index ¶
- func NewConnManager() netConn.IConnManager
- func NewConnection(server *Service, conn net.Conn, connId uint64, msgHandler netConn.IMsgHandle, ...) netConn.IConnection
- func NewMsgHandle(workerSize int, taskQueueSize int) netConn.IMsgHandle
- func NewService(config *Config) netConn.IService
- func NewUdpConnection(server *UdpService, conn *net.UDPConn, connId uint64, remoteAddr net.Addr, ...) netConn.IConnection
- func NewUdpService(config *Config) netConn.IService
- type CallSendHandle
- type Config
- type ConnManager
- type Connection
- func (c *Connection) GetBytesCache() *bytes.Buffer
- func (c *Connection) GetConnId() uint64
- func (c *Connection) GetNetConn() net.Conn
- func (c *Connection) GetPackCount() int64
- func (c *Connection) GetProperty(key string) (interface{}, error)
- func (c *Connection) GetReceiver() []netConn.IReceiver
- func (c *Connection) GetRemoteAddr() net.Addr
- func (c *Connection) OnCompleted(data []byte, offset int, count int)
- func (c *Connection) OnReceiveCompleted(data []byte)
- func (c *Connection) RemoveProperty(key string)
- func (c *Connection) SendData(data []byte) error
- func (c *Connection) SendDataCall(data []byte, param interface{}, ...) error
- func (c *Connection) SetProperty(key string, value interface{})
- func (c *Connection) Start()
- func (c *Connection) Stop()
- type MsgHandle
- type Request
- type Service
- func (s *Service) CallErrorhandle(level string, msg ...string)
- func (s *Service) CallOnConnStart(conn netConn.IConnection)
- func (s *Service) CallOnConnStop(conn netConn.IConnection)
- func (s *Service) CallOnReceiveCompleted(conn netConn.IConnection, data []byte)
- func (s *Service) GetConn(connId uint64) (netConn.IConnection, bool)
- func (s *Service) GetConnId() uint64
- func (s *Service) GetConnMgr() netConn.IConnManager
- func (s *Service) SetCreateReceiver(hookFunc func(netConn.IConnection, []byte) []netConn.IReceiver)
- func (s *Service) SetErrorHandle(hookFunc func(level string, msg string))
- func (s *Service) SetOnConnStart(hookFunc func(netConn.IConnection))
- func (s *Service) SetOnConnStop(hookFunc func(netConn.IConnection))
- func (s *Service) SetOnReceiveCompleted(hookFunc func(netConn.IConnection, []byte))
- func (s *Service) Start()
- func (s *Service) Stop()
- type UdpConnection
- func (c *UdpConnection) GetBytesCache() *bytes.Buffer
- func (c *UdpConnection) GetConnId() uint64
- func (c *UdpConnection) GetPackCount() int64
- func (c *UdpConnection) GetProperty(key string) (interface{}, error)
- func (c *UdpConnection) GetRemoteAddr() net.Addr
- func (c *UdpConnection) OnCompleted(data []byte, offset int, count int)
- func (c *UdpConnection) OnReceiveCompleted(data []byte)
- func (c *UdpConnection) RemoveProperty(key string)
- func (c *UdpConnection) SendData(data []byte) error
- func (c *UdpConnection) SendDataCall(data []byte, param interface{}, ...) error
- func (c *UdpConnection) SetProperty(key string, value interface{})
- func (c *UdpConnection) Start()
- func (c *UdpConnection) Stop()
- type UdpService
- func (s *UdpService) CallErrorhandle(level string, msgs ...string)
- func (s *UdpService) CallOnConnStart(conn netConn.IConnection)
- func (s *UdpService) CallOnConnStop(conn netConn.IConnection)
- func (s *UdpService) CallOnReceiveCompleted(conn netConn.IConnection, data []byte)
- func (s *UdpService) GetConn(connId uint64) (netConn.IConnection, bool)
- func (s *UdpService) GetConnId() uint64
- func (s *UdpService) GetConnMgr() netConn.IConnManager
- func (s *UdpService) SetCreateReceiver(hookFunc func(netConn.IConnection, []byte) []netConn.IReceiver)
- func (s *UdpService) SetErrorHandle(hookFunc func(level string, msg string))
- func (s *UdpService) SetOnConnStart(hookFunc func(netConn.IConnection))
- func (s *UdpService) SetOnConnStop(hookFunc func(netConn.IConnection))
- func (s *UdpService) SetOnReceiveCompleted(hookFunc func(netConn.IConnection, []byte))
- func (s *UdpService) Start()
- func (s *UdpService) Stop()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewConnection ¶
func NewConnection(server *Service, conn net.Conn, connId uint64, msgHandler netConn.IMsgHandle, config *Config) netConn.IConnection
创建连接的方法
func NewMsgHandle ¶
func NewMsgHandle(workerSize int, taskQueueSize int) netConn.IMsgHandle
创建消息处理队列
func NewUdpConnection ¶
func NewUdpConnection(server *UdpService, conn *net.UDPConn, connId uint64, remoteAddr net.Addr, msgHandler netConn.IMsgHandle, config *Config) netConn.IConnection
Udp连接
func NewUdpService ¶
创建一个服务器句柄 name:服务名称 network:网络类型 "udp", "udp4", "udp6" port:端口 workerSize:工作池 workerPoolSize:单工作线程缓存个数 bufferSize:单个缓存大小 bufferCount:缓存数量
Types ¶
type CallSendHandle ¶
type CallSendHandle struct { Buf []byte //发送数据 CallFunc func(netConn.IConnection, bool, interface{}, error) //回调方法 Param interface{} //参数 }
数据发送回调处理
type Config ¶
type Config struct { Network string //网络 AddrAry []string //监听地址和端口:绑定的IP加端口:["192.168.1.24:7018",...] WorkerSize int //工作池中工作线程个数 TaskQueueSize int //单个工作队列缓存任务大小 BufferSize uint //缓存尺寸(字节) BufferCount uint //缓存池缓存大小 SendDataCount int //单个连接待发送数据个数 }
配置
type ConnManager ¶
type ConnManager struct {
// contains filtered or unexported fields
}
连接管理模块
func (*ConnManager) Get ¶
func (connMgr *ConnManager) Get(connID uint64) (netConn.IConnection, bool)
利用ConnID获取链接
type Connection ¶
type Connection struct { Server *Service //当前conn属于哪个server,在conn初始化的时候添加即可 Conn net.Conn //当前连接的socket 套接字 ConnId uint64 //当前连接的ID 也可以称作为SessionID,ID全局唯一 ExitBuffChan chan bool //告知该链接已经退出/停止的channel(管道) MsgHandler netConn.IMsgHandle //消息处理 HeartTime time.Time //心跳时间 Receiver []netConn.IReceiver //分包器 PackCount int64 //包个数 // contains filtered or unexported fields }
连接结构体
func (*Connection) GetProperty ¶
func (c *Connection) GetProperty(key string) (interface{}, error)
获取链接属性
func (*Connection) OnCompleted ¶
func (c *Connection) OnCompleted(data []byte, offset int, count int)
数据上传处理
func (*Connection) OnReceiveCompleted ¶
func (c *Connection) OnReceiveCompleted(data []byte)
数据上传了完整一包的回调
func (*Connection) SendData ¶
func (c *Connection) SendData(data []byte) error
直接将Message数据发送数据给远程的TCP客户端
func (*Connection) SendDataCall ¶
func (c *Connection) SendDataCall(data []byte, param interface{}, callFunc func(netConn.IConnection, bool, interface{}, error)) error
直接将Message数据发送数据给远程的TCP客户端(带参数和回调)
func (*Connection) SetProperty ¶
func (c *Connection) SetProperty(key string, value interface{})
设置链接属性
type MsgHandle ¶
type MsgHandle struct { TaskQueue []chan netConn.IRequest //Worker负责取任务的消息队列 ExitMsgChan []chan bool //告知该链接已经退出/停止的channel(管道) // contains filtered or unexported fields }
消息处理队列
func (*MsgHandle) SendMsgToTaskQueue ¶
将消息交给TaskQueue,由worker进行处理
func (*MsgHandle) StartOneWorker ¶
启动一个Worker工作流程
type Request ¶
type Request struct { Conn netConn.IConnection //已经和客户端建立好的 链接 Data []byte //客户端请求的数据 Count int //数据长度 }
客户端请求内容
type Service ¶
type Service struct { ConnId uint64 //客户端id ConnMgr netConn.IConnManager //当前Server的链接管理器 OnErrorHandle func(level string, msg string) //设置异常处理 OnConnStart func(conn netConn.IConnection) //该Server的连接创建时Hook函数 OnConnStop func(conn netConn.IConnection) //该Server的连接断开时的Hook函数 OnReceiveCompleted func(conn netConn.IConnection, data []byte) //数据上传完成 ExitServiceChan chan bool //告知该链接已经退出/停止的channel(管道) CreateReceiver func(conn netConn.IConnection, data []byte) []netConn.IReceiver //数据分包 BufferPool *util.BufferPool //缓存管理器 // contains filtered or unexported fields }
IServer 接口实现,定义一个Server服务类
func (*Service) CallErrorhandle ¶
错误消息处理
func (*Service) CallOnConnStart ¶
func (s *Service) CallOnConnStart(conn netConn.IConnection)
调用连接OnConnStart Hook函数
func (*Service) CallOnConnStop ¶
func (s *Service) CallOnConnStop(conn netConn.IConnection)
调用连接OnConnStop Hook函数
func (*Service) CallOnReceiveCompleted ¶
func (s *Service) CallOnReceiveCompleted(conn netConn.IConnection, data []byte)
数据上传完成回调
func (*Service) SetCreateReceiver ¶
设置创建分包策略方法
func (*Service) SetErrorHandle ¶
func (*Service) SetOnConnStart ¶
func (s *Service) SetOnConnStart(hookFunc func(netConn.IConnection))
设置该Server的连接创建时Hook函数
func (*Service) SetOnConnStop ¶
func (s *Service) SetOnConnStop(hookFunc func(netConn.IConnection))
设置该Server的连接断开时的Hook函数
func (*Service) SetOnReceiveCompleted ¶
func (s *Service) SetOnReceiveCompleted(hookFunc func(netConn.IConnection, []byte))
数据上传完成处理函数[分包后]
type UdpConnection ¶
type UdpConnection struct { Server *UdpService //当前conn属于哪个server,在conn初始化的时候添加即可 Conn *net.UDPConn //当前连接的socket 套接字 ConnId uint64 //当前连接的ID 也可以称作为SessionID,ID全局唯一 RemoteAddr net.Addr //客户端地址 ExitBuffChan chan bool //告知该链接已经退出/停止的channel(管道) PackCount int64 //包个数 MsgHandler netConn.IMsgHandle //消息处理 HeartTime time.Time //心跳时间 // contains filtered or unexported fields }
func (*UdpConnection) GetProperty ¶
func (c *UdpConnection) GetProperty(key string) (interface{}, error)
获取链接属性
func (*UdpConnection) OnCompleted ¶
func (c *UdpConnection) OnCompleted(data []byte, offset int, count int)
数据上次一包(未分包开始处理不确定是否完成)
func (*UdpConnection) OnReceiveCompleted ¶
func (c *UdpConnection) OnReceiveCompleted(data []byte)
数据上传完整的一包处理
func (*UdpConnection) SendDataCall ¶
func (c *UdpConnection) SendDataCall(data []byte, param interface{}, callFunc func(netConn.IConnection, bool, interface{}, error)) error
发送消息到客户端带回调
func (*UdpConnection) SetProperty ¶
func (c *UdpConnection) SetProperty(key string, value interface{})
设置链接属性
type UdpService ¶
type UdpService struct { ConnId uint64 //客户端id ConnMgr netConn.IConnManager //当前Server的链接管理器 OnErrorHandle func(level string, msg string) //设置异常处理 OnConnStart func(conn netConn.IConnection) //该Server的连接创建时Hook函数 OnConnStop func(conn netConn.IConnection) //该Server的连接断开时的Hook函数 OnReceiveCompleted func(conn netConn.IConnection, data []byte) //数据上传完成 ExitServiceChan chan bool //告知该链接已经退出/停止的channel(管道) CreateReceiver func(conn netConn.IConnection, data []byte) []netConn.IReceiver //数据分包 BufferPool *util.BufferPool //缓存管理器 ConnectionsMap map[string]uint64 //连接地址映射 // contains filtered or unexported fields }
IServer 接口实现,定义一个Server服务类
func (*UdpService) CallErrorhandle ¶
func (s *UdpService) CallErrorhandle(level string, msgs ...string)
错误消息处理
func (*UdpService) CallOnConnStart ¶
func (s *UdpService) CallOnConnStart(conn netConn.IConnection)
调用连接OnConnStart Hook函数
func (*UdpService) CallOnConnStop ¶
func (s *UdpService) CallOnConnStop(conn netConn.IConnection)
调用连接OnConnStop Hook函数
func (*UdpService) CallOnReceiveCompleted ¶
func (s *UdpService) CallOnReceiveCompleted(conn netConn.IConnection, data []byte)
数据上传完成回调
func (*UdpService) GetConn ¶
func (s *UdpService) GetConn(connId uint64) (netConn.IConnection, bool)
func (*UdpService) SetCreateReceiver ¶
func (s *UdpService) SetCreateReceiver(hookFunc func(netConn.IConnection, []byte) []netConn.IReceiver)
设置创建分包策略方法
func (*UdpService) SetErrorHandle ¶
func (s *UdpService) SetErrorHandle(hookFunc func(level string, msg string))
func (*UdpService) SetOnConnStart ¶
func (s *UdpService) SetOnConnStart(hookFunc func(netConn.IConnection))
设置该Server的连接创建时Hook函数
func (*UdpService) SetOnConnStop ¶
func (s *UdpService) SetOnConnStop(hookFunc func(netConn.IConnection))
设置该Server的连接断开时的Hook函数
func (*UdpService) SetOnReceiveCompleted ¶
func (s *UdpService) SetOnReceiveCompleted(hookFunc func(netConn.IConnection, []byte))
数据上传完成处理函数[分包后]