ziface

package
v1.1.17 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2023 License: GPL-3.0 Imports: 6 Imported by: 194

Documentation

Overview

Package ziface 主要提供zinx全部抽象层接口定义. 包括:

		IServer 服务mod接口
		IRouter 路由mod接口
		IConnection 连接mod层接口
     IMessage 消息mod接口
		IDataPack 消息拆解接口
     IMsgHandler 消息处理及协程池接口
     IClient 客户端接口

当前文件描述: @Title iclient.go @Description 提供Client抽象层全部接口声明 @Author Aceld - 2023-2-28

Package ziface 主要提供zinx全部抽象层接口定义. 包括:

		IServer 服务mod接口
		IRouter 路由mod接口
		IConnection 连接mod层接口
     IMessage 消息mod接口
		IDataPack 消息拆解接口
     IMsgHandler 消息处理及协程池接口

当前文件描述: @Title iconnection.go @Description 全部连接相关方法声明 @Author Aceld - Thu Mar 11 10:32:29 CST 2019

Package ziface 主要提供zinx全部抽象层接口定义. 包括:

		IServer 服务mod接口
		IRouter 路由mod接口
		IConnection 连接mod层接口
     IMessage 消息mod接口
		IDataPack 消息拆解接口
     IMsgHandler 消息处理及协程池接口

当前文件描述: @Title iconnmanager.go @Description 连接管理相关,包括添加、删除、通过一个连接ID获得连接对象,当前连接数量、清空全部连接等方法 @Author Aceld - Thu Mar 11 10:32:29 CST 2019

Package ziface 主要提供zinx全部抽象层接口定义. 包括:

		IServer 服务mod接口
		IRouter 路由mod接口
		IConnection 连接mod层接口
     IMessage 消息mod接口
		IDataPack 消息拆解接口
     IMsgHandler 消息处理及协程池接口

当前文件描述: @Title idatapack.go @Description 消息的打包和解包方法 @Author Aceld - Thu Mar 11 10:32:29 CST 2019

Package ziface 主要提供zinx全部抽象层接口定义. 包括:

		IServer 服务mod接口
		IRouter 路由mod接口
		IConnection 连接mod层接口
     IMessage 消息mod接口
		IDataPack 消息拆解接口
     IMsgHandler 消息处理及协程池接口

当前文件描述: @Title imessage.go @Description 提供消息的基本方法 @Author Aceld - Thu Mar 11 10:32:29 CST 2019

Package ziface 主要提供zinx全部抽象层接口定义. 包括:

		IServer 服务mod接口
		IRouter 路由mod接口
		IConnection 连接mod层接口
     IMessage 消息mod接口
		IDataPack 消息拆解接口
     IMsgHandler 消息处理及协程池接口

当前文件描述: @Title imsghandler.go @Description 提供worker启动、处理消息业务调用等接口 @Author Aceld - Thu Mar 11 10:32:29 CST 2019

Package ziface 主要提供zinx全部抽象层接口定义. 包括:

		IServer 服务mod接口
		IRouter 路由mod接口
		IConnection 连接mod层接口
     IMessage 消息mod接口
		IDataPack 消息拆解接口
     IMsgHandler 消息处理及协程池接口

当前文件描述: @Title irequest.go @Description 提供连接请求全部接口声明 @Author Aceld - Thu Mar 11 10:32:29 CST 2019

Package ziface 主要提供zinx全部抽象层接口定义. 包括:

		IServer 服务mod接口
		IRouter 路由mod接口
		IConnection 连接mod层接口
     IMessage 消息mod接口
		IDataPack 消息拆解接口
     IMsgHandler 消息处理及协程池接口

当前文件描述: @Title irouter.go @Description 提供消息路由全部接口声明 @Author Aceld - Thu Mar 11 10:32:29 CST 2019

Package ziface 主要提供zinx全部抽象层接口定义. 包括:

		IServer 服务mod接口
		IRouter 路由mod接口
		IConnection 连接mod层接口
     IMessage 消息mod接口
		IDataPack 消息拆解接口
     IMsgHandler 消息处理及协程池接口

当前文件描述: @Title iserver.go @Description 提供Server抽象层全部接口声明 @Author Aceld - Thu Mar 11 10:32:29 CST 2019

Index

Constants

View Source
const (
	//Zinx 标准封包和拆包方式
	ZinxDataPack    string = "zinx_pack_tlv_big_endian"
	ZinxDataPackOld string = "zinx_pack_ltv_little_endian"
)
View Source
const (
	HeartBeatDefaultMsgID uint32 = 99999
)
View Source
const (
	//Zinx 默认标准报文协议格式
	ZinxMessage string = "zinx_message"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HandleStep added in v1.0.2

type HandleStep int

type HeartBeatFunc added in v1.1.15

type HeartBeatFunc func(IConnection) error

HeartBeatFunc 用户自定义心跳函数

type HeartBeatMsgFunc added in v1.1.0

type HeartBeatMsgFunc func(IConnection) []byte

用户自定义的心跳检测消息处理方法

type HeartBeatOption added in v1.1.0

type HeartBeatOption struct {
	MakeMsg          HeartBeatMsgFunc //用户自定义的心跳检测消息处理方法
	OnRemoteNotAlive OnRemoteNotAlive //用户自定义的远程连接不存活时的处理方法
	HeadBeatMsgID    uint32           //用户自定义的心跳检测消息ID
	Router           IRouter          //用户自定义的心跳检测消息业务处理路由
}

type IChain added in v1.1.15

type IChain interface {
	Request() IcReq        //获取当前责任链中的请求数据(当前拦截器)
	GetIMessage() IMessage //从Chain中获取IMessage
	Proceed(IcReq) IcResp  //进入并执行下一个拦截器,且将请求数据传递给下一个拦截器
	ProceedWithIMessage(IMessage, IcReq) IcResp
}

责任链

type IClient added in v1.1.0

type IClient interface {
	Start()
	Stop()
	AddRouter(msgID uint32, router IRouter)
	Conn() IConnection
	SetOnConnStart(func(IConnection))                         //设置该Client的连接创建时Hook函数
	SetOnConnStop(func(IConnection))                          //设置该Client的连接断开时的Hook函数
	GetOnConnStart() func(IConnection)                        //获取该Client的连接创建时Hook函数
	GetOnConnStop() func(IConnection)                         //设置该Client的连接断开时的Hook函数
	GetPacket() IDataPack                                     //获取Client绑定的数据协议封包方式
	SetPacket(IDataPack)                                      //设置Client绑定的数据协议封包方式
	GetMsgHandler() IMsgHandle                                //获取Client绑定的消息处理模块
	StartHeartBeat(time.Duration)                             //启动心跳检测
	StartHeartBeatWithOption(time.Duration, *HeartBeatOption) //启动心跳检测(自定义回调)
	GetLengthField() *LengthField
	SetDecoder(IDecoder)
	AddInterceptor(IInterceptor)
	GetErrChan() chan error // 获取客户端错误管道
}

type IConnManager

type IConnManager interface {
	Add(IConnection)                                                       //添加链接
	Remove(IConnection)                                                    //删除连接
	Get(uint64) (IConnection, error)                                       //利用ConnID获取链接
	Len() int                                                              //获取当前连接
	ClearConn()                                                            //删除并停止所有链接
	GetAllConnID() []uint64                                                //获取所有连接ID
	Range(func(uint64, IConnection, interface{}) error, interface{}) error //遍历所有连接
}

连接管理抽象层

type IConnection

type IConnection interface {
	Start()                   //启动连接,让当前连接开始工作
	Stop()                    //停止连接,结束当前连接状态
	Context() context.Context //返回ctx,用于用户自定义的go程获取连接退出状态

	GetConnection() net.Conn    //从当前连接获取原始的socket
	GetWsConn() *websocket.Conn // 从当前连接中获取原始的websocket连接
	// Deprecated: use GetConnection instead
	GetTCPConnection() net.Conn //从当前连接获取原始的socket TCPConn
	GetConnID() uint64          //获取当前连接ID
	RemoteAddr() net.Addr       //获取链接远程地址信息
	LocalAddr() net.Addr        //获取链接本地地址信息

	Send(data []byte) error
	SendToQueue(data []byte) error
	SendMsg(msgID uint32, data []byte) error     //直接将Message数据发送数据给远程的TCP客户端(无缓冲)
	SendBuffMsg(msgID uint32, data []byte) error //直接将Message数据发送给远程的TCP客户端(有缓冲)

	SetProperty(key string, value interface{})   //设置链接属性
	GetProperty(key string) (interface{}, error) //获取链接属性
	RemoveProperty(key string)                   //移除链接属性
	IsAlive() bool                               //判断当前连接是否存活
	SetHeartBeat(checker IHeartbeatChecker)      //设置心跳检测器
}

定义连接接口

type IDataPack

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

封包数据和拆包数据 直接面向TCP连接中的数据流,为传输数据添加头部信息,用于处理TCP粘包问题。

type IDecoder added in v1.1.1

type IDecoder interface {
	IInterceptor
	GetLengthField() *LengthField
}

type IFrameDecoder added in v1.1.15

type IFrameDecoder interface {
	Decode(buff []byte) [][]byte
}

type IHeartbeatChecker added in v1.1.12

type IHeartbeatChecker interface {
	SetOnRemoteNotAlive(OnRemoteNotAlive)
	SetHeartbeatMsgFunc(HeartBeatMsgFunc)
	SetHeartbeatFunc(HeartBeatFunc)
	BindRouter(uint32, IRouter)
	Start()
	Stop()
	SendHeartBeatMsg() error
	BindConn(IConnection)
	Clone() IHeartbeatChecker
	MsgID() uint32
	Router() IRouter
}

type IInterceptor added in v1.1.15

type IInterceptor interface {
	Intercept(IChain) IcResp //拦截器的拦截处理方法(由开发者定义)
}

拦截器

type ILogger added in v1.1.0

type ILogger interface {
	//没有context的日志接口
	InfoF(format string, v ...interface{})
	ErrorF(format string, v ...interface{})
	DebugF(format string, v ...interface{})

	//携带context的日志接口
	InfoFX(ctx context.Context, format string, v ...interface{})
	ErrorFX(ctx context.Context, format string, v ...interface{})
	DebugFX(ctx context.Context, format string, v ...interface{})
}

type IMessage

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

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

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

type IMsgHandle

type IMsgHandle interface {
	//为消息添加具体的处理逻辑, msgID,支持整型,字符串
	AddRouter(msgID uint32, router IRouter)
	StartWorkerPool()                    //启动worker工作池
	SendMsgToTaskQueue(request IRequest) //将消息交给TaskQueue,由worker进行处理

	Execute(request IRequest)                //执行责任链上的拦截器方法
	AddInterceptor(interceptor IInterceptor) //注册责任链任务入口,每个拦截器处理完后,数据都会传递至下一个拦截器,使得消息可以层层处理层层传递,顺序取决于注册顺序
}

消息管理抽象层

type IRequest

type IRequest interface {
	GetConnection() IConnection //获取请求连接信息

	GetData() []byte  //获取请求消息的数据
	GetMsgID() uint32 //获取请求的消息ID

	GetMessage() IMessage //获取请求消息的原始数据 add by uuxia 2023-03-10

	GetResponse() IcResp //获取解析完后序列化数据
	SetResponse(IcResp)  //设置解析完后序列化数据

	BindRouter(router IRouter) //绑定这次请求由哪个路由处理
	Call()                     //转进到下一个处理器开始执行 但是调用此方法的函数会根据先后顺序逆序执行
	Abort()                    //终止处理函数的运行 但调用此方法的函数会执行完毕
	//慎用,会导致循环调用
	Goto(HandleStep) //指定接下来的Handle去执行哪个Handler函数
}

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)                   //路由功能:给当前服务注册一个路由业务方法,供客户端链接处理使用
	GetConnMgr() IConnManager                                 //得到链接管理
	SetOnConnStart(func(IConnection))                         //设置该Server的连接创建时Hook函数
	SetOnConnStop(func(IConnection))                          //设置该Server的连接断开时的Hook函数
	GetOnConnStart() func(IConnection)                        //得到该Server的连接创建时Hook函数
	GetOnConnStop() func(IConnection)                         //得到该Server的连接断开时的Hook函数
	GetPacket() IDataPack                                     //获取Server绑定的数据协议封包方式
	GetMsgHandler() IMsgHandle                                //获取Server绑定的消息处理模块
	SetPacket(IDataPack)                                      //设置Server绑定的数据协议封包方式
	StartHeartBeat(time.Duration)                             //启动心跳检测
	StartHeartBeatWithOption(time.Duration, *HeartBeatOption) //启动心跳检测(自定义回调)
	GetHeartBeat() IHeartbeatChecker                          //获取心跳检测器
	GetLengthField() *LengthField
	SetDecoder(IDecoder)
	AddInterceptor(IInterceptor)
	SetWebsocketAuth(func(r *http.Request) error) // 添加websocket认证方法
}

定义服务接口

type IcReq added in v1.1.15

type IcReq interface{}

拦截器输入数据

type IcResp added in v1.1.15

type IcResp interface{}

拦截器输出数据

type Inotify added in v1.0.2

type Inotify interface {
	//是否有这个id
	HasIdConn(id uint64) bool
	//存储的map长度
	ConnNums() int
	//添加链接
	SetNotifyID(Id uint64, conn IConnection)
	//得到某个链接
	GetNotifyByID(Id uint64) (IConnection, error)
	//删除某个链接
	DelNotifyByID(Id uint64)
	//通知某个id的方法
	NotifyToConnByID(Id uint64, MsgId uint32, data []byte) error
	//通知所有人
	NotifyAll(MsgId uint32, data []byte) error
	//通过缓冲队列通知某个id的方法
	NotifyBuffToConnByID(Id uint64, MsgId uint32, data []byte) error
	//缓冲队列通知所有人
	NotifyBuffAll(MsgId uint32, data []byte) error
}

type LengthField added in v1.1.1

type LengthField struct {
	/*
		Note:
		大端模式:是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中,地址由小向大增加,而数据从高位往低位放;
		小端模式:是指数据的高字节保存在内存的高地址中,而数据的低字节保存在内存的低地址中,高地址部分权值高,低地址部分权值低,和我们的日常逻辑方法一致。
	*/
	Order               binary.ByteOrder //大小端
	MaxFrameLength      uint64           //最大帧长度
	LengthFieldOffset   int              //长度字段偏移量
	LengthFieldLength   int              //长度域字段的字节数
	LengthAdjustment    int              //长度调整
	InitialBytesToStrip int              //需要跳过的字节数
}

ILengthField 具备的基础属性

type OnRemoteNotAlive added in v1.1.0

type OnRemoteNotAlive func(IConnection)

用户自定义的远程连接不存活时的处理方法

Jump to

Keyboard shortcuts

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