tiface

package
v0.0.0-...-5b63d95 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IConnManager

type IConnManager interface {
	Add(conn IConnection)                   // 添加链接
	Remove(conn IConnection)                // 删除连接
	Get(connID uint32) (IConnection, error) // 根据ConnID获取链接
	Len() int                               // 获取当前连接总数
	ClearConn()                             // 删除并停止所有链接
}

连接管理抽象层

type IConnection

type IConnection interface {

	// 启动连接,让当前连接开始工作
	Start()

	// 停止连接,结束当前连接状态
	Stop()

	// 从当前连接获取原始的socket TCPConn
	GetTCPConnection() *net.TCPConn

	// 获取当前连接ID
	GetConnID() uint32

	// 获取远程客户端地址信息
	RemoteAddr() net.Addr

	// 将数据发送给无缓冲队列,通过专门从队列读数据的goroutine写给TCP客户端(无缓冲)
	SendMsg(msgId uint32, data []byte) error

	// 将数据发送给有缓冲队列,通过专门从缓冲队列读数据的goroutine写给TCP客户端(有缓冲)
	SendBuffMsg(msgId uint32, data []byte) error

	// 设置链接属性
	SetProperty(key string, value interface{})

	// 获取链接属性
	GetProperty(key string) (interface{}, error)

	// 移除链接属性
	RemoveProperty(key string)
}

定义连接接口

type IDataPack

type IDataPack interface {
	GetHeadLen() uint32                // 获取包头长度方法
	Pack(msg IMessage) ([]byte, error) // 封包方法
	Unpack([]byte) (IMessage, error)   // 拆包方法
}

封包数据和拆包数据 直接面向TCP连接中的数据流,采用经典的TLV(Type-Len-Value)封包格式,为传输数据添加头部信息,用于解决TCP粘包问题。

type IMessage

type IMessage interface {
	GetMsgId() uint32   // 获取消息ID
	GetDataLen() uint32 // 获取消息数据段长度
	GetData() []byte    // 获取消息内容

	SetMsgId(uint32)   // 设置消息ID
	SetDataLen(uint32) // 设置消息数据段长度
	SetData([]byte)    // 设置消息内容
}

将请求的一个消息封装到message中,定义抽象层接口

type IMsgHandle

type IMsgHandle interface {
	DoMsgHandler(request IRequest)          // 马上以非阻塞方式处理消息,调度/执行对应的Router消息处理方法
	AddRouter(msgId uint32, router IRouter) // 为消息添加具体的处理逻辑
	StartWorkerPool()                       // 启动worker工作池
	SendMsgToTaskQueue(request IRequest)    // 将消息交给TaskQueue,由worker进行处理
}

消息管理抽象层

type IRequest

type IRequest interface {
	GetConnection() IConnection // 获取请求的链接信息
	GetData() []byte            // 获取请求的消息数据
	GetMsgID() uint32           //获取请求的消息ID
}

IRequest 接口: 实际上是把客户端请求的链接信息 和 请求的数据 包装到了 Request里

type IRouter

type IRouter interface {
	PreHandle(request IRequest)  //在处理conn业务之前的钩子方法
	Handle(request IRequest)     //处理conn业务的主方法
	PostHandle(request IRequest) //处理conn业务之后的钩子方法
}

路由接口, 这里面路由是 使用框架者给该链接自定的 处理业务方法 路由里的IRequest 则包含用该链接的链接信息和该链接的请求数据信息

type IServer

type IServer interface {
	//启动服务器方法
	Start()

	//停止服务器方法
	Stop()

	//开启业务服务方法
	Serve()

	//路由功能:给当前的服务注册一个路由方法,供客户端链接处理使用
	AddRouter(msgId uint32, router IRouter)

	//得到当前server的链接管理模块
	GetConnMgr() IConnManager

	//设置该Server的连接创建时Hook函数
	SetOnConnStart(func(IConnection))

	//设置该Server的连接断开时的Hook函数
	SetOnConnStop(func(IConnection))

	//调用连接OnConnStart Hook函数
	CallOnConnStart(conn IConnection)

	//调用连接OnConnStop Hook函数
	CallOnConnStop(conn IConnection)
}

Jump to

Keyboard shortcuts

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