sess

package
v0.0.0-...-0012533 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// MaxMsgBuffer 消息最大长度
	MaxMsgBuffer = 100 * 1024
)
View Source
const (

	//这是包含msgID的长度
	MsgHeadSize = 6
)

Variables

View Source
var (
	//未知监听类型
	Err_Unknown_ProtocalEnum = common.NewError("Unknown ProtocalEnum:%s")
)

Functions

func DecodeMsg

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

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

func DecryptData

func DecryptData(buf []byte) []byte

DecryptData 解密算法

func EncodeMsg

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

func EncodeMsgWithEncrypt

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

EncodeMsg 序列化消息

func EncryptData

func EncryptData(buf []byte) []byte

EncryptData 加密算法

func GetDecrypt_key

func GetDecrypt_key() []byte

func GetEncrypt_key

func GetEncrypt_key() []byte

func GetMsgID

func GetMsgID(buf []byte) uint16

GetMsgID 获取消息ID

func NetSrvSetConfig

func NetSrvSetConfig(v *global.ListenCfg) optionF

通过配置表加载配置

func NetSrvSetEnabled

func NetSrvSetEnabled(v bool) optionF

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

func NetSrvSetIsClient

func NetSrvSetIsClient(v bool) optionF

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

func NetSrvSetMaxConn

func NetSrvSetMaxConn(v int) optionF

设置监听链接上限,默认为0(无上限)

func NetSrvSetMsgHander

func NetSrvSetMsgHander(v *msghandler.MsgHandler) optionF

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

func NetSrvSetProtocal

func NetSrvSetProtocal(v ProtocalEnum) optionF

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

func SetDecrypt_key

func SetDecrypt_key(bytes []byte)

func SetEncrypt_key

func SetEncrypt_key(bytes []byte)

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(msg msgdef.IMsg)
	//开始
	Start()
	//关闭 只会被运行一次
	Close()

	//设置消息路由
	//一开始使用的都是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()
}

func Dial

func Dial(protocal ProtocalEnum, addr string, thgo *threads.ThreadGo) (INetSess, error)

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

Dial 创建一个连接会话

type ISessCallBack

type ISessCallBack interface {
	//连接被关闭的时候(这个回调会在任意协程上,所以需要注意安全)
	SessClosed(sess INetSess)
	//会话的验证回调 如果是连接客户端的上层需要重载实现验证逻辑,失败就返回错误,底层会关闭连接
	SessVerify(msg msgdef.ISessionVerifyReqMsg, sess INetSess) error
}

type NetSess

type NetSess struct {
	// contains filtered or unexported fields
}

一个连接会话

func (*NetSess) Close

func (this *NetSess) Close()

关闭 只会被运行一次

func (*NetSess) GetID

func (this *NetSess) GetID() uint64

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

func (*NetSess) GetType

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

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

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)

立即发送

func (*NetSess) SetID

func (this *NetSess) SetID(v uint64)

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

func (*NetSess) SetMsgHandler

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

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

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.MsgHandler
	// contains filtered or unexported fields
}

网络连接的监听器 处理监听,管理连接会话,基础的连接验证

func NewNetServer

func NewNetServer(addr string, thgo *threads.ThreadGo, realPtr INetServer, opts ...optionF) (result *NetSrv)

func (*NetSrv) GetClientSess

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

获取会话

func (*NetSrv) GetEncryEnabled

func (this *NetSrv) GetEncryEnabled() bool

是否加密

func (*NetSrv) GetProtocal

func (this *NetSrv) GetProtocal() ProtocalEnum

连接类型

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

连接类型

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

Jump to

Keyboard shortcuts

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