Documentation
¶
Index ¶
- Variables
- func GenerateConnID() string
- func PutMessage(m IMessage)
- func SetCustomServer(custom *CustomServer)
- type AppConf
- type BaseRouter
- type ConnectionBase
- func (c *ConnectionBase) ByteToProtocol(byte []byte, target proto.Message) error
- func (c *ConnectionBase) Close()
- func (c *ConnectionBase) ConnCtxDone() <-chan struct{}
- func (c *ConnectionBase) DoTask(task func())
- func (c *ConnectionBase) FlowControl() (b bool)
- func (c *ConnectionBase) GetConnId() string
- func (c *ConnectionBase) GetProperty(key string) any
- func (c *ConnectionBase) IsClose() bool
- func (c *ConnectionBase) Open()
- func (c *ConnectionBase) ProtocolToByte(str proto.Message) []byte
- func (c *ConnectionBase) RemoteAddrStr() string
- func (c *ConnectionBase) RemoveProperty(key string)
- func (c *ConnectionBase) SendMsg(msgId int32, msgData proto.Message)
- func (c *ConnectionBase) SetProperty(key string, value any)
- type ConnectionManager
- func (c *ConnectionManager) Add(conn IConnection)
- func (c *ConnectionManager) ClearConn()
- func (c *ConnectionManager) ConnRateLimiting(conn IConnection)
- func (c *ConnectionManager) Get(connId string) (IConnection, bool)
- func (c *ConnectionManager) GetConnClosed(conn IConnection)
- func (c *ConnectionManager) GetConnOpened(conn IConnection)
- func (c *ConnectionManager) Len() int
- func (c *ConnectionManager) RangeConnections(handler func(conn IConnection))
- func (c *ConnectionManager) Remove(conn IConnection)
- func (c *ConnectionManager) SetConnClosed(connCloseCallBack func(conn IConnection))
- func (c *ConnectionManager) SetConnOnRateLimiting(limitCallBack func(conn IConnection))
- func (c *ConnectionManager) SetConnOpened(connOpenCallBack func(conn IConnection))
- type CustomServer
- type IConnection
- func NewConnectionHTTP(server IServer, writer http.ResponseWriter, reader *http.Request) IConnection
- func NewConnectionKCP(server *serverKCP, conn net.Conn) IConnection
- func NewConnectionTCP(server IServer, conn *net.TCPConn) IConnection
- func NewConnectionWS(server IServer, conn *websocket.Conn) IConnection
- type IDataPack
- type IErrCapture
- type IFilter
- type IMessage
- type INewMsgStructTemplate
- type IReceiveMsgHandler
- type IServer
- type Message
- type MsgHandler
- func (m *MsgHandler) AddRouter(msgId int32, msgTemplate INewMsgStructTemplate, msgHandler IReceiveMsgHandler)
- func (m *MsgHandler) GetApis() map[int32]*BaseRouter
- func (m *MsgHandler) GetErrCapture(conn IConnection)
- func (m *MsgHandler) GetFilter() IFilter
- func (m *MsgHandler) SetErrCapture(fun IErrCapture)
- func (m *MsgHandler) SetFilter(fun IFilter)
- type ServerConf
- type ServerManager
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ConnPropertyHttpAuthorization = "HttpAuthorization" ConnPropertyHttpReader = "HttpReader" ConnPropertyHttpWriter = "HttpWriter" )
Functions ¶
func GenerateConnID ¶ added in v1.2.2
func GenerateConnID() string
func PutMessage ¶ added in v1.2.2
func PutMessage(m IMessage)
Types ¶
type AppConf ¶
type AppConf struct {
AppName string // 服务名称
MaxPackSize int // 数据包最大长度
MaxConn int // 最大允许连接数
WorkerTaskMaxLen int // 每个工作队列可执行最大任务数量
MaxMsgChanLen int // 读写通道最大限度
MaxFlowSecond int // 每秒允许的最大请求数量
ProtocolIsJson bool // 是否使用json协议
ConnRWTimeOut int // 连接读写超时时间(秒)
ServerTCP ServerConf // tcp服务
ServerWS ServerConf // websocket服务
ServerHTTP ServerConf // http服务
ServerKCP ServerConf // http服务
}
type BaseRouter ¶
type BaseRouter struct {
// contains filtered or unexported fields
}
func (*BaseRouter) GetNewMsg ¶
func (b *BaseRouter) GetNewMsg() proto.Message
func (*BaseRouter) RunHandler ¶
func (b *BaseRouter) RunHandler(conn IConnection, message proto.Message)
func (*BaseRouter) SetHandler ¶
func (b *BaseRouter) SetHandler(msgHandler IReceiveMsgHandler)
func (*BaseRouter) SetMsg ¶
func (b *BaseRouter) SetMsg(msgTemplate INewMsgStructTemplate)
type ConnectionBase ¶
type ConnectionBase struct {
// contains filtered or unexported fields
}
func (*ConnectionBase) ByteToProtocol ¶
func (c *ConnectionBase) ByteToProtocol(byte []byte, target proto.Message) error
func (*ConnectionBase) Close ¶ added in v1.2.2
func (c *ConnectionBase) Close()
func (*ConnectionBase) ConnCtxDone ¶ added in v1.2.2
func (c *ConnectionBase) ConnCtxDone() <-chan struct{}
func (*ConnectionBase) DoTask ¶
func (c *ConnectionBase) DoTask(task func())
func (*ConnectionBase) FlowControl ¶
func (c *ConnectionBase) FlowControl() (b bool)
func (*ConnectionBase) GetConnId ¶
func (c *ConnectionBase) GetConnId() string
func (*ConnectionBase) GetProperty ¶
func (c *ConnectionBase) GetProperty(key string) any
func (*ConnectionBase) IsClose ¶
func (c *ConnectionBase) IsClose() bool
func (*ConnectionBase) Open ¶ added in v1.2.2
func (c *ConnectionBase) Open()
func (*ConnectionBase) ProtocolToByte ¶
func (c *ConnectionBase) ProtocolToByte(str proto.Message) []byte
func (*ConnectionBase) RemoteAddrStr ¶ added in v1.2.2
func (c *ConnectionBase) RemoteAddrStr() string
func (*ConnectionBase) RemoveProperty ¶ added in v1.2.2
func (c *ConnectionBase) RemoveProperty(key string)
func (*ConnectionBase) SendMsg ¶
func (c *ConnectionBase) SendMsg(msgId int32, msgData proto.Message)
func (*ConnectionBase) SetProperty ¶ added in v1.2.2
func (c *ConnectionBase) SetProperty(key string, value any)
type ConnectionManager ¶
type ConnectionManager struct {
// contains filtered or unexported fields
}
func (*ConnectionManager) Add ¶
func (c *ConnectionManager) Add(conn IConnection)
func (*ConnectionManager) ClearConn ¶
func (c *ConnectionManager) ClearConn()
func (*ConnectionManager) ConnRateLimiting ¶
func (c *ConnectionManager) ConnRateLimiting(conn IConnection)
func (*ConnectionManager) Get ¶
func (c *ConnectionManager) Get(connId string) (IConnection, bool)
func (*ConnectionManager) GetConnClosed ¶ added in v1.2.2
func (c *ConnectionManager) GetConnClosed(conn IConnection)
func (*ConnectionManager) GetConnOpened ¶ added in v1.2.2
func (c *ConnectionManager) GetConnOpened(conn IConnection)
func (*ConnectionManager) Len ¶
func (c *ConnectionManager) Len() int
func (*ConnectionManager) RangeConnections ¶
func (c *ConnectionManager) RangeConnections(handler func(conn IConnection))
func (*ConnectionManager) Remove ¶
func (c *ConnectionManager) Remove(conn IConnection)
func (*ConnectionManager) SetConnClosed ¶ added in v1.2.2
func (c *ConnectionManager) SetConnClosed(connCloseCallBack func(conn IConnection))
func (*ConnectionManager) SetConnOnRateLimiting ¶
func (c *ConnectionManager) SetConnOnRateLimiting(limitCallBack func(conn IConnection))
func (*ConnectionManager) SetConnOpened ¶ added in v1.2.2
func (c *ConnectionManager) SetConnOpened(connOpenCallBack func(conn IConnection))
type CustomServer ¶
自定义服务器
type IConnection ¶
type IConnection interface {
// 启动连接(通过connmanager调用)
Open()
// 停止连接(通过connmanager调用)
Close()
// 获取真实连接
GetNetConn() net.Conn
// 连接上下文关闭信号
ConnCtxDone() <-chan struct{}
// 启动接收消息协程
StartReader() bool
// 启动发送消息协程
StartWriter(data []byte) bool
// 执行任务
DoTask(task func())
// 获取当前连接Id
GetConnId() string
// 获取客户端地址信息
RemoteAddrStr() string
// 获取连接是否已关闭
IsClose() bool
// 获取连接绑定的属性
GetProperty(key string) any
// 设置连接绑定的属性
SetProperty(key string, value any)
// 移除连接绑定的属性
RemoveProperty(key string)
// 发送消息给客户端
SendMsg(msgId int32, msgData proto.Message)
// 限流控制
FlowControl() bool
// 序列化
ProtocolToByte(str proto.Message) []byte
// 反序列化
ByteToProtocol(byte []byte, target proto.Message) error
}
func NewConnectionHTTP ¶
func NewConnectionHTTP(server IServer, writer http.ResponseWriter, reader *http.Request) IConnection
func NewConnectionKCP ¶
func NewConnectionKCP(server *serverKCP, conn net.Conn) IConnection
func NewConnectionTCP ¶
func NewConnectionTCP(server IServer, conn *net.TCPConn) IConnection
func NewConnectionWS ¶
func NewConnectionWS(server IServer, conn *websocket.Conn) IConnection
type IDataPack ¶
type IDataPack interface {
// 获取消息头长度
GetHeadLen() int
// 消息封包
Pack(msg IMessage) []byte
// 消息拆包
UnPack([]byte) IMessage
}
封包拆包,通过固定的包头获取消息数据,解决TCP粘包问题
func NewDataPack ¶
func NewDataPack() IDataPack
type IErrCapture ¶
type IErrCapture func(conn IConnection, panicInfo string)
type IFilter ¶
type IFilter func(conn IConnection, msg IMessage) bool
type IMessage ¶
type IMessage interface {
// 获取消息Id
GetMsgId() uint16
// 获取消息长度
GetDataLen() uint16
// 获取消息内容
GetData() []byte
// 设置消息内容
SetData([]byte)
}
定义消息模板
type INewMsgStructTemplate ¶
type IReceiveMsgHandler ¶
type IReceiveMsgHandler func(conn IConnection, message proto.Message)
type IServer ¶
type IServer interface {
// 获取服务器名称
GetServerName() string
// 启动服务器
Start()
}
定义服务器接口
func GetServerHTTP ¶
func GetServerHTTP() IServer
func GetServerKCP ¶
func GetServerKCP() IServer
func GetServerTCP ¶
func GetServerTCP() IServer
func GetServerWS ¶
func GetServerWS() IServer
type Message ¶
type Message struct {
proto.Message `json:"-"`
Id uint16 `protobuf:"bytes,1,opt,name=msg_id,proto3" json:"msg_id"` // 消息Id
Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data"` // 消息内容
DataLen uint16 `json:"-"` // 消息长度
}
func GetMessage ¶ added in v1.2.2
func GetMessage() *Message
func (*Message) GetDataLen ¶
type MsgHandler ¶
type MsgHandler struct {
// contains filtered or unexported fields
}
func (*MsgHandler) AddRouter ¶
func (m *MsgHandler) AddRouter(msgId int32, msgTemplate INewMsgStructTemplate, msgHandler IReceiveMsgHandler)
func (*MsgHandler) GetApis ¶
func (m *MsgHandler) GetApis() map[int32]*BaseRouter
func (*MsgHandler) GetErrCapture ¶
func (m *MsgHandler) GetErrCapture(conn IConnection)
func (*MsgHandler) GetFilter ¶
func (m *MsgHandler) GetFilter() IFilter
func (*MsgHandler) SetErrCapture ¶
func (m *MsgHandler) SetErrCapture(fun IErrCapture)
func (*MsgHandler) SetFilter ¶
func (m *MsgHandler) SetFilter(fun IFilter)
type ServerConf ¶
type ServerManager ¶
type ServerManager struct {
// contains filtered or unexported fields
}
func (*ServerManager) IsClose ¶
func (c *ServerManager) IsClose() bool
func (*ServerManager) RegisterServer ¶
func (c *ServerManager) RegisterServer(server ...IServer)
func (*ServerManager) StopAll ¶
func (c *ServerManager) StopAll()
func (*ServerManager) WaitGroupAdd ¶
func (c *ServerManager) WaitGroupAdd(delta int)
func (*ServerManager) WaitGroupDone ¶
func (c *ServerManager) WaitGroupDone()
Source Files
¶
Click to show internal directories.
Click to hide internal directories.