Documentation
¶
Index ¶
- Constants
- Variables
- func GetRequestID(ctx Context) string
- type App
- func (a *App) AddAfterMiddleware(m Middleware)
- func (a *App) AddBeforeMiddleware(m Middleware)
- func (a *App) AddHandler(opcode interface{}, h Handler)
- func (a *App) AddModule(module Moddule)
- func (a *App) AddSession(userID int64, session *Session)
- func (a *App) DelSession(userID int64, sessionid int64)
- func (a *App) DisableFuse() error
- func (a *App) EnableFuse() error
- func (a *App) GetGd() GameMetaData
- func (a *App) GetTotalConn() int64
- func (a *App) Init() (err error)
- func (a *App) OnConnect(session *Session) bool
- func (a *App) OnDisConnect(session *Session)
- func (a *App) OnHeartbeat(session *Session)
- func (a *App) OnMessage(session *Session, p Packet) bool
- func (a *App) SendActor(userID int64, opcode uint16, msg proto.Message) error
- func (a *App) SendAllActor(opcode uint16, msg proto.Message) error
- func (a *App) SetConnectListener(l Listener)
- func (a *App) SetDisconnectListener(l Listener)
- func (a *App) SetHeartbeatListener(l Listener)
- func (a *App) Start() error
- func (a *App) Stop()
- func (a *App) UpdateGdExtra(extra interface{}) error
- func (a *App) UpdateGdState(state int) error
- type Client
- type Context
- type DefaultContext
- func (c *DefaultContext) Context() context.Context
- func (c *DefaultContext) GetOpCode() uint16
- func (c *DefaultContext) GetRequestID() string
- func (c *DefaultContext) Params(m protoreflect.ProtoMessage) error
- func (c *DefaultContext) Send(opcode interface{}, msg proto.Message) error
- func (c *DefaultContext) SendActor(userID int64, opcode interface{}, msg proto.Message) error
- func (c *DefaultContext) SendActorLocation(userID int64, opcode interface{}, msg proto.Message) error
- func (c *DefaultContext) SendActorPush(userID int64, opcode interface{}, msg proto.Message) error
- func (c *DefaultContext) SendAllActor(opcode interface{}, msg proto.Message) error
- func (c *DefaultContext) Session() *Session
- func (c *DefaultContext) Valid(userID int64)
- type DefaultPacket
- type GameMetaData
- type Handler
- type KcpNetConn
- type KcpNetListener
- type Listener
- type Middleware
- type Moddule
- type NetConfig
- type NetConn
- type NetListener
- type Packet
- type PushActorMessage
- type Session
- func (s *Session) App() *App
- func (s *Session) Close()
- func (s *Session) IsClosed() bool
- func (s *Session) IsExipred() bool
- func (s *Session) IsValid() bool
- func (s *Session) Run()
- func (s *Session) SetExpired(expired int64)
- func (s *Session) SetUserData(d interface{})
- func (s *Session) SetUserID(userID int64)
- func (s *Session) UserData() interface{}
- func (s *Session) UserID() int64
- func (s *Session) WritePacket(p Packet) (err error)
- type SessionCallback
- type SessionCreator
- type TcpNetConn
- type TcpNetListener
- type UserNats
- type WebSocketNetConn
- type WebSocketNetListener
Constants ¶
const ( StateZero = iota // 服务初始状态,服务连接数为0 StateIdle // 服务空闲,优先获得连接 StateAliave // 服务正常运行,均衡获得连接 StateBusy // 服务繁忙,滞后获得连接 StateFull // 服务满载,不获得新连接 )
const (
// 向NATS中push推送给其他玩家消息的键
NatsPushUserK = `GAME_PUSH_USER:%s`
)
Variables ¶
Functions ¶
Types ¶
type App ¶
type App struct {
boot.BaseInstance
// contains filtered or unexported fields
}
App 游戏服务器实现
func (*App) AddAfterMiddleware ¶ added in v0.1.2
func (a *App) AddAfterMiddleware(m Middleware)
AddAfterMiddleware 设置详细处理后的中间件
func (*App) AddBeforeMiddleware ¶ added in v0.1.2
func (a *App) AddBeforeMiddleware(m Middleware)
SetBeforeMiddleware 设置消息处理前中间件
func (*App) AddSession ¶ added in v0.0.4
AddSession 加入Session
func (*App) DelSession ¶ added in v0.1.5
DelSession 删除Session
func (*App) OnHeartbeat ¶ added in v0.1.6
OnHeartbeat 心跳处理
func (*App) SendAllActor ¶ added in v0.1.2
SendAllActor 向所有玩家发送消息
func (*App) SetConnectListener ¶
SetConnectListener 设置连接监听器
func (*App) SetDisconnectListener ¶
SetDisconnectListener 设置断连监听器
func (*App) SetHeartbeatListener ¶ added in v0.1.6
SetHeartbeatListener 设置心跳监听器
func (*App) UpdateGdExtra ¶ added in v0.1.6
UpdateGdExtra 修改服务的
func (*App) UpdateGdState ¶ added in v0.1.2
UpdateGdState 修改服务状态
type Client ¶ added in v0.0.4
type Client struct {
Conn NetConn
// contains filtered or unexported fields
}
Client tcp/kcp的传输客户端
func NewClient ¶ added in v0.0.4
func NewClient(network string, config NetConfig, readLimit int, sendLimit int, heart time.Duration) (*Client, error)
NewClient 新客户端 conn 连接协议实例 readLimit 最大读取包 sendLimit 最大写入包 heart 心跳周期
func (*Client) Send ¶ added in v0.0.4
func (c *Client) Send(opcode uint16, msg protoreflect.ProtoMessage) error
Send 发送消息
type Context ¶
type Context interface {
// Context 返回一个context.Context
Context() context.Context
// Parmas 取出请求参数
Params(m protoreflect.ProtoMessage) error
// Session 获取这个玩家的Session
Session() *Session
// Send 发送消息到玩家
Send(opcode interface{}, msg proto.Message) error
// SendActor 向其他玩家发送消息
SendActor(userID int64, opcode interface{}, msg proto.Message) error
// SendAllActor 向所有玩家发送消息
SendAllActor(opcode interface{}, msg proto.Message) error
// SendActorLocation 向本地其他玩家发送Actor
SendActorLocation(userID int64, opcode interface{}, msg proto.Message) error
// SendActorPush 向远程玩家发送Actor
SendActorPush(userID int64, opcode interface{}, msg proto.Message) error
// Valid 验证玩家成功,传入用户ID
Valid(userID int64)
// GetOpCode 获取此次请求的opcode
GetOpCode() uint16
// GetRequestID 获得请求ID
GetRequestID() string
}
Context 抽象每个Handler的调用参数
type DefaultContext ¶
type DefaultContext struct {
// contains filtered or unexported fields
}
DefaultContext 默认Context实现
func (*DefaultContext) Context ¶
func (c *DefaultContext) Context() context.Context
func (*DefaultContext) GetOpCode ¶ added in v0.1.2
func (c *DefaultContext) GetOpCode() uint16
GetOpCode 获取此次处理的opcode
func (*DefaultContext) GetRequestID ¶ added in v0.1.0
func (c *DefaultContext) GetRequestID() string
GetRequestID 获得请求ID
func (*DefaultContext) Params ¶
func (c *DefaultContext) Params(m protoreflect.ProtoMessage) error
func (*DefaultContext) Send ¶
func (c *DefaultContext) Send(opcode interface{}, msg proto.Message) error
WritePbPacket 写入Protobuf的包
func (*DefaultContext) SendActor ¶ added in v0.0.4
func (c *DefaultContext) SendActor(userID int64, opcode interface{}, msg proto.Message) error
SendActor 向指定玩家发送消息
func (*DefaultContext) SendActorLocation ¶ added in v0.1.5
func (c *DefaultContext) SendActorLocation(userID int64, opcode interface{}, msg proto.Message) error
SendActorLocation 向本地其他玩家发送Actor
func (*DefaultContext) SendActorPush ¶ added in v0.1.5
func (c *DefaultContext) SendActorPush(userID int64, opcode interface{}, msg proto.Message) error
SendActorPush 向远程玩家发送消息
func (*DefaultContext) SendAllActor ¶ added in v0.1.2
func (c *DefaultContext) SendAllActor(opcode interface{}, msg proto.Message) error
SendAllActor 向所有玩家发送消息
func (*DefaultContext) Session ¶
func (c *DefaultContext) Session() *Session
func (*DefaultContext) Valid ¶ added in v0.0.4
func (c *DefaultContext) Valid(userID int64)
Valid 验证成功
type DefaultPacket ¶
type DefaultPacket struct {
// contains filtered or unexported fields
}
DefaultPacket 基于Protobuffer的包协议
func NewDefaultPacket ¶
func NewDefaultPacket(buff []byte, opcode uint16) *DefaultPacket
NewPbPacket 新建一个pb的Packet
func (*DefaultPacket) Serialize ¶
func (p *DefaultPacket) Serialize() []byte
Serialize 序列化,输出完整的字符数组
type GameMetaData ¶ added in v0.1.2
type GameMetaData struct {
ID int64 `json:"id"` // 实例ID
Network string `json:"network"` // 传输协议
UseTLS bool `json:"use_tls"` // 是否启用TLS
Fuse bool `json:"fuse"` // 熔断开关,true为开启,熔断状态下,服务不接受新连接,等待服务器降到0
State int `json:"state"` // 状态
Password string `json:"password"` // 该服务加密使用的密码
OutUrl string `json:"out_url"` // 外部访问的URL
Extra interface{} `json:"extra"` // 额外数据,自定义舒勇
}
type KcpNetConn ¶ added in v0.1.1
type KcpNetConn struct {
// contains filtered or unexported fields
}
KcpNetConn kcp协议的连接抽象
func (*KcpNetConn) ReadPacket ¶ added in v0.1.1
func (k *KcpNetConn) ReadPacket() (Packet, error)
ReadPacket 读取数据包
func (*KcpNetConn) WritePacket ¶ added in v0.1.1
func (k *KcpNetConn) WritePacket(p Packet) error
WritePacket 写入数据包
type KcpNetListener ¶ added in v0.1.1
type KcpNetListener struct {
// contains filtered or unexported fields
}
KcpNetListener kcp连接监听器
func (*KcpNetListener) Accept ¶ added in v0.1.1
func (l *KcpNetListener) Accept() (NetConn, error)
Accept 接收连接
type Middleware ¶
type Middleware struct {
// Condition 是否要处理的opcode
Condition func(uint16) bool
// Do 处理执行
Do func(Context) error
}
Middleware 中间件处理
type NetConfig ¶ added in v0.1.1
type NetConfig struct {
Addr string // 监听地址
TLSConfig *tls.Config // TLS配置
WriteTimeout time.Duration // 写入超时
ReadeTimeout time.Duration // 读取超时
WebSocketPath string // ws连接的升级使用地址
KcpMode string // kcp的模式
}
NetConfig 配置
type NetConn ¶ added in v0.1.1
type NetConn interface {
// ReadPacket 读取报文
ReadPacket() (Packet, error)
// WritePacket 写入报文
WritePacket(Packet) error
// Close 关闭连接
Close()
}
NetConn 传输协议的抽象
func NewKcpNetConn ¶ added in v0.1.1
NewKcpNetConn 创建一个kcp连接
func NewTcpNetConn ¶ added in v0.1.1
NewTcpNetConn 创建一个Tcp连接
type NetListener ¶ added in v0.1.1
NetListener 接收请求的监听器
func NewKcpNetListener ¶ added in v0.1.1
func NewKcpNetListener(config NetConfig) (NetListener, error)
NewKcpNetListener 创建一个kcp连接监听器
func NewTcpNetListener ¶ added in v0.1.1
func NewTcpNetListener(config NetConfig) (NetListener, error)
NewTcpNetListener 创建一个tcp连接监听器
func NewWebSocketNetListener ¶ added in v0.1.1
func NewWebSocketNetListener(config NetConfig) (NetListener, error)
NewWebSocketNetListener 创建Websocket监听器
type Packet ¶
type Packet interface {
// Serialize 序列化
Serialize() []byte
// OpeCode 获取该包的OpCode
OpCode() uint16
// BodyLen 内容长度
BodyLen() uint16
// Body 获取完整body
Body() []byte
// Free 释放空间
Free()
}
Packet 包接口
var ( HeartPacket Packet = NewDefaultPacket(nil, 0) DefaultPacketPool = sync.Pool{ New: func() interface{} { return new(DefaultPacket) }, } )
HeartPacket 默认使用0号协议作为心跳包的协议
func CretaePbPacket ¶
CretaePbPacket 创建要给protobuf的包
type PushActorMessage ¶ added in v0.0.4
type PushActorMessage struct {
UserID int64 `json:"user_id"`
OpCode uint16 `json:"opcode"`
Msg []byte `json:"msg"`
}
PushActorMessage 发送消息给远程玩家
type Session ¶
type Session struct {
ID int64 // session的唯一标识
// contains filtered or unexported fields
}
Session 网络会话
func (*Session) Run ¶
func (s *Session) Run()
Run 执行主体逻辑,三套循环 readLoop 读循环 writeLoop 写循环 handleLopp 处理消息循环
func (*Session) SetExpired ¶ added in v0.1.5
SetExpired 设置session的过期时间
type SessionCallback ¶
type SessionCallback interface {
// OnConnect 当连接建立时调用
OnConnect(*Session) bool
// OnHeartbeat 当心跳包到达时处理
OnHeartbeat(*Session)
// OnMessage 当连接处理消息时
OnMessage(*Session, Packet) bool
// OnDisConnect 当连接断开时
OnDisConnect(*Session)
}
SessionCallback session触发外部事件调用
type SessionCreator ¶
SessionCreator session创建生成器,做一些预处理
type TcpNetConn ¶ added in v0.1.1
type TcpNetConn struct {
// contains filtered or unexported fields
}
TcpNetConn tpc协议的连接抽象
func (*TcpNetConn) ReadPacket ¶ added in v0.1.1
func (t *TcpNetConn) ReadPacket() (Packet, error)
ReadPacket 读取数据报文
func (*TcpNetConn) WritePacket ¶ added in v0.1.1
func (t *TcpNetConn) WritePacket(p Packet) error
WritePacket 写入数据报文
type TcpNetListener ¶ added in v0.1.1
type TcpNetListener struct {
// contains filtered or unexported fields
}
TcpNetListener tcp连接监听器
func (*TcpNetListener) Accept ¶ added in v0.1.1
func (l *TcpNetListener) Accept() (NetConn, error)
Accept 接收连接
type UserNats ¶ added in v0.1.2
type UserNats struct {
// contains filtered or unexported fields
}
UserNats 向用户发送消息
func NewUserNats ¶ added in v0.1.2
NewUserNats 创建一个向用户发送消息的nats
func (*UserNats) Consume ¶ added in v0.1.2
func (u *UserNats) Consume() <-chan PushActorMessage
Consume 读取消费到的用户消息
func (*UserNats) StartSubscribe ¶ added in v0.1.2
StartSubscribe 开始消费用户消息
type WebSocketNetConn ¶ added in v0.1.1
type WebSocketNetConn struct {
// contains filtered or unexported fields
}
WebSocketNetConn Websocket协议的连接抽象
func (*WebSocketNetConn) ReadPacket ¶ added in v0.1.1
func (w *WebSocketNetConn) ReadPacket() (Packet, error)
ReadPacket 读取数据包
func (*WebSocketNetConn) WritePacket ¶ added in v0.1.1
func (w *WebSocketNetConn) WritePacket(p Packet) error
WritePacket 写入数据包
type WebSocketNetListener ¶ added in v0.1.1
type WebSocketNetListener struct {
// contains filtered or unexported fields
}
WebSocketNetListener websocket连接的监听器
func (*WebSocketNetListener) Accept ¶ added in v0.1.1
func (l *WebSocketNetListener) Accept() (NetConn, error)
Accept 接收请求
func (*WebSocketNetListener) Close ¶ added in v0.1.1
func (l *WebSocketNetListener) Close()
Close 关闭连接