Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ClosedErr = errors.New("连接关闭,不能发送消息") NotKey = errors.New("扩展参数没有找到对应的Key") )
Functions ¶
This section is empty.
Types ¶
type IConnManager ¶
type IConnManager interface {
Add(conn IConnection) bool //添加链接
Remove(conn IConnection) bool //移除连接
RemoveById(connId uint64) bool //移除连接
Get(connId uint64) (IConnection, bool) //利用ConnID获取链接
Count() int32 //获取个数
Range(hFunc func(connId uint64, value IConnection) bool) //遍历
ClearConn() //删除并停止所有链接
}
IConnManager
连接管理接口
type IConnection ¶
type IConnection interface {
GetNetConn() interface{} //获取连接(udp获取到的是监听的连接,tcp获取的是真正的客户端连接)
GetNetwork() string //获取网络类型[tcp,udp]
GetConnId() uint64 //获取客户端ID
GetRemoteAddr() net.Addr //获取远程客户端地址信息
GetLocalAddr() net.Addr //获取本地地址
GetHeartTime() time.Time //连接最后一次接收数据时间[初始为连接建立时间]
GetSendTime() time.Time //连接最后一次发送数据时间[初始化,调用接口,调用系统发送 的情况下会更新时间]
GetStartTime() time.Time //连接建立时间
SendData(data []byte, cmdCode string) error //发送消息到客户端: data 下发数据,cmdCode 指令标识[如: rep 普通回复, cmd 用户操作下发 。。]供业务使用
// SendDataCall 发送消息到客户端带回调:data 下发数据,param 下发需要回调携带参数,cmdCode 指令标识[如: rep 普通回复, cmd 用户操作下发 。。]供业务使用,callFunc 下发后回调函数
SendDataCall(data []byte, cmdCode string, param interface{}, callFunc func(IConnection, []byte, bool, string, interface{}, error)) error
SetProperty(key string, value interface{}) //设置链接属性
GetProperty(key string) (interface{}, error) //获取链接属性
RemoveProperty(key string) //移除链接属性
GetPropertyKeys() []string //获取所有属性key
GetRecInfo() (count, byteSize uint64) //上行当前处理的包总数(处理前,1开始),总大小(字节)
GetRepInfo() (count, byteSize, errCount uint64) //下行当前处理的包总数(处理后),总大小(字节)
Stop() //停止连接,结束当前连接状态
GetIsClosed() bool //获取的状态(ture:关闭状态,false:未关闭)
Incr(val int64) int64 //连接提供给业务作为流水号使用,循环累加,(val 为正数为递增,val为负数为递减,val为0则获取值)
}
IConnection 连接的接口
type IReceiver ¶
type IReceiver interface {
GetHeadLen() int //获取分包所需包头长度
CanHandle(conn IConnection, buffer []byte) bool //是否能处理
Receiver(conn IConnection, buffer []byte) ([]byte, int) //返回数据包,处理到的位置
Reset() //重置分包状态为初始状态[在Receiver内部异常时调用减少由于发送方发送数据有异常时包的丢失率]
}
IReceiver 分包器
type IService ¶
type IService interface {
GetIsStart() bool //获取是否启动[true 启动,false 未启动]
Start() //启动服务器方法
Stop() //停止服务器方法
GetConnMgr() IConnManager //得到链接管理
GetConn(connId uint64) (IConnection, bool) //获取连接
SetLogHandle(h func(level ErrLevel, msg ...interface{})) //设置内部异常抛出处理
SetHandleStrategy(h func(IConnection, []byte) *StrategyData) //设置处理策略,TCP和UDP表现不一样,tcp 只会在连接上第一包数据时调用,udp 会在每一包数上次都调用
SetOnConnStart(h func(IConnection)) //设置连接开始处理方法
SetOnConnStop(h func(IConnection)) //设置连接结束处理方法
SetOnReceive(h func(IConnection, []byte)) //连接上传一包完整数据(如果配置 config.HDataCache 为true(默认true)时方法运行完成后会回收,不可再另开协程处理,如果要开启其它协程处理需要复制数据内容])
SetOnReply(h func(IConnection, []byte, bool, string, interface{}, error)) //设置下发回调(连接,下发数据,是否成功,业务代码,下发数据时带的参数,异常信息)
GetStartTime() time.Time //获取服务启动时间
ToMap() map[string]int64 //获取内部日志
}
IService 定义服务器接口
type StrategyData ¶ added in v1.8.17
type StrategyData struct {
/*
业务唯一编号,注意该属性会设置到IConnection 扩展参数,业务需要避免覆盖属性
Udp会内部根据这个判断是否更换了连接,
如果编号一样则没有更换,
如果更换则认定为更换的连接
内部会创建新连接对象 IConnection 的 ConnId 会更改
TCP可以忽略不设置
*/
Key string
Receivers []IReceiver //分包器,tcp使用,udp可以忽略
ExtData map[string]interface{} //扩展数据,会添加到 IConnection 的扩展数据中;
}
StrategyData 策略数据
Click to show internal directories.
Click to hide internal directories.