znet

package
v0.0.0-...-ef14408 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: GPL-3.0 Imports: 20 Imported by: 1

Documentation

Overview

服务端Server的链接模块

Index

Constants

View Source
const (
	PRE_HANDLE  ziface.HandleStep = iota //PreHandle 预处理
	HANDLE                               //Handle 处理
	POST_HANDLE                          //PostHandle 后处理

	HANDLE_OVER
)

Variables

View Source
var AcceptDelay *acceptDelay

Functions

func NewClient

func NewClient(ip string, port int, opts ...ClientOption) ziface.IClient

func NewHeartbeatChecker

func NewHeartbeatChecker(interval time.Duration) ziface.IHeartbeatChecker

NewHeartbeatChecker 创建心跳检测器

func NewRequest

func NewRequest(conn ziface.IConnection, msg ziface.IMessage) ziface.IRequest

func NewServer

func NewServer(opts ...Option) ziface.IServer

NewServer 创建一个服务器句柄

func NewTLSClient

func NewTLSClient(ip string, port int, opts ...ClientOption) ziface.IClient

func NewUserConfServer

func NewUserConfServer(config *zconf.Config, opts ...Option) ziface.IServer

NewServer 创建一个服务器句柄

func NewWsClient

func NewWsClient(ip string, port int, opts ...ClientOption) ziface.IClient

Types

type BaseRouter

type BaseRouter struct{}

BaseRouter 实现router时,先嵌入这个基类,然后根据需要对这个基类的方法进行重写

func (*BaseRouter) Handle

func (br *BaseRouter) Handle(req ziface.IRequest)

Handle -

func (*BaseRouter) PostHandle

func (br *BaseRouter) PostHandle(req ziface.IRequest)

PostHandle -

func (*BaseRouter) PreHandle

func (br *BaseRouter) PreHandle(req ziface.IRequest)

PreHandle -

type Client

type Client struct {
	//目标链接服务器的IP
	Ip string
	//目标链接服务器的端口
	Port int

	// errChan
	ErrChan chan error
	// contains filtered or unexported fields
}

func (*Client) AddInterceptor

func (c *Client) AddInterceptor(interceptor ziface.IInterceptor)

func (*Client) AddRouter

func (c *Client) AddRouter(msgID uint32, router ziface.IRouter)

func (*Client) Conn

func (c *Client) Conn() ziface.IConnection

func (*Client) GetLengthField

func (c *Client) GetLengthField() *ziface.LengthField

func (*Client) GetMsgHandler

func (c *Client) GetMsgHandler() ziface.IMsgHandle

func (*Client) GetOnConnStart

func (c *Client) GetOnConnStart() func(ziface.IConnection)

GetOnConnStart 得到该Server的连接创建时Hook函数

func (*Client) GetOnConnStop

func (c *Client) GetOnConnStop() func(ziface.IConnection)

得到该Server的连接断开时的Hook函数

func (*Client) GetPacket

func (c *Client) GetPacket() ziface.IDataPack

获取Client绑定的数据协议封包方式

func (*Client) SetDecoder

func (c *Client) SetDecoder(decoder ziface.IDecoder)

func (*Client) SetOnConnStart

func (c *Client) SetOnConnStart(hookFunc func(ziface.IConnection))

设置该Client的连接创建时Hook函数

func (*Client) SetOnConnStop

func (c *Client) SetOnConnStop(hookFunc func(ziface.IConnection))

设置该Client的连接断开时的Hook函数

func (*Client) SetPacket

func (c *Client) SetPacket(packet ziface.IDataPack)

设置Client绑定的数据协议封包方式

func (*Client) Start

func (c *Client) Start()

启动客户端,发送请求且建立链接

func (*Client) StartHeartBeat

func (c *Client) StartHeartBeat(interval time.Duration)

StartHeartBeat 启动心跳检测 interval 每次发送心跳的时间间隔

func (*Client) StartHeartBeatWithOption

func (c *Client) StartHeartBeatWithOption(interval time.Duration, option *ziface.HeartBeatOption)

启动心跳检测(自定义回调)

func (*Client) Stop

func (c *Client) Stop()

type ClientOption

type ClientOption func(c ziface.IClient)

Client的客户端Option

func WithPacketClient

func WithPacketClient(pack ziface.IDataPack) ClientOption

Client的客户端Option

type ConnManager

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

ConnManager 连接管理模块

func NewConnManager

func NewConnManager() *ConnManager

NewConnManager 创建一个链接管理

func (*ConnManager) Add

func (connMgr *ConnManager) Add(conn ziface.IConnection)

Add 添加链接

func (*ConnManager) ClearConn

func (connMgr *ConnManager) ClearConn()

ClearConn 清除并停止所有连接

func (*ConnManager) Get

func (connMgr *ConnManager) Get(connID uint64) (ziface.IConnection, error)

Get 利用ConnID获取链接

func (*ConnManager) GetAllConnID

func (connMgr *ConnManager) GetAllConnID() []uint64

GetAllConnID 获取所有连接的ID

func (*ConnManager) Len

func (connMgr *ConnManager) Len() int

Len 获取当前连接

func (*ConnManager) Range

func (connMgr *ConnManager) Range(cb func(uint64, ziface.IConnection, interface{}) error, args interface{}) (err error)

Range 遍历所有连接

func (*ConnManager) Remove

func (connMgr *ConnManager) Remove(conn ziface.IConnection)

Remove 删除连接

type Connection

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

Connection Tcp连接模块 用于处理Tcp连接的读写业务 一个连接对应一个Connection

func (*Connection) Context

func (c *Connection) Context() context.Context

返回ctx,用于用户自定义的go程获取连接退出状态

func (*Connection) GetConnID

func (c *Connection) GetConnID() uint64

GetConnID 获取当前连接ID

func (*Connection) GetConnection

func (c *Connection) GetConnection() net.Conn

func (*Connection) GetProperty

func (c *Connection) GetProperty(key string) (interface{}, error)

GetProperty 获取链接属性

func (*Connection) GetTCPConnection deprecated

func (c *Connection) GetTCPConnection() net.Conn

Deprecated: use GetConnection instead

func (*Connection) GetWsConn

func (c *Connection) GetWsConn() *websocket.Conn

func (*Connection) IsAlive

func (c *Connection) IsAlive() bool

func (*Connection) LocalAddr

func (c *Connection) LocalAddr() net.Addr

LocalAddr 获取链接本地地址信息

func (*Connection) RemoteAddr

func (c *Connection) RemoteAddr() net.Addr

RemoteAddr 获取链接远程地址信息

func (*Connection) RemoveProperty

func (c *Connection) RemoveProperty(key string)

RemoveProperty 移除链接属性

func (*Connection) Send

func (c *Connection) Send(data []byte) error

func (*Connection) SendBuffMsg

func (c *Connection) SendBuffMsg(msgID uint32, data []byte) error

SendBuffMsg 发生BuffMsg

func (*Connection) SendMsg

func (c *Connection) SendMsg(msgID uint32, data []byte) error

SendMsg 直接将Message数据发送数据给远程的TCP客户端

func (*Connection) SendToQueue

func (c *Connection) SendToQueue(data []byte) error

func (*Connection) SetHeartBeat

func (c *Connection) SetHeartBeat(checker ziface.IHeartbeatChecker)

func (*Connection) SetProperty

func (c *Connection) SetProperty(key string, value interface{})

SetProperty 设置链接属性

func (*Connection) Start

func (c *Connection) Start()

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

func (*Connection) StartReader

func (c *Connection) StartReader()

StartReader 读消息Goroutine,用于从客户端中读取数据

func (*Connection) StartWriter

func (c *Connection) StartWriter()

StartWriter 写消息Goroutine, 用户将数据发送给客户端

func (*Connection) Stop

func (c *Connection) Stop()

Stop 停止连接,结束当前连接状态M

type HeartbeatChecker

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

func (*HeartbeatChecker) BindConn

func (h *HeartbeatChecker) BindConn(conn ziface.IConnection)

BindConn 绑定一个链接

func (*HeartbeatChecker) BindRouter

func (h *HeartbeatChecker) BindRouter(msgID uint32, router ziface.IRouter)

func (*HeartbeatChecker) Clone

CloneTo 克隆到一个指定的链接上

func (*HeartbeatChecker) MsgID

func (h *HeartbeatChecker) MsgID() uint32

func (*HeartbeatChecker) Router

func (h *HeartbeatChecker) Router() ziface.IRouter

func (*HeartbeatChecker) SendHeartBeatMsg

func (h *HeartbeatChecker) SendHeartBeatMsg() error

func (*HeartbeatChecker) SetHeartbeatFunc

func (h *HeartbeatChecker) SetHeartbeatFunc(beatFunc ziface.HeartBeatFunc)

func (*HeartbeatChecker) SetHeartbeatMsgFunc

func (h *HeartbeatChecker) SetHeartbeatMsgFunc(f ziface.HeartBeatMsgFunc)

func (*HeartbeatChecker) SetOnRemoteNotAlive

func (h *HeartbeatChecker) SetOnRemoteNotAlive(f ziface.OnRemoteNotAlive)

func (*HeartbeatChecker) Start

func (h *HeartbeatChecker) Start()

Start 启动心跳检测

func (*HeartbeatChecker) Stop

func (h *HeartbeatChecker) Stop()

停止心跳检测

type HeatBeatDefaultRouter

type HeatBeatDefaultRouter struct {
	BaseRouter
}

收到remote心跳消息的默认回调路由业务

func (*HeatBeatDefaultRouter) Handle

func (r *HeatBeatDefaultRouter) Handle(req ziface.IRequest)

Handle -

type MsgHandle

type MsgHandle struct {
	Apis           map[uint32]ziface.IRouter // 存放每个MsgID 所对应的处理方法的map属性
	WorkerPoolSize uint32                    // 业务工作Worker池的数量
	TaskQueue      []chan ziface.IRequest    // Worker负责取任务的消息队列
	// contains filtered or unexported fields
}

MsgHandle 对消息的处理回调模块

func NewMsgHandle

func NewMsgHandle() *MsgHandle

NewMsgHandle 创建MsgHandle

func (*MsgHandle) AddInterceptor

func (mh *MsgHandle) AddInterceptor(interceptor ziface.IInterceptor)

func (*MsgHandle) AddRouter

func (mh *MsgHandle) AddRouter(msgID uint32, router ziface.IRouter)

AddRouter 为消息添加具体的处理逻辑

func (*MsgHandle) Execute

func (mh *MsgHandle) Execute(request ziface.IRequest)

func (*MsgHandle) Intercept

func (mh *MsgHandle) Intercept(chain ziface.IChain) ziface.IcResp

func (*MsgHandle) SendMsgToTaskQueue

func (mh *MsgHandle) SendMsgToTaskQueue(request ziface.IRequest)

SendMsgToTaskQueue 将消息交给TaskQueue,由worker进行处理

func (*MsgHandle) StartOneWorker

func (mh *MsgHandle) StartOneWorker(workerID int, taskQueue chan ziface.IRequest)

StartOneWorker 启动一个Worker工作流程

func (*MsgHandle) StartWorkerPool

func (mh *MsgHandle) StartWorkerPool()

StartWorkerPool 启动worker工作池

type Option

type Option func(s *Server)

Server的服务Option

func WithPacket

func WithPacket(pack ziface.IDataPack) Option

只要实现Packet 接口可自由实现数据包解析格式,如果没有则使用默认解析格式

type Request

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

Request 请求

func (*Request) Abort

func (r *Request) Abort()

func (*Request) BindRouter

func (r *Request) BindRouter(router ziface.IRouter)

func (*Request) Call

func (r *Request) Call()

func (*Request) GetConnection

func (r *Request) GetConnection() ziface.IConnection

GetConnection 获取请求连接信息

func (*Request) GetData

func (r *Request) GetData() []byte

GetData 获取请求消息的数据

func (*Request) GetMessage

func (r *Request) GetMessage() ziface.IMessage

GetMessage 获取消息实体

func (*Request) GetMsgID

func (r *Request) GetMsgID() uint32

GetMsgID 获取请求的消息的ID

func (*Request) GetResponse

func (r *Request) GetResponse() ziface.IcResp

func (*Request) Goto

func (r *Request) Goto(step ziface.HandleStep)

func (*Request) SetResponse

func (r *Request) SetResponse(response ziface.IcResp)

type Server

type Server struct {
	//服务器的名称
	Name string
	//tcp4 or other
	IPVersion string
	//服务绑定的IP地址
	IP string
	//服务绑定的端口
	Port int

	//当前Server的链接管理器
	ConnMgr ziface.IConnManager
	// contains filtered or unexported fields
}

Server 接口实现,定义一个Server服务类

func (*Server) AddInterceptor

func (s *Server) AddInterceptor(interceptor ziface.IInterceptor)

func (*Server) AddRouter

func (s *Server) AddRouter(msgID uint32, router ziface.IRouter)

AddRouter 路由功能:给当前服务注册一个路由业务方法,供客户端链接处理使用

func (*Server) GetConnMgr

func (s *Server) GetConnMgr() ziface.IConnManager

GetConnMgr 得到链接管理

func (*Server) GetHeartBeat

func (s *Server) GetHeartBeat() ziface.IHeartbeatChecker

func (*Server) GetLengthField

func (s *Server) GetLengthField() *ziface.LengthField

func (*Server) GetMsgHandler

func (s *Server) GetMsgHandler() ziface.IMsgHandle

func (*Server) GetOnConnStart

func (s *Server) GetOnConnStart() func(ziface.IConnection)

GetOnConnStart 得到该Server的连接创建时Hook函数

func (*Server) GetOnConnStop

func (s *Server) GetOnConnStop() func(ziface.IConnection)

得到该Server的连接断开时的Hook函数

func (*Server) GetPacket

func (s *Server) GetPacket() ziface.IDataPack

func (*Server) Serve

func (s *Server) Serve()

Serve 运行服务

func (*Server) SetDecoder

func (s *Server) SetDecoder(decoder ziface.IDecoder)

func (*Server) SetOnConnStart

func (s *Server) SetOnConnStart(hookFunc func(ziface.IConnection))

SetOnConnStart 设置该Server的连接创建时Hook函数

func (*Server) SetOnConnStop

func (s *Server) SetOnConnStop(hookFunc func(ziface.IConnection))

SetOnConnStop 设置该Server的连接断开时的Hook函数

func (*Server) SetPacket

func (s *Server) SetPacket(packet ziface.IDataPack)

func (*Server) Start

func (s *Server) Start()

Start 开启网络服务

func (*Server) StartHeartBeat

func (s *Server) StartHeartBeat(interval time.Duration)

StartHeartBeat 启动心跳检测 interval 每次发送心跳的时间间隔

func (*Server) StartHeartBeatWithOption

func (s *Server) StartHeartBeatWithOption(interval time.Duration, option *ziface.HeartBeatOption)

StartHeartBeatWithFunc 启动心跳检测 option 心跳检测的配置

func (*Server) Stop

func (s *Server) Stop()

Stop 停止服务

type WsConnection

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

WsConnection Websocket连接模块 用于处理 Websocket 连接的读写业务 一个连接对应一个Connection

func (*WsConnection) Context

func (c *WsConnection) Context() context.Context

返回ctx,用于用户自定义的go程获取连接退出状态

func (*WsConnection) GetConnID

func (c *WsConnection) GetConnID() uint64

GetConnID 获取当前连接ID

func (*WsConnection) GetConnection

func (c *WsConnection) GetConnection() net.Conn

func (*WsConnection) GetProperty

func (c *WsConnection) GetProperty(key string) (interface{}, error)

GetProperty 获取链接属性

func (*WsConnection) GetTCPConnection deprecated

func (c *WsConnection) GetTCPConnection() net.Conn

Deprecated: use GetConnection instead

func (*WsConnection) GetWsConn

func (c *WsConnection) GetWsConn() *websocket.Conn

func (*WsConnection) IsAlive

func (c *WsConnection) IsAlive() bool

func (*WsConnection) LocalAddr

func (c *WsConnection) LocalAddr() net.Addr

LocalAddr 获取链接本地地址信息

func (*WsConnection) RemoteAddr

func (c *WsConnection) RemoteAddr() net.Addr

RemoteAddr 获取链接远程地址信息

func (*WsConnection) RemoveProperty

func (c *WsConnection) RemoveProperty(key string)

RemoveProperty 移除链接属性

func (*WsConnection) Send

func (c *WsConnection) Send(data []byte) error

func (*WsConnection) SendBuffMsg

func (c *WsConnection) SendBuffMsg(msgID uint32, data []byte) error

SendBuffMsg 发生BuffMsg

func (*WsConnection) SendMsg

func (c *WsConnection) SendMsg(msgID uint32, data []byte) error

SendMsg 直接将Message数据发送数据给远程的TCP客户端

func (*WsConnection) SendToQueue

func (c *WsConnection) SendToQueue(data []byte) error

func (*WsConnection) SetHeartBeat

func (c *WsConnection) SetHeartBeat(checker ziface.IHeartbeatChecker)

func (*WsConnection) SetProperty

func (c *WsConnection) SetProperty(key string, value interface{})

SetProperty 设置链接属性

func (*WsConnection) Start

func (c *WsConnection) Start()

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

func (*WsConnection) StartReader

func (c *WsConnection) StartReader()

StartReader 读消息Goroutine,用于从客户端中读取数据

func (*WsConnection) StartWriter

func (c *WsConnection) StartWriter()

StartWriter 写消息Goroutine, 用户将数据发送给客户端

func (*WsConnection) Stop

func (c *WsConnection) Stop()

Stop 停止连接,结束当前连接状态M

Jump to

Keyboard shortcuts

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