iface

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IConnection

type IConnection interface {
	// 启动连接(通过connmanager调用)
	Start(readerHandler func() bool, writerHandler func(data []byte) bool)
	// 停止连接(通过connmanager调用)
	Stop() bool

	// 启动接收消息协程
	StartReader() bool
	// 启动发送消息协程
	StartWriter(data []byte) bool
	// 执行任务
	DoTask(task func())

	// 获取当前连接Id
	GetConnId() int
	// 获取客户端地址信息
	RemoteAddrStr() string
	// 获取连接是否已关闭
	IsClose() bool
	// 获取连接绑定的属性列表
	GetProperty() any

	// 发送消息给客户端
	SendMsg(msgId int32, msgData proto.Message)

	// 限流控制
	FlowControl() bool

	// 序列化
	ProtocolToByte(str proto.Message) []byte
	// 反序列化
	ByteToProtocol(byte []byte, target proto.Message) error
}

type IConnectionManager

type IConnectionManager interface {
	// 生成一个新的连接Id
	NewConnId() int
	// 遍历所有连接
	RangeConnections(handler func(conn IConnection))
	// 添加连接
	Add(conn IConnection)
	// 删除连接
	Remove(conn IConnection)
	// 根据ConnId获取连接
	Get(connId int) (conn IConnection, ok bool)
	// 获取当前连接数量
	Len() int
	// 删除并停止所有连接
	ClearConn()

	// 设置连接创建时的Hook函数
	SetConnOnOpened(connOpenCallBack func(conn IConnection))
	// 执行连接创建时的Hook函数
	ConnOnOpened(conn IConnection)

	// 设置连接断开时的Hook函数
	SetConnOnClosed(connCloseCallBack func(conn IConnection))
	// 执行连接断开时的Hook函数
	ConnOnClosed(conn IConnection)

	// 触发限流时的Hook函数
	SetConnOnRateLimiting(limitCallBack func(conn IConnection))
	// 执行触发限流时的Hook函数
	ConnRateLimiting(conn IConnection)
}

连接管理器

type IDataPack

type IDataPack interface {
	// 获取消息头长度
	GetHeadLen() int
	// 消息封包
	Pack(msg IMessage) []byte
	// 消息拆包
	UnPack([]byte) IMessage
}

封包拆包,通过固定的包头获取消息数据,解决TCP粘包问题

type IErrCapture

type IErrCapture func(conn IConnection, msg IMessage, panicInfo string)

type IFilter

type IFilter func(conn IConnection, msg IMessage) bool

type IMessage

type IMessage interface {
	// 获取消息Id
	GetMsgId() uint16
	// 获取消息长度
	GetDataLen() uint16
	// 获取消息内容
	GetData() []byte
	// 设置消息内容
	SetData([]byte)
}

定义消息模板

type IMsgHandler

type IMsgHandler interface {
	// 获取已注册的协议列表
	GetApis() map[int32]IRouter
	// 为消息注册解析体和处理函数
	AddRouter(msgId int32, msgTemplate INewMsgStructTemplate, msgHandler IReceiveMsgHandler)
	// 设置过滤器
	SetFilter(fun IFilter)
	// 获取过滤器
	GetFilter() IFilter
	// 设置错误捕获
	SetErrCapture(fun IErrCapture)
	// 获取错误捕获
	GetErrCapture(conn IConnection, msg IMessage)
}

消息处理器

type INewMsgStructTemplate

type INewMsgStructTemplate func() proto.Message

type IReceiveMsgHandler

type IReceiveMsgHandler func(conn IConnection, message proto.Message)

type IRouter

type IRouter interface {
	// 设置消息体
	SetMsg(msgTemplate INewMsgStructTemplate)
	// 获取新的空消息结构体
	GetNewMsg() proto.Message
	// 设置处理函数
	SetHandler(msgHandler IReceiveMsgHandler)
	// 执行处理函数
	RunHandler(conn IConnection, message proto.Message)
}

定义协议路由模板

type IServer

type IServer interface {
	// 获取服务器名称
	GetServerName() string
	// 启动服务器
	Start()
}

定义服务器接口

type IServerManager

type IServerManager interface {
	// 注册服务
	RegisterServer(server ...IServer)
	// 获取启动的服务列表
	Servers() []IServer
	// 获取服务是否已关闭
	IsClose() bool
	// 增加连接断开等待执行计数
	WaitGroupAdd(delta int)
	// 连接断开hook已完成
	WaitGroupDone()
	// 停止所有服务
	StopAll()
}

Jump to

Keyboard shortcuts

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