znet

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewServer

func NewServer() ziface.IServer

NewServer 创建一个服务器句柄

Types

type BaseRouter

type BaseRouter struct{}

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

func (*BaseRouter) Handle

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

func (*BaseRouter) PostHandle

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

func (*BaseRouter) PreHandle

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

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 uint32) (ziface.IConnection, error)

Get 利用ConnID获取连接

func (*ConnManager) Len

func (connMgr *ConnManager) Len() int

Len 获取当前连接

func (*ConnManager) Remove

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

Remove 删除连接

type Connection

type Connection struct {
	// 当前Conn属于哪个Server
	TcpServer ziface.IServer
	// 当前连接的socket TCP套接字
	Conn *net.TCPConn
	// 当前连接的ID 也可以称作为SessionID,ID全局唯一
	ConnID uint32
	// 消息管理MsgId和对应处理方法的消息管理模块
	MsgHandler ziface.IMsgHandle

	sync.RWMutex
	// contains filtered or unexported fields
}

func NewConntion

func NewConntion(server ziface.IServer, conn *net.TCPConn, connID uint32, msgHandler ziface.IMsgHandle) *Connection

创建连接的方法

func (*Connection) GetConnID

func (c *Connection) GetConnID() uint32

GetConnID 获取当前连接ID

func (*Connection) GetProperty

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

GetProperty 获取连接属性

func (*Connection) GetTCPConnection

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

GetTCPConnection 从当前连接获取原始的socket TCPConn

func (*Connection) RemoteAddr

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

RemoteAddr 获取远程客户端地址信息

func (*Connection) RemoveProperty

func (c *Connection) RemoveProperty(key string)

RemoveProperty 移除连接属性

func (*Connection) SendBuffMsg

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

func (*Connection) SendMsg

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

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

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 读消息协程,用于从客户端中读取数据

func (*Connection) StartWriter

func (c *Connection) StartWriter()

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

func (*Connection) Stop

func (c *Connection) Stop()

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

type DataPack

type DataPack struct{}

DataPack 封包拆包类实例,暂时不需要成员

func NewDataPack

func NewDataPack() *DataPack

NewDataPack 封包拆包实例初始化方法

func (*DataPack) GetHeadLen

func (dp *DataPack) GetHeadLen() uint32

GetHeadLen 获取包头长度方法

func (*DataPack) Pack

func (dp *DataPack) Pack(msg ziface.IMessage) ([]byte, error)

Pack 封包方法(压缩数据)

func (*DataPack) Unpack

func (dp *DataPack) Unpack(binaryData []byte) (ziface.IMessage, error)

Unpack 拆包方法(解压数据)

type Message

type Message struct {
	DataLen uint32 // 消息的长度
	Id      uint32 // 消息的ID
	Data    []byte // 消息的内容
}

func NewMsgPackage

func NewMsgPackage(id uint32, data []byte) *Message

NewMsgPackage 创建一个Message消息包

func (*Message) GetData

func (msg *Message) GetData() []byte

GetData 获取消息内容

func (*Message) GetDataLen

func (msg *Message) GetDataLen() uint32

GetDataLen 获取消息数据段长度

func (*Message) GetMsgId

func (msg *Message) GetMsgId() uint32

GetMsgId 获取消息ID

func (*Message) SetData

func (msg *Message) SetData(data []byte)

SetData 设计消息内容

func (*Message) SetDataLen

func (msg *Message) SetDataLen(len uint32)

SetDataLen 设置消息数据段长度

func (*Message) SetMsgId

func (msg *Message) SetMsgId(msgId uint32)

SetMsgId 设计消息ID

type MsgHandle

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

func NewMsgHandle

func NewMsgHandle() *MsgHandle

func (*MsgHandle) AddRouter

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

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

func (*MsgHandle) DoMsgHandler

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

DoMsgHandler 马上以非阻塞方式处理消息

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 Request

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

func (*Request) GetConnection

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

获取请求连接信息

func (*Request) GetData

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

获取请求消息的数据

func (*Request) GetMsgID

func (r *Request) GetMsgID() uint32

获取请求的消息的ID

type Server

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

	// 当前Server的连接管理器
	ConnMgr ziface.IConnManager
	// 该Server的连接创建时Hook函数
	OnConnStart func(conn ziface.IConnection)
	// 该Server的连接断开时的Hook函数
	OnConnStop func(conn ziface.IConnection)
	// contains filtered or unexported fields
}

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

func (*Server) AddRouter

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

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

func (*Server) CallOnConnStart

func (s *Server) CallOnConnStart(conn ziface.IConnection)

CallOnConnStart 调用连接OnConnStart Hook函数

func (*Server) CallOnConnStop

func (s *Server) CallOnConnStop(conn ziface.IConnection)

CallOnConnStop 调用连接OnConnStop Hook函数

func (*Server) GetConnMgr

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

GetConnMgr 得到连接管理

func (*Server) Serve

func (s *Server) Serve()

Serve 运行服务

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) Start

func (s *Server) Start()

Start 开启网络服务

func (*Server) Stop

func (s *Server) Stop()

Stop 停止服务

Jump to

Keyboard shortcuts

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