sess

package
v0.0.0-...-29d89d3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: BSD-4-Clause Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MsgIDSize 消息ID的长度
	MsgIDSize = 2

	// MsgHeadSize 这是包含msgID的长度
	MsgHeadSize = 6
	// MaxMsgBuffer 消息最大长度
	MaxMsgBuffer = 100 * 1024
)
View Source
const (
	// 压缩标记
	SessFlag_Compress = 1 << iota
	// 加密标记
	SessFlag_Encrypt
	// 分片消息标记(仅中间分片使用,最后分片无此标记)
	SessFlag_Fragment
)
View Source
const (
	// 压缩/加密标志掩码
	SessFlagMask_CompressEncrypt = SessFlag_Compress | SessFlag_Encrypt
)

Variables

View Source
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 DecodeBytes

func DecodeBytes(data []byte) (msg msgdef.IMsg, err error)

发给客户端的二进制,用于调试

func DecodeMsg

func DecodeMsg(flag byte, buf []byte) (msgdef.IMsg, error)

DecodeMsg 返回消息名称及反序列化后的消息对象

func DecodeMsgByMsgID

func DecodeMsgByMsgID(flag byte, buf []byte, msgID uint32) (msgContent msgdef.IMsg, err error)

DecodeMsg 返回消息名称及反序列化后的消息对象

func DecryptData

func DecryptData(buf []byte) []byte

DecryptData 服务器解密算法

func DecryptDataByClient

func DecryptDataByClient(buf []byte) []byte

DecryptDataByClient 模拟客户端解密算法

func EncodeMsg

func EncodeMsg(msg msgdef.IMsg, buf []byte, forceNoCompress bool) ([]byte, error)

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 EncryptData

func EncryptData(buf []byte) []byte

EncryptData 服务器加密算法

func GetClient_key

func GetClient_key() []byte

获取客户端编码的密钥

func GetListener

func GetListener(protocal string, info *global.ListenCfg) (result net.Listener, port int)

func GetMsgID

func GetMsgID(buf []byte) uint16

GetMsgID 获取消息ID

func GetServer_key

func GetServer_key() []byte

获取服务器编码的密钥

func NetSrvSetCompress

func NetSrvSetCompress(v bool) optionF

设置是否压缩,默认为false

func NetSrvSetEncry

func NetSrvSetEncry(v bool) optionF

设置是否加密,默认为false;isClient为true时有效果

func NetSrvSetFragmentConfig

func NetSrvSetFragmentConfig(sendEnabled, recvEnabled bool, size int) optionF

NetSrvSetFragmentConfig 设置分片配置 sendEnabled: 发送分片开关,true时大消息自动拆包发送 recvEnabled: 接收分片开关,true时自动合包重组 size: 分片大小,默认64KB,必须小于MaxMsgBuffer

func NetSrvSetHbTicker

func NetSrvSetHbTicker(v time.Duration) optionF

设置心跳时间

func NetSrvSetIsClient

func NetSrvSetIsClient(v bool) optionF

设置是否为管理客户端会话,默认为false

func NetSrvSetMsgHander

func NetSrvSetMsgHander(v msghandler.IMsgHandler) optionF

设置链接管理器的消息路由

func NetSrvSetProtocal

func NetSrvSetProtocal(v ProtocalEnum) optionF

设置监听服务类型 ProtocalEnum,默认为tcp

func NetSrvSetTimeout

func NetSrvSetTimeout(v time.Duration) optionF

设置超时时间

func SetClient_key

func SetClient_key(bytes []byte)

设置客户端编码的密钥

func SetServer_key

func SetServer_key(bytes []byte)

设置服务器编码的密钥

func SnappyCompressBytes

func SnappyCompressBytes(olddata, buf []byte) ([]byte, error)

对二进制数据进行压缩

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
}

func Dial

func Dial(protocal ProtocalEnum, cb ISessCallBack, addr string, thgo *threads.ThreadGo, timeout, hbticker time.Duration) (INetSess, error)

网络连接的客户端 是去主动连接别人的

Dial 创建一个连接会话

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) DelayClose

func (this *NetSess) DelayClose()

延时关闭连接

func (*NetSess) GetID

func (this *NetSess) GetID() uint64

表示连接的ID,一般与上层的服务器ID或是用户ID一致

func (*NetSess) GetType

func (this *NetSess) GetType() global.ServerTypeEnum

如果连接是服务器过来的话,那就是服务器类型,2表示客户端

func (*NetSess) IsFragmentRecvEnabled

func (this *NetSess) IsFragmentRecvEnabled() bool

IsFragmentRecvEnabled 获取分片接收开关状态

func (*NetSess) IsFragmentSendEnabled

func (this *NetSess) IsFragmentSendEnabled() bool

IsFragmentSendEnabled 获取分片发送开关状态

func (*NetSess) IsVerified

func (this *NetSess) IsVerified() bool

是否验证过

func (*NetSess) RemoteAddr

func (this *NetSess) RemoteAddr() string

远程地址

func (*NetSess) Send

func (this *NetSess) Send(msg msgdef.IMsg)

发消息

func (*NetSess) SendBuf

func (this *NetSess) SendBuf(data []byte)

发消息二进制数据

func (*NetSess) SendNow

func (this *NetSess) SendNow(msg msgdef.IMsg)

立即发送 实际与Send没有区别

func (*NetSess) SetCompressEnabled

func (this *NetSess) SetCompressEnabled(v bool)

设置是否压缩

func (*NetSess) SetEncryEnabled

func (this *NetSess) SetEncryEnabled(v bool)

设置是否加密

func (*NetSess) SetFragmentConfig

func (this *NetSess) SetFragmentConfig(sendEnabled, recvEnabled bool, size int)

SetFragmentConfig 设置分片配置 sendEnabled: 发送分片开关,true时大消息自动拆包发送 recvEnabled: 接收分片开关,true时自动合包重组 size: 分片大小,默认64KB,必须小于MaxMsgBuffer

func (*NetSess) SetID

func (this *NetSess) SetID(v uint64)

设置表示连接的ID,一般与上层的服务器ID或是用户ID一致

func (*NetSess) SetInputMsg

func (this *NetSess) SetInputMsg(v bool)

设置是否输出 发出的消息

func (*NetSess) SetMsgHandler

func (this *NetSess) SetMsgHandler(msgh msghandler.IMsgHandler)

设置消息路由 正常情况下都是使用外部的,验证前使用管理器的,验证后使用对象的

func (*NetSess) SetRecvMsg

func (this *NetSess) SetRecvMsg(v bool)

设置是否输出 收到的消息

func (*NetSess) SetType

func (this *NetSess) SetType(stype global.ServerTypeEnum)

设置如果连接是服务器过来的话,那就是服务器类型,2表示客户端

func (*NetSess) SetVerify

func (this *NetSess) SetVerify()

设置验证过

func (*NetSess) Start

func (this *NetSess) Start()

开始

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 NewNetServer(cfg *global.ListenCfg, thgo *threads.ThreadGo, realPtr INetServer, opts ...optionF) (result *NetSrv)

func (*NetSrv) GetClientSess

func (this *NetSrv) GetClientSess(id uint64) (result *NetSess)

获取会话

func (*NetSrv) GetCompressEnabled

func (this *NetSrv) GetCompressEnabled() bool

是否压缩

func (*NetSrv) GetEncryEnabled

func (this *NetSrv) GetEncryEnabled() bool

是否加密

func (*NetSrv) GetProtocal

func (this *NetSrv) GetProtocal() ProtocalEnum

连接类型

func (*NetSrv) PutSess

func (this *NetSrv) PutSess(sess INetSess)

把连接放进集合中

func (*NetSrv) RangeSess

func (this *NetSrv) RangeSess(f func(sess INetSess) bool)

遍历所有被动连接,返回false就退出遍历

func (*NetSrv) SessClosed

func (this *NetSrv) SessClosed(sess INetSess)

连接被关闭的时候

func (*NetSrv) SessVerify

func (this *NetSrv) SessVerify(msg msgdef.ISessionVerifyReqMsg, sess INetSess) error

会话的验证回调 如果是连接客户端的上层需要重载实现验证逻辑,失败就返回错误,底层会关闭连接

func (*NetSrv) Start

func (this *NetSrv) Start() (err error)

开始服务

func (*NetSrv) Stop

func (this *NetSrv) Stop()

结束服务

type ProtocalEnum

type ProtocalEnum = string

ProtocalEnum 连接类型

const (
	Protocal_Tcp       ProtocalEnum = "tcp"
	Protocal_Kcp       ProtocalEnum = "kcp"
	Protocal_WebSocket ProtocalEnum = "ws"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL