impl

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2021 License: MIT Imports: 19 Imported by: 2

Documentation

Overview

@Title @Description @Author Wangwengang 2021/8/17 下午5:21 @Update Wangwengang 2021/8/17 下午5:21

@Title @Description @Author Wangwengang 2021/8/17 下午5:25 @Update Wangwengang 2021/8/17 下午5:25

@Title @Description @Author Wangwengang 2021/8/17 下午5:26 @Update Wangwengang 2021/8/17 下午5:26

@Title @Description @Author Wangwengang 2021/8/17 下午5:18 @Update Wangwengang 2021/8/17 下午5:18

@Title @Description @Author Wangwengang 2021/8/17 下午5:11 @Update Wangwengang 2021/8/17 下午5:11

@Title @Description @Author Wangwengang 2021/8/17 下午9:54 @Update Wangwengang 2021/8/17 下午9:54

@Title @Description @Author Wangwengang 2021/8/17 下午9:55 @Update Wangwengang 2021/8/17 下午9:55

@Title @Description @Author Wangwengang 2021/8/17 下午5:08 @Update Wangwengang 2021/8/17 下午5:08

@Title @Description @Author Wangwengang 2021/8/17 下午4:45 @Update Wangwengang 2021/8/17 下午4:45

@Title @Description @Author Wangwengang 2021/8/19 下午11:27 @Update Wangwengang 2021/8/19 下午11:27

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConnection

func NewConnection(server anet.Server, conn *net.TCPConn, connID uint64, msgHandler anet.MsgHandle) anet.Connection

NewConnection 创建连接的方法

func NewDataPack

func NewDataPack() anet.Packet

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

func NewServer

func NewServer(opts ...Option) anet.Server

NewServer 创建一个服务器句柄

func NewWsConnection

func NewWsConnection(server anet.Server, conn *websocket.Conn, connID uint64, msgHandler anet.MsgHandle) anet.Connection

NewConnection 创建连接的方法

Types

type BaseRouter

type BaseRouter struct{}

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

func (*BaseRouter) Handle

func (br *BaseRouter) Handle(req anet.Request)

Handle -

func (*BaseRouter) PostHandle

func (br *BaseRouter) PostHandle(req anet.Request)

PostHandle -

func (*BaseRouter) PreHandle

func (br *BaseRouter) PreHandle(req anet.Request)

PreHandle -

type ConnManager

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

func NewConnManager

func NewConnManager() *ConnManager

NewConnManager 创建一个链接管理

func (*ConnManager) Add

func (connMgr *ConnManager) Add(conn anet.Connection)

Add 添加链接

func (*ConnManager) ClearConn

func (connMgr *ConnManager) ClearConn()

ClearConn 清除并停止所有连接

func (*ConnManager) ClearOneConn

func (connMgr *ConnManager) ClearOneConn(connID uint64)

ClearOneConn 利用ConnID获取一个链接 并且删除

func (*ConnManager) Get

func (connMgr *ConnManager) Get(connID uint64) (anet.Connection, error)

Get 利用ConnID获取链接

func (*ConnManager) Len

func (connMgr *ConnManager) Len() int

Len 获取当前连接

func (*ConnManager) Remove

func (connMgr *ConnManager) Remove(conn anet.Connection)

Remove 删除连接

type DataPack

type DataPack struct{}

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

func (*DataPack) GetHeadLen

func (dp *DataPack) GetHeadLen() uint32

GetHeadLen 获取包头长度方法

func (*DataPack) Pack

func (dp *DataPack) Pack(msg anet.Message) ([]byte, error)

Pack 封包方法(压缩数据)

func (*DataPack) Unpack

func (dp *DataPack) Unpack(binaryData []byte) (anet.Message, error)

Unpack 拆包方法(解压数据)

type Message

type Message struct {
	pigeon.Proto
}

Message 消息

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

func (msg *Message) GetSeq() uint32

GetSeq 获取版本

func (*Message) GetVer

func (msg *Message) GetVer() uint32

GetVer 获取版本

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]anet.Router //存放每个MsgID 所对应的处理方法的map属性
	Rpcx           anet.RpcxRouter
	WorkerPoolSize uint32              //业务工作Worker池的数量
	TaskQueue      []chan anet.Request //Worker负责取任务的消息队列
}

func NewMsgHandle

func NewMsgHandle() *MsgHandle

NewMsgHandle 创建MsgHandle

func (*MsgHandle) AddRouter

func (mh *MsgHandle) AddRouter(msgID uint32, router anet.Router)

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

func (*MsgHandle) DoMsgHandler

func (mh *MsgHandle) DoMsgHandler(request anet.Request)

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

func (*MsgHandle) SendMsgToTaskQueue

func (mh *MsgHandle) SendMsgToTaskQueue(request anet.Request)

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

func (*MsgHandle) SetRpcxRouter

func (mh *MsgHandle) SetRpcxRouter(router anet.RpcxRouter)

SetRpcxRouter 为消息添加Rpcx处理逻辑

func (*MsgHandle) StartOneWorker

func (mh *MsgHandle) StartOneWorker(workerID int, taskQueue chan anet.Request)

StartOneWorker 启动一个Worker工作流程

func (*MsgHandle) StartWorkerPool

func (mh *MsgHandle) StartWorkerPool()

StartWorkerPool 启动worker工作池

type Option

type Option func(s *Server)

func WithPacket

func WithPacket(pack anet.Packet) Option

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

type Request

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

Request 请求

func (*Request) GetConnection

func (r *Request) GetConnection() anet.Connection

GetConnection 获取请求连接信息

func (*Request) GetData

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

GetData 获取请求消息的数据

func (*Request) GetMsgID

func (r *Request) GetMsgID() uint32

GetMsgID 获取请求的消息的ID

type RpcxRouter

type RpcxRouter struct {
}

RpcxRouter

func (*RpcxRouter) Handle

func (br *RpcxRouter) Handle(req anet.Request)

Handle -

type Server

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

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

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

func (*Server) AddRouter

func (s *Server) AddRouter(msgID uint32, router anet.Router)

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

func (*Server) CallOnConnStart

func (s *Server) CallOnConnStart(conn anet.Connection)

CallOnConnStart 调用连接OnConnStart Hook函数

func (*Server) CallOnConnStop

func (s *Server) CallOnConnStop(conn anet.Connection)

CallOnConnStop 调用连接OnConnStop Hook函数

func (*Server) GenID

func (s *Server) GenID() uint64

func (*Server) GetConnMgr

func (s *Server) GetConnMgr() anet.ConnManager

GetConnMgr 得到链接管理

func (*Server) Packet

func (s *Server) Packet() anet.Packet

func (*Server) Serve

func (s *Server) Serve()

Serve 运行服务

func (*Server) SetOnConnStart

func (s *Server) SetOnConnStart(hookFunc func(anet.Connection))

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

func (*Server) SetOnConnStop

func (s *Server) SetOnConnStop(hookFunc func(anet.Connection))

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

func (*Server) SetRpcxRouter

func (s *Server) SetRpcxRouter(router anet.RpcxRouter)

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

func (*Server) StartTcp

func (s *Server) StartTcp()

Start 开启Tcp网络服务

func (*Server) StartWebsocket

func (s *Server) StartWebsocket()

Start Websocket网络服务

func (*Server) Stop

func (s *Server) Stop()

Stop 停止服务

type TcpConnection

type TcpConnection struct {
	//当前Conn属于哪个Server
	TCPServer anet.Server
	//当前连接的socket TCP套接字
	Conn *net.TCPConn
	//当前连接的ID 也可以称作为SessionID,ID全局唯一
	ConnID uint64
	//消息管理MsgID和对应处理方法的消息管理模块
	MsgHandler anet.MsgHandle

	sync.RWMutex
	// contains filtered or unexported fields
}

func (*TcpConnection) Context

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

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

func (*TcpConnection) GetConnID

func (c *TcpConnection) GetConnID() uint64

GetConnID 获取当前连接ID

func (*TcpConnection) GetProperty

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

GetProperty 获取链接属性

func (*TcpConnection) GetTcpConnection

func (c *TcpConnection) GetTcpConnection() *net.TCPConn

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

func (*TcpConnection) GetWsConnection

func (c *TcpConnection) GetWsConnection() *websocket.Conn

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

func (*TcpConnection) RemoteAddr

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

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

func (*TcpConnection) RemoveProperty

func (c *TcpConnection) RemoveProperty(key string)

RemoveProperty 移除链接属性

func (*TcpConnection) SendBuffMsg

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

SendBuffMsg 发生BuffMsg

func (*TcpConnection) SendMsg

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

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

func (*TcpConnection) SetProperty

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

SetProperty 设置链接属性

func (*TcpConnection) Start

func (c *TcpConnection) Start()

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

func (*TcpConnection) StartReader

func (c *TcpConnection) StartReader()

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

func (*TcpConnection) StartWriter

func (c *TcpConnection) StartWriter()

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

func (*TcpConnection) Stop

func (c *TcpConnection) Stop()

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

type WsConnection

type WsConnection struct {
	//当前Conn属于哪个Server
	TCPServer anet.Server
	//当前连接的socket TCP套接字
	Conn *websocket.Conn
	//当前连接的ID 也可以称作为SessionID,ID全局唯一
	ConnID uint64
	//消息管理MsgID和对应处理方法的消息管理模块
	MsgHandler anet.MsgHandle

	sync.RWMutex
	// contains filtered or unexported fields
}

func (*WsConnection) Context

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

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

func (*WsConnection) GetConnID

func (c *WsConnection) GetConnID() uint64

GetConnID 获取当前连接ID

func (*WsConnection) GetProperty

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

GetProperty 获取链接属性

func (*WsConnection) GetTcpConnection

func (c *WsConnection) GetTcpConnection() *net.TCPConn

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

func (*WsConnection) GetWsConnection

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

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

func (*WsConnection) RemoteAddr

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

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

func (*WsConnection) RemoveProperty

func (c *WsConnection) RemoveProperty(key string)

RemoveProperty 移除链接属性

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

type WsHandler

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

func (*WsHandler) ServeHTTP

func (h *WsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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