Documentation ¶
Index ¶
- Constants
- Variables
- func EncodeInternalMessage(message *InternalMessage) ([]byte, error)
- func EncodeLimitMessage(message *Message, limitSize int) ([]byte, error)
- func EncodeMessage(message *Message) ([]byte, error)
- func IsTimeout(err error) bool
- func RegisterMakeListener(network string, ml MakeListener)
- func WriteInternalMessage(conn io.Writer, msg *InternalMessage) error
- func WriteLimitMessage(conn io.Writer, message *Message, limitSize int) error
- func WriteMessage(conn io.Writer, message *Message) error
- type Client
- func (client *Client) Close()
- func (client *Client) EnqueueEvent(fn func(*Client)) bool
- func (client *Client) EnqueueMessage(msg *Message) bool
- func (client *Client) EnqueueNonBlockMessage(msg *Message) bool
- func (client *Client) EnsureEvent(fn func(*Client))
- func (client *Client) FlushMessage()
- func (client *Client) LocalAddr() net.Addr
- func (client *Client) Log() string
- func (client *Client) Run()
- func (client *Client) SendLMessages()
- func (client *Client) String() string
- type ClientSet
- type Conn
- type DataCreator
- type Dispatcher
- type Exchanger
- type ExternalImp
- func (e *ExternalImp) Clone() *ExternalImp
- func (e *ExternalImp) HandleAuthClient(client *Client) bool
- func (e *ExternalImp) HandleBeforeWriteMessage(client *Client, message *Message)
- func (e *ExternalImp) HandleClientClosed(client *Client)
- func (e *ExternalImp) HandleMessage(client *Client, msg *Message)
- func (e *ExternalImp) SetBeforeWrite(h MessageHandler)
- func (e *ExternalImp) SetDefaultHandler(h MessageHandler, filters ...Filter)
- func (e *ExternalImp) SetMsgHandler(cmd int, h MessageHandler, filters ...Filter)
- func (e *ExternalImp) SetOnAuthClient(h func(*Client) bool)
- func (e *ExternalImp) SetOnClientClosed(h func(*Client))
- type ExternalPlugin
- type Filter
- type InternalMessage
- type InternalMessageHandler
- type ListenerConfig
- type MakeListener
- type Message
- type MessageBroker
- type MessageExchanger
- type MessageHandler
- type NetConn
- type NopPusher
- type OptionFn
- func WithExternalPlugin(plugin ExternalPlugin) OptionFn
- func WithListenAddr(addr string) OptionFn
- func WithMaxConn(n int) OptionFn
- func WithNetwork(network string) OptionFn
- func WithOptions(ops map[string]interface{}) OptionFn
- func WithReadTimeout(readTimeout time.Duration) OptionFn
- func WithTLSConfig(cfg *tls.Config) OptionFn
- func WithWriteTimeout(writeTimeout time.Duration) OptionFn
- type ProtocolBody
- type ProtocolData
- type ProtocolHeader
- type Publisher
- type Pusher
- type Router
- type Server
Constants ¶
const ( DefaultReadTimeout = time.Minute * 15 DefaultWriteTimeout = time.Second * 10 DefaultMaxConn = 65536 )
const (
MessageQueueLimit = 1000
)
Variables ¶
Functions ¶
func EncodeInternalMessage ¶
func EncodeInternalMessage(message *InternalMessage) ([]byte, error)
func EncodeLimitMessage ¶
限制编码消息
func RegisterMakeListener ¶
func RegisterMakeListener(network string, ml MakeListener)
RegisterMakeListener registers a MakeListener for network.
func WriteInternalMessage ¶
func WriteInternalMessage(conn io.Writer, msg *InternalMessage) error
func WriteLimitMessage ¶
限制写
Types ¶
type Client ¶
type Client struct { UID int64 // 用户id UserData interface{} // 用户其他私有数据 DC DataCreator // 协议数据构建器 Version int32 // contains filtered or unexported fields }
func (*Client) EnqueueEvent ¶
func (*Client) EnqueueNonBlockMessage ¶
发送非阻塞消息
type ClientSet ¶
type ClientSet map[*Client]struct{}
ClientSet is client set
func NewClientSet ¶
func NewClientSet() ClientSet
type Conn ¶
type Conn interface { Read([]byte) (int, error) Write(b []byte) (int, error) // 写 RemoteAddr() net.Addr // 远端地址 LocalAddr() net.Addr // 本地地址 Close() error // 关闭 }
为什么不直接用net.Conn? 考虑兼容 websocket.Conn等其他情况
type DataCreator ¶
type DataCreator interface { CreateHeader() ProtocolHeader CreateBody(cmd int) ProtocolBody GetCmd(body interface{}) (int, bool) GetCmd2(t reflect.Type) (int, bool) GetDescription(cmd int) string }
协议数据创建器,可以分别创建头和body 定义DataCreator的作用之一是,在必要的时候,可以对不同的客户端使用不同的数据交换协议
type Dispatcher ¶
type Dispatcher interface {
DispatchMessage(*InternalMessage) bool
}
本地消息分发,外部发送到本服务的消息
type Exchanger ¶
type Exchanger struct { MessageBroker // Pusher // InternalMessageHandler // // contains filtered or unexported fields }
Exchanger is just a example 消息交换机,处理消息的对内分发和对外分发 TODO add feedback for not dispatched or not published
func NewMessageExchanger ¶
func NewMessageExchanger(broker MessageBroker, pusher Pusher, handler InternalMessageHandler, router *Router) *Exchanger
func (*Exchanger) DispatchMessage ¶
func (exc *Exchanger) DispatchMessage(msg *InternalMessage) bool
直接下发 单纯的进行消息下发,未考虑业务消息
func (*Exchanger) PublishMessage ¶
func (exc *Exchanger) PublishMessage(msg *InternalMessage) bool
发布消息到Broker
type ExternalImp ¶
type ExternalImp struct {
// contains filtered or unexported fields
}
func NewExternalImp ¶
func NewExternalImp() *ExternalImp
func (*ExternalImp) Clone ¶
func (e *ExternalImp) Clone() *ExternalImp
func (*ExternalImp) HandleAuthClient ¶
func (e *ExternalImp) HandleAuthClient(client *Client) bool
func (*ExternalImp) HandleBeforeWriteMessage ¶
func (e *ExternalImp) HandleBeforeWriteMessage(client *Client, message *Message)
func (*ExternalImp) HandleClientClosed ¶
func (e *ExternalImp) HandleClientClosed(client *Client)
func (*ExternalImp) HandleMessage ¶
func (e *ExternalImp) HandleMessage(client *Client, msg *Message)
func (*ExternalImp) SetBeforeWrite ¶
func (e *ExternalImp) SetBeforeWrite(h MessageHandler)
func (*ExternalImp) SetDefaultHandler ¶
func (e *ExternalImp) SetDefaultHandler(h MessageHandler, filters ...Filter)
func (*ExternalImp) SetMsgHandler ¶
func (e *ExternalImp) SetMsgHandler(cmd int, h MessageHandler, filters ...Filter)
func (*ExternalImp) SetOnAuthClient ¶
func (e *ExternalImp) SetOnAuthClient(h func(*Client) bool)
func (*ExternalImp) SetOnClientClosed ¶
func (e *ExternalImp) SetOnClientClosed(h func(*Client))
type ExternalPlugin ¶
type ExternalPlugin interface { HandleAuthClient(*Client) bool // run的第一步, auth 认证客户端,至少确定协议方式,亦即 DataCreator HandleMessage(*Client, *Message) // 消息处理函数 HandleClientClosed(*Client) // 关闭客户端之后的处理 HandleBeforeWriteMessage(*Client, *Message) // }
由具体业务实现的方法函数 must
type Filter ¶
type Filter func(MessageHandler) MessageHandler
server和gate 的外部函数实现 example for ExternalPlugin
type InternalMessage ¶
type InternalMessage struct { *Message // 发送的消息体 Sender int64 // 发送人 Receiver int64 // 接收人 Timestamp int64 // 时间戳,ms }
内部消息,服务之间或者组件之间进行消息交换 example
func DecodeInternalMessgae ¶
func DecodeInternalMessgae(b []byte, dc DataCreator) (*InternalMessage, error)
解码
func ReadInternalMessage ¶
func ReadInternalMessage(reader io.Reader, dc DataCreator) (*InternalMessage, error)
编码Message
type InternalMessageHandler ¶
type InternalMessageHandler interface {
HandleInternalMessage(msg *InternalMessage) // 和Client不一样的地方
}
这个涉及到离线用户的消息处理,只能由业务处理
type ListenerConfig ¶
type MakeListener ¶
type MakeListener func(cfg *ListenerConfig) (ln net.Listener, err error)
listener
type Message ¶
type Message struct { Header ProtocolHeader Body ProtocolBody }
一个完整的消息包含头和body两部分
func ReadLimitMessage ¶
限制读
type MessageBroker ¶
type MessageBroker interface { Connect() // 连接 Subscribe(uid int64) // 注册用户 UnSubscribe(uid int64) // 注销用户 SendMessage(msg *InternalMessage) error // 发布消息 ReceiveMessage() (*InternalMessage, error) // 接收消息 SyncMessage(msg *InternalMessage) (*InternalMessage, error) // 同步消息请求 Close() }
分布式用户消息交换
type MessageExchanger ¶
type MessageExchanger interface { MessageBroker Dispatcher Publisher InternalMessageHandler }
消息交换机
type MessageHandler ¶
server和gate 的外部函数实现 example for ExternalPlugin
func FilterRecover ¶
func FilterRecover(fn MessageHandler) MessageHandler
filter 防止panic just for example
type NopPusher ¶
type NopPusher struct{}
func (*NopPusher) PushMessage ¶
func (p *NopPusher) PushMessage(msg *InternalMessage) bool
type OptionFn ¶
type OptionFn func(*Server)
OptionFn 对服务的参数进行插入式配置
func WithExternalPlugin ¶
func WithExternalPlugin(plugin ExternalPlugin) OptionFn
WithExternalPlugin sets server plugin
func WithOptions ¶
WithOptions sets multiple options.
func WithReadTimeout ¶
WithReadTimeout sets readTimeout.
func WithWriteTimeout ¶
WithWriteTimeout sets writeTimeout.
type ProtocolData ¶
协议数据,定义了数据基本交换协议 头和body 都属于协议数据
type ProtocolHeader ¶
type ProtocolHeader interface { ProtocolData Length() int Cmd() int // 协议指令 SetCmd(int) // 指定协议指令 Seq() int SetSeq(int) BodyLength() int SetBodyLength(n int) Ver() int SetVer(v int) Clone() ProtocolHeader }
协议头
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router 进行用户客户端管理/查找的路由服务 example
func (*Router) FindClient ¶
FindClient 只查找一个在线client
func (*Router) FindClientSet ¶
func (*Router) RemoveClient ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server 提供一个连接服务
func NewServerWithConfig ¶
func NewServerWithConfig(cfg *ListenerConfig, options ...OptionFn) *Server
新建服务