Documentation ¶
Overview ¶
Package server 提供了包含多种网络类型的服务器实现
Index ¶
- Constants
- Variables
- type Conn
- func (slf *Conn) Close()
- func (slf *Conn) GetData(key any) any
- func (slf *Conn) GetID() string
- func (slf *Conn) GetIP() string
- func (slf *Conn) IsWebsocket() bool
- func (slf *Conn) ReleaseData() *Conn
- func (slf *Conn) RemoteAddr() net.Addr
- func (slf *Conn) SetData(key, value any) *Conn
- func (slf *Conn) Write(data []byte, messageType ...int)
- func (slf *Conn) WriteString(data string, messageType ...int)
- type ConnReadonly
- type ConnectionClosedEventHandle
- type ConnectionOpenedEventHandle
- type ConnectionReceivePacketEventHandle
- type ConnectionReceiveWebsocketPacketEventHandle
- type ConsoleCommandEventHandle
- type Cross
- type Message
- type MessageErrorAction
- type MessageErrorEventHandle
- type MessageLowExecEventHandle
- type MessageType
- type MultipleServer
- type Network
- type Option
- func WithCross(crossName string, serverId int64, cross Cross) Option
- func WithDiversion(diversion func(conn ConnReadonly, packet []byte) chan func()) Option
- func WithGRPCServerOptions(options ...grpc.ServerOption) Option
- func WithMessageBufferSize(size int) Option
- func WithMultiCore(count int) Option
- func WithProd() Option
- func WithTLS(certFile, keyFile string) Option
- func WithTicker(size int, autonomy bool) Option
- func WithWebsocketMessageType(messageTypes ...int) Option
- func WithWebsocketWriteMessageType(messageType int) Option
- type ReceiveCrossPacketEventHandle
- type Server
- func (slf *Server) GRPCServer() *grpc.Server
- func (slf *Server) GetID() int64
- func (slf *Server) HttpRouter() gin.IRouter
- func (slf *Server) IsDev() bool
- func (slf *Server) IsProd() bool
- func (slf Server) OnConnectionClosedEvent(conn *Conn, err any)
- func (slf Server) OnConnectionOpenedEvent(conn *Conn)
- func (slf Server) OnConnectionReceivePacketEvent(conn *Conn, packet []byte)
- func (slf Server) OnConnectionReceiveWebsocketPacketEvent(conn *Conn, packet []byte, messageType int)
- func (slf Server) OnConsoleCommandEvent(command string)
- func (slf Server) OnMessageErrorEvent(message *Message, err error)
- func (slf Server) OnMessageLowExecEvent(message *Message, cost time.Duration)
- func (slf Server) OnReceiveCrossPacketEvent(serverId int64, packet []byte)
- func (slf Server) OnStartBeforeEvent()
- func (slf Server) OnStartFinishEvent()
- func (slf *Server) PushCrossMessage(crossName string, serverId int64, packet []byte) error
- func (slf *Server) PushMessage(messageType MessageType, attrs ...any)
- func (slf Server) RegConnectionClosedEvent(handle ConnectionClosedEventHandle)
- func (slf Server) RegConnectionOpenedEvent(handle ConnectionOpenedEventHandle)
- func (slf Server) RegConnectionReceivePacketEvent(handle ConnectionReceivePacketEventHandle)
- func (slf Server) RegConnectionReceiveWebsocketPacketEvent(handle ConnectionReceiveWebsocketPacketEventHandle)
- func (slf Server) RegConsoleCommandEvent(command string, handle ConsoleCommandEventHandle)
- func (slf Server) RegMessageErrorEvent(handle MessageErrorEventHandle)
- func (slf Server) RegMessageLowExecEvent(handle MessageLowExecEventHandle)
- func (slf Server) RegReceiveCrossPacketEvent(handle ReceiveCrossPacketEventHandle)
- func (slf Server) RegStartBeforeEvent(handle StartBeforeEventHandle)
- func (slf Server) RegStartFinishEvent(handle StartFinishEventHandle)
- func (slf *Server) Run(addr string) error
- func (slf *Server) Shutdown(err error, stack ...string)
- func (slf *Server) Ticker() *timer.Ticker
- type StartBeforeEventHandle
- type StartFinishEventHandle
Constants ¶
const ( // WebsocketMessageTypeText 表示文本数据消息。文本消息负载被解释为 UTF-8 编码的文本数据 WebsocketMessageTypeText = 1 // WebsocketMessageTypeBinary 表示二进制数据消息 WebsocketMessageTypeBinary = 2 // WebsocketMessageTypeClose 表示关闭控制消息。可选消息负载包含数字代码和文本。使用 FormatCloseMessage 函数来格式化关闭消息负载 WebsocketMessageTypeClose = 8 // WebsocketMessageTypePing 表示 ping 控制消息。可选的消息负载是 UTF-8 编码的文本 WebsocketMessageTypePing = 9 // WebsocketMessageTypePong 表示一个 pong 控制消息。可选的消息负载是 UTF-8 编码的文本 WebsocketMessageTypePong = 10 )
Variables ¶
var ( ErrConstructed = errors.New("the Server must be constructed using the server.New function") ErrCanNotSupportNetwork = errors.New("can not support network") ErrMessageTypePacketAttrs = errors.New("MessageTypePacket must contain *Conn and []byte") ErrWebsocketMessageTypePacketAttrs = errors.New("MessageTypePacket must contain *Conn and []byte and int(MessageType)") ErrMessageTypeErrorAttrs = errors.New("MessageTypePacket must contain error and MessageErrorAction") ErrMessageTypeCrossErrorAttrs = errors.New("MessageTypeCross must contain int64(server id) and []byte") ErrMessageTypeTickerErrorAttrs = errors.New("MessageTypeTicker must contain func()") ErrNetworkOnlySupportHttp = errors.New("the current network mode is not compatible with HttpRouter, only NetworkHttp is supported") ErrNetworkOnlySupportGRPC = errors.New("the current network mode is not compatible with RegGrpcServer, only NetworkGRPC is supported") ErrNetworkIncompatibleHttp = errors.New("the current network mode is not compatible with NetworkHttp") ErrWebsocketMessageTypeException = errors.New("unknown message type, will not work") ErrNotWebsocketUseMessageType = errors.New("message type filtering only supports websocket and does not take effect") ErrWebsocketIllegalMessageType = errors.New("illegal message type") ErrPleaseUseWebsocketHandle = errors.New("in Websocket mode, please use the RegConnectionReceiveWebsocketPacketEvent function to register") ErrPleaseUseOrdinaryPacketHandle = errors.New("non Websocket mode, please use the RegConnectionReceivePacketEvent function to register") ErrNoSupportCross = errors.New("the server does not support GetID or PushCrossMessage, please use the WithCross option to create the server") ErrNoSupportTicker = errors.New("the server does not support Ticker, please use the WithTicker option to create the server") ErrUnregisteredCrossName = errors.New("unregistered cross name, please use the WithCross option to create the server") )
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn 服务器连接
func (*Conn) RemoteAddr ¶
type ConnReadonly ¶ added in v0.0.4
type ConsoleCommandEventHandle ¶
type ConsoleCommandEventHandle func(srv *Server)
type Cross ¶
type Cross interface { // Init 初始化跨服 // - serverId: 本服id // - packetHandle.serverId: 发送跨服消息的服务器id // - packetHandle.packet: 数据包 Init(server *Server, packetHandle func(serverId int64, packet []byte)) error // PushMessage 推送跨服消息 // - serverId: 目标服务器id PushMessage(serverId int64, packet []byte) error // Release 释放资源 Release() }
type MessageErrorAction ¶
type MessageErrorAction byte
MessageErrorAction 错误消息类型操作
const ( MessageErrorActionNone MessageErrorAction = iota // 错误消息类型操作:将不会被进行任何特殊处理,仅进行日志输出 MessageErrorActionShutdown // 错误消息类型操作:当接收到该类型的操作时,服务器将执行 Server.Shutdown 函数 )
func (MessageErrorAction) String ¶
func (slf MessageErrorAction) String() string
type MessageErrorEventHandle ¶
type MessageType ¶
type MessageType byte
MessageType 消息类型
const ( // MessageTypePacket 数据包消息类型:该类型的数据将被发送到 ConnectionReceivePacketEvent 进行处理 // - *server.Conn // - []byte MessageTypePacket MessageType = iota // MessageTypeError 错误消息类型:根据不同的错误状态,将交由 Server 进行统一处理 // - error // - server.MessageErrorAction MessageTypeError // MessageTypeCross 跨服消息类型:将被推送到跨服的 Cross 实现中进行处理 // - int64(serverId) // - []byte MessageTypeCross // MessageTypeTicker 定时器消息类型 // - func() MessageTypeTicker )
func (MessageType) String ¶
func (slf MessageType) String() string
type MultipleServer ¶
type MultipleServer struct {
// contains filtered or unexported fields
}
func NewMultipleServer ¶
func NewMultipleServer(serverHandle ...func() (addr string, srv *Server)) *MultipleServer
func (*MultipleServer) OnExitEvent ¶ added in v0.0.4
func (slf *MultipleServer) OnExitEvent()
func (*MultipleServer) RegExitEvent ¶ added in v0.0.4
func (slf *MultipleServer) RegExitEvent(handle func())
RegExitEvent 注册退出事件
func (*MultipleServer) Run ¶
func (slf *MultipleServer) Run()
type Network ¶
type Network string
const ( NetworkTcp Network = "tcp" NetworkTcp4 Network = "tcp4" NetworkTcp6 Network = "tcp6" NetworkUdp Network = "udp" NetworkUdp4 Network = "udp4" NetworkUdp6 Network = "udp6" NetworkUnix Network = "unix" NetworkHttp Network = "http" // NetworkWebsocket 该模式下需要获取url参数值时,可以通过连接的GetData函数获取 // - 当有多个同名参数时,获取到的值为切片类型 NetworkWebsocket Network = "websocket" NetworkKcp Network = "kcp" NetworkGRPC Network = "grpc" )
type Option ¶
type Option func(srv *Server)
func WithCross ¶
WithCross 通过跨服的方式创建服务器
- 推送跨服消息时,将推送到对应crossName的跨服中间件中,crossName可以满足不同功能采用不同的跨服/消息中间件
- 通常情况下crossName仅需一个即可
func WithDiversion ¶ added in v0.0.4
func WithDiversion(diversion func(conn ConnReadonly, packet []byte) chan func()) Option
WithDiversion 通过分流的方式创建服务器
- diversion:分流函数,返回一个函数通道,用于接收分流的消息
- 需要确保能够通过 conn 和 packet 确定分流通道
- 多核模式下将导致消息顺序不一致,通过结果依然是单核处理的,因为分流通道仅有一个
func WithGRPCServerOptions ¶
func WithGRPCServerOptions(options ...grpc.ServerOption) Option
WithGRPCServerOptions 通过GRPC的可选项创建GRPC服务器
func WithMessageBufferSize ¶
WithMessageBufferSize 通过特定的消息缓冲池大小运行服务器
- 默认大小为 4096 * 1024
- 消息数量超出这个值的时候,消息处理将会造成更大的开销(频繁创建新的结构体),同时服务器将输出警告内容
func WithMultiCore ¶
WithMultiCore 通过特定核心数量运行服务器,默认为单核
- count > 1 的情况下,将会有对应数量的 goroutine 来处理消息
- 注意:HTTP和GRPC网络模式下不会生效
- 在需要分流的场景推荐采用多核模式,如游戏以房间的形式进行,每个房间互不干扰,这种情况下便可以每个房间单独维护数据包消息进行处理
func WithTicker ¶
WithTicker 通过定时器创建服务器,为服务器添加定时器功能
- autonomy:定时器是否独立运行(独立运行的情况下不会作为服务器消息运行,会导致并发问题)
- 多核与分流情况下需要考虑是否有必要 autonomy
func WithWebsocketMessageType ¶
WithWebsocketMessageType 设置仅支持特定类型的Websocket消息
func WithWebsocketWriteMessageType ¶
WithWebsocketWriteMessageType 设置客户端写入的Websocket消息类型
- 默认: WebsocketMessageTypeBinary
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server 网络服务器
func (*Server) GRPCServer ¶
func (*Server) HttpRouter ¶
HttpRouter 当网络类型为 NetworkHttp 时将被允许获取路由器进行路由注册,否则将会发生 panic
func (Server) OnConnectionClosedEvent ¶
func (Server) OnConnectionOpenedEvent ¶
func (slf Server) OnConnectionOpenedEvent(conn *Conn)
func (Server) OnConnectionReceivePacketEvent ¶
func (Server) OnConnectionReceiveWebsocketPacketEvent ¶
func (Server) OnConsoleCommandEvent ¶
func (slf Server) OnConsoleCommandEvent(command string)
func (Server) OnMessageErrorEvent ¶
func (Server) OnMessageLowExecEvent ¶
func (Server) OnReceiveCrossPacketEvent ¶
func (Server) OnStartBeforeEvent ¶
func (slf Server) OnStartBeforeEvent()
func (Server) OnStartFinishEvent ¶
func (slf Server) OnStartFinishEvent()
func (*Server) PushCrossMessage ¶
PushCrossMessage 推送跨服消息到特定跨服的服务器中
func (*Server) PushMessage ¶
func (slf *Server) PushMessage(messageType MessageType, attrs ...any)
PushMessage 向服务器中写入特定类型的消息,需严格遵守消息属性要求
func (Server) RegConnectionClosedEvent ¶
func (slf Server) RegConnectionClosedEvent(handle ConnectionClosedEventHandle)
RegConnectionClosedEvent 在连接关闭后将立刻执行被注册的事件处理函数
func (Server) RegConnectionOpenedEvent ¶
func (slf Server) RegConnectionOpenedEvent(handle ConnectionOpenedEventHandle)
RegConnectionOpenedEvent 在连接打开后将立刻执行被注册的事件处理函数
func (Server) RegConnectionReceivePacketEvent ¶
func (slf Server) RegConnectionReceivePacketEvent(handle ConnectionReceivePacketEventHandle)
RegConnectionReceivePacketEvent 在接收到数据包时将立刻执行被注册的事件处理函数
func (Server) RegConnectionReceiveWebsocketPacketEvent ¶
func (slf Server) RegConnectionReceiveWebsocketPacketEvent(handle ConnectionReceiveWebsocketPacketEventHandle)
RegConnectionReceiveWebsocketPacketEvent 在接收到Websocket数据包时将立刻执行被注册的事件处理函数
func (Server) RegConsoleCommandEvent ¶
func (slf Server) RegConsoleCommandEvent(command string, handle ConsoleCommandEventHandle)
RegConsoleCommandEvent 控制台收到指令时将立即执行被注册的事件处理函数
- 默认将注册 "exit", "quit", "close", "shutdown", "EXIT", "QUIT", "CLOSE", "SHUTDOWN" 指令作为关闭服务器的指令
- 可通过注册默认指令进行默认行为的覆盖
func (Server) RegMessageErrorEvent ¶
func (slf Server) RegMessageErrorEvent(handle MessageErrorEventHandle)
RegMessageErrorEvent 在处理消息发生错误时将立即执行被注册的事件处理函数
func (Server) RegMessageLowExecEvent ¶
func (slf Server) RegMessageLowExecEvent(handle MessageLowExecEventHandle)
RegMessageLowExecEvent 在处理消息缓慢时将立即执行被注册的事件处理函数
func (Server) RegReceiveCrossPacketEvent ¶
func (slf Server) RegReceiveCrossPacketEvent(handle ReceiveCrossPacketEventHandle)
RegReceiveCrossPacketEvent 在接收到跨服数据包时将立即执行被注册的事件处理函数
func (Server) RegStartBeforeEvent ¶
func (slf Server) RegStartBeforeEvent(handle StartBeforeEventHandle)
RegStartBeforeEvent 在服务器初始化完成启动前立刻执行被注册的事件处理函数
func (Server) RegStartFinishEvent ¶
func (slf Server) RegStartFinishEvent(handle StartFinishEventHandle)
RegStartFinishEvent 在服务器启动完成时将立刻执行被注册的事件处理函数
func (*Server) Run ¶
Run 使用特定地址运行服务器
server.NetworkTcp (addr:":8888") server.NetworkTcp4 (addr:":8888") server.NetworkTcp6 (addr:":8888") server.NetworkUdp (addr:":8888") server.NetworkUdp4 (addr:":8888") server.NetworkUdp6 (addr:":8888") server.NetworkUnix (addr:"socketPath") server.NetworkHttp (addr:":8888") server.NetworkWebsocket (addr:":8888/ws") server.NetworkKcp (addr:":8888")
type StartBeforeEventHandle ¶
type StartBeforeEventHandle func(srv *Server)
type StartFinishEventHandle ¶
type StartFinishEventHandle func(srv *Server)