Documentation
¶
Index ¶
- Constants
- Variables
- func DecodeBytes(data []byte) (msg msgdef.IMsg, err error)
- func DecodeMsg(flag byte, buf []byte) (msgdef.IMsg, error)
- func DecodeMsgByMsgID(flag byte, buf []byte, msgID uint32) (msgContent msgdef.IMsg, err error)
- func DecryptData(buf []byte) []byte
- func DecryptDataByClient(buf []byte) []byte
- func EncodeMsg(msg msgdef.IMsg, buf []byte, forceNoCompress bool) ([]byte, error)
- func EncodeMsgWithEncrypt(msg msgdef.IMsg, buf []byte, forceNoCompress bool, encryptEnabled bool) ([]byte, error)
- func EncryptData(buf []byte) []byte
- func GetClient_key() []byte
- func GetListener(protocal string, info *global.ListenCfg) (result net.Listener, port int)
- func GetMsgID(buf []byte) uint16
- func GetServer_key() []byte
- func NetSrvSetCompress(v bool) optionF
- func NetSrvSetEncry(v bool) optionF
- func NetSrvSetFragmentConfig(sendEnabled, recvEnabled bool, size int) optionF
- func NetSrvSetHbTicker(v time.Duration) optionF
- func NetSrvSetIsClient(v bool) optionF
- func NetSrvSetMsgHander(v msghandler.IMsgHandler) optionF
- func NetSrvSetProtocal(v ProtocalEnum) optionF
- func NetSrvSetTimeout(v time.Duration) optionF
- func SetClient_key(bytes []byte)
- func SetServer_key(bytes []byte)
- func SnappyCompressBytes(olddata, buf []byte) ([]byte, error)
- type INetServer
- type INetSess
- type ISessCallBack
- type IkcpSession
- type NetSess
- func (this *NetSess) Close()
- func (this *NetSess) DelayClose()
- func (this *NetSess) GetID() uint64
- func (this *NetSess) GetType() global.ServerTypeEnum
- func (this *NetSess) IsFragmentRecvEnabled() bool
- func (this *NetSess) IsFragmentSendEnabled() bool
- func (this *NetSess) IsVerified() bool
- func (this *NetSess) RemoteAddr() string
- func (this *NetSess) Send(msg msgdef.IMsg)
- func (this *NetSess) SendBuf(data []byte)
- func (this *NetSess) SendNow(msg msgdef.IMsg)
- func (this *NetSess) SetCompressEnabled(v bool)
- func (this *NetSess) SetEncryEnabled(v bool)
- func (this *NetSess) SetFragmentConfig(sendEnabled, recvEnabled bool, size int)
- func (this *NetSess) SetID(v uint64)
- func (this *NetSess) SetInputMsg(v bool)
- func (this *NetSess) SetMsgHandler(msgh msghandler.IMsgHandler)
- func (this *NetSess) SetRecvMsg(v bool)
- func (this *NetSess) SetType(stype global.ServerTypeEnum)
- func (this *NetSess) SetVerify()
- func (this *NetSess) Start()
- type NetSrv
- func (this *NetSrv) GetClientSess(id uint64) (result *NetSess)
- func (this *NetSrv) GetCompressEnabled() bool
- func (this *NetSrv) GetEncryEnabled() bool
- func (this *NetSrv) GetProtocal() ProtocalEnum
- func (this *NetSrv) PutSess(sess INetSess)
- func (this *NetSrv) RangeSess(f func(sess INetSess) bool)
- func (this *NetSrv) SessClosed(sess INetSess)
- func (this *NetSrv) SessVerify(msg msgdef.ISessionVerifyReqMsg, sess INetSess) error
- func (this *NetSrv) Start() (err error)
- func (this *NetSrv) Stop()
- type ProtocalEnum
Constants ¶
const ( // MsgIDSize 消息ID的长度 MsgIDSize = 2 // MsgHeadSize 这是包含msgID的长度 MsgHeadSize = 6 // MaxMsgBuffer 消息最大长度 MaxMsgBuffer = 100 * 1024 )
const ( // 压缩标记 SessFlag_Compress = 1 << iota // 加密标记 SessFlag_Encrypt // 分片消息标记(仅中间分片使用,最后分片无此标记) SessFlag_Fragment )
const ( // 压缩/加密标志掩码 SessFlagMask_CompressEncrypt = SessFlag_Compress | SessFlag_Encrypt )
Variables ¶
var ( // 未知的协议枚举 Err_Unknown_ProtocalEnum = common.NewError(-1, "Unknown ProtocalEnum:%s") // 数据长度错误 Err_Data_Len = common.NewError(-2, "Data Length Error %d") // 会话不存在 Err_Sess_Not_Exist = common.NewError(-3, "Sess Not Exist") // 数据长度超限 Err_Data_Len_Over = common.NewError(-4, "Data Length Over %d") // 压缩标志错误 Err_Compression_Flag = common.NewError(-5, "Compression Flag Error") // 未知的监听类型 Err_Unknown_ListenType = common.NewError(-6, "Unknown ListenType:%s") // 分片功能已禁用 Err_Fragment_Disabled = common.NewError(-7, "Fragment Disabled") // 分片大小无效 Err_Fragment_Size = common.NewError(-8, "Fragment Size Invalid %d") // 分片消息ID不匹配 Err_Fragment_Mismatch = common.NewError(-9, "Fragment msgID mismatch: expected %d, got %d") )
Functions ¶
func DecodeMsgByMsgID ¶
DecodeMsg 返回消息名称及反序列化后的消息对象
func DecryptDataByClient ¶
DecryptDataByClient 模拟客户端解密算法
func EncodeMsg ¶
EncodeMsg 将消息编码为字节流,可选择禁用压缩但不启用加密
参数:
- msg: 要编码的消息实例,必须实现 IMsg 接口
- buf: 用于编码的缓冲区,可复用或预分配内存。若为nil会自动分配
- forceNoCompress: 为true时强制禁用压缩,即使消息默认启用压缩
返回值:
- []byte: 编码后的字节数据,可能引用传入的buf或新分配的内存
- error: 编码过程中遇到的错误,成功时为nil
说明:
本函数通过调用 EncodeMsgWithEncrypt 实现,固定不启用加密功能(第四个参数设为false) 如需加密功能请直接调用 EncodeMsgWithEncrypt
func EncodeMsgWithEncrypt ¶
func EncodeMsgWithEncrypt(msg msgdef.IMsg, buf []byte, forceNoCompress bool, encryptEnabled bool) ([]byte, error)
EncodeMsgWithEncrypt 对消息进行编码处理,支持压缩和加密选项 参数:
msg: 需要编码的消息对象,必须实现msgdef.IMsg接口 buf: 用于存储编码结果的缓冲区,允许传入现有缓冲区减少内存分配 forceNoCompress: 强制禁用压缩的标志,true表示不进行压缩 encryptEnabled: 启用加密的标志,true表示需要对消息内容进行加密
返回值:
[]byte: 编码后的字节数据(包含可能的压缩和加密处理) error: 处理过程中遇到的错误信息
func GetListener ¶
func NetSrvSetFragmentConfig ¶
NetSrvSetFragmentConfig 设置分片配置 sendEnabled: 发送分片开关,true时大消息自动拆包发送 recvEnabled: 接收分片开关,true时自动合包重组 size: 分片大小,默认64KB,必须小于MaxMsgBuffer
func NetSrvSetProtocal ¶
func NetSrvSetProtocal(v ProtocalEnum) optionF
设置监听服务类型 ProtocalEnum,默认为tcp
Types ¶
type INetServer ¶
type INetServer interface {
//主要给INetSess调用的方法,也可以重载
ISessCallBack
// //开始服务
// Start() (err error)
// //结束服务
// Stop()
// 遍历所有被动连接,返回false就退出遍历
RangeSess(f func(sess INetSess) bool)
// 获取会话
GetClientSess(id uint64) (result *NetSess)
//是否加密
GetEncryEnabled() bool
//连接类型
GetProtocal() ProtocalEnum
}
type INetSess ¶
type INetSess interface {
//发消息
Send(msg msgdef.IMsg)
//发消息二进制数据
SendBuf(data []byte)
// SendNow 立即发送数据。后期可能会废弃
// 注意:此方法不是并发安全的,调用者必须确保在同一时刻只有一个 Goroutine 调用此方法,或自行加锁保护。
SendNow(msg msgdef.IMsg)
//开始
Start()
//关闭 只会被运行一次
Close()
// 延时关闭连接
DelayClose()
//设置消息路由
//一开始使用的都是service的,验证过后
//如果是用户就换成用户的
//剩下的还是使用service的
SetMsgHandler(msgh msghandler.IMsgHandler)
//设置表示连接的ID,一般与上层的服务器ID或是用户ID一致
SetID(uint64)
//表示连接的ID,一般与上层的服务器ID或是用户ID一致
GetID() uint64
//如果连接是服务器过来的话,那就是服务器类型,2表示客户端
GetType() global.ServerTypeEnum
//设置如果连接是服务器过来的话,那就是服务器类型,2表示客户端
SetType(stype global.ServerTypeEnum)
//远程地址
RemoteAddr() string
//是否验证过
IsVerified() bool
//设置验证过
SetVerify()
// 设置是否输出 发出的消息
SetInputMsg(v bool)
// 设置是否输出 收到的消息
SetRecvMsg(v bool)
// 设置是否加密
SetEncryEnabled(v bool)
// 设置是否压缩
SetCompressEnabled(v bool)
// SetFragmentConfig 设置分片配置
// sendEnabled: 发送分片开关,true时大消息自动拆包发送
// recvEnabled: 接收分片开关,true时自动合包重组
// size: 分片大小,默认64KB,必须小于MaxMsgBuffer
SetFragmentConfig(sendEnabled, recvEnabled bool, size int)
// IsFragmentSendEnabled 获取分片发送开关状态
IsFragmentSendEnabled() bool
// IsFragmentRecvEnabled 获取分片接收开关状态
IsFragmentRecvEnabled() bool
}
type ISessCallBack ¶
type ISessCallBack interface {
//连接被关闭的时候(这个回调会在任意协程上,所以需要注意安全)
SessClosed(sess INetSess)
//会话的验证回调 如果是连接客户端的上层需要重载实现验证逻辑,失败就返回错误,底层会关闭连接
SessVerify(msg msgdef.ISessionVerifyReqMsg, sess INetSess) error
//连接类型
GetProtocal() ProtocalEnum
}
type IkcpSession ¶
type IkcpSession interface {
// SetStreamMode toggles the stream mode on/off
SetStreamMode(enable bool)
// SetWindowSize set maximum window size
SetWindowSize(sndwnd, rcvwnd int)
// SetNoDelay calls nodelay() of kcp
// https://github.com/skywind3000/kcp/blob/master/README.en.md#protocol-configuration
SetNoDelay(nodelay, interval, resend, nc int)
// SetDSCP sets the 6bit DSCP field in IPv4 header, or 8bit Traffic Class in IPv6 header.
//
// if the underlying connection has implemented `func SetDSCP(int) error`, SetDSCP() will invoke
// this function instead.
//
// It has no effect if it's accepted from Listener.
SetDSCP(dscp int) error
// SetMtu sets the maximum transmission unit(not including UDP header)
SetMtu(mtu int) bool
// SetACKNoDelay changes ack flush option, set true to flush ack immediately,
SetACKNoDelay(nodelay bool)
}
type NetSess ¶
type NetSess struct {
// contains filtered or unexported fields
}
一个连接会话
func (*NetSess) Close ¶
func (this *NetSess) Close()
关闭 只会被运行一次 关闭的逻辑需要立即运行,处理链接对应的附加逻辑。 不然如果是多链接顶替逻辑的时候,有可能出现新链接来了然后被关了的情况。 因为新链接来的时候
func (*NetSess) GetType ¶
func (this *NetSess) GetType() global.ServerTypeEnum
如果连接是服务器过来的话,那就是服务器类型,2表示客户端
func (*NetSess) IsFragmentRecvEnabled ¶
IsFragmentRecvEnabled 获取分片接收开关状态
func (*NetSess) IsFragmentSendEnabled ¶
IsFragmentSendEnabled 获取分片发送开关状态
func (*NetSess) SetFragmentConfig ¶
SetFragmentConfig 设置分片配置 sendEnabled: 发送分片开关,true时大消息自动拆包发送 recvEnabled: 接收分片开关,true时自动合包重组 size: 分片大小,默认64KB,必须小于MaxMsgBuffer
func (*NetSess) SetMsgHandler ¶
func (this *NetSess) SetMsgHandler(msgh msghandler.IMsgHandler)
设置消息路由 正常情况下都是使用外部的,验证前使用管理器的,验证后使用对象的
func (*NetSess) SetType ¶
func (this *NetSess) SetType(stype global.ServerTypeEnum)
设置如果连接是服务器过来的话,那就是服务器类型,2表示客户端
type NetSrv ¶
type NetSrv struct {
msghandler.IMsgHandler
//监听地址
ListenAddr string
// 服务器配置
Cfg *global.ListenCfg
//超时时间
TimeoutV time.Duration
//心跳时间
HBTickerV time.Duration
// contains filtered or unexported fields
}
网络连接的监听器 处理监听,管理连接会话,基础的连接验证
func NewNetServer ¶
func (*NetSrv) SessVerify ¶
func (this *NetSrv) SessVerify(msg msgdef.ISessionVerifyReqMsg, sess INetSess) error
会话的验证回调 如果是连接客户端的上层需要重载实现验证逻辑,失败就返回错误,底层会关闭连接
type ProtocalEnum ¶
type ProtocalEnum = string
ProtocalEnum 连接类型
const ( Protocal_Tcp ProtocalEnum = "tcp" Protocal_Kcp ProtocalEnum = "kcp" Protocal_WebSocket ProtocalEnum = "ws" )