Documentation ¶
Index ¶
- Constants
- type AsyncReceiver
- func (r *AsyncReceiver) Close()
- func (r *AsyncReceiver) Conn() Connection
- func (r *AsyncReceiver) Ctx() *ServerContext
- func (r *AsyncReceiver) Recv() (Message, error)
- func (r *AsyncReceiver) RecvChan() <-chan Message
- func (r *AsyncReceiver) Send(msg Message) error
- func (r *AsyncReceiver) String() string
- type Callback
- type Connection
- type ConnectionConfig
- type DefaultMessageMux
- type FuncUDPConnectionSend
- type Listener
- type Message
- type MessageHandler
- type MessageMux
- type MessageProtocol
- type MessageServeAsync
- type MessageServeSync
- type RawMessage
- type RawUDPPacket
- type Receiver
- type Server
- type ServerBuilder
- type ServerContext
- type SyncReceiver
- type TCPConnection
- type TCPConnectionConfig
- type TCPListener
- type TCPListenerConfig
- type UDPConnection
- func (uc *UDPConnection) Close() error
- func (uc *UDPConnection) ID() uint64
- func (uc *UDPConnection) IsClosed() bool
- func (uc *UDPConnection) LastRecvTs() int64
- func (uc *UDPConnection) PushPacketClip(clip *UDPPacketClip) error
- func (uc *UDPConnection) Recv() (*RawMessage, error)
- func (uc *UDPConnection) Remote() string
- func (uc *UDPConnection) Send(msg *RawMessage) error
- type UDPConnectionConfig
- type UDPListener
- type UDPListenerConfig
- type UDPPacket
- type UDPPacketClip
Constants ¶
View Source
const UDPPacketClipHeaderLen = 8 + 8 + 2 + 2
View Source
const UDPPacketClipLimit = 480
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AsyncReceiver ¶
type AsyncReceiver struct {
// contains filtered or unexported fields
}
异步接收者 生命周期中会持有2个goroutine 必须由用户代码Close, 才能关闭连接和goroutine
func NewAsyncReceiver ¶ added in v0.0.2
func NewAsyncReceiver(ctx *ServerContext, conn Connection) *AsyncReceiver
func (*AsyncReceiver) Close ¶ added in v0.0.2
func (r *AsyncReceiver) Close()
func (*AsyncReceiver) Conn ¶ added in v0.0.2
func (r *AsyncReceiver) Conn() Connection
func (*AsyncReceiver) Ctx ¶ added in v0.0.2
func (r *AsyncReceiver) Ctx() *ServerContext
func (*AsyncReceiver) Recv ¶ added in v0.0.2
func (r *AsyncReceiver) Recv() (Message, error)
接收消息. 尝试从接收缓冲区中读一个消息, 缓冲区空时直接返回nil
func (*AsyncReceiver) Send ¶ added in v0.0.2
func (r *AsyncReceiver) Send(msg Message) error
发送消息. 消息将进入发送缓冲区
func (*AsyncReceiver) String ¶ added in v0.0.2
func (r *AsyncReceiver) String() string
type Callback ¶
type Callback struct { OnDebug func(string) OnError func(string, error, Receiver, Message) OnNewReceiver func(Receiver) }
func (*Callback) TrigNewReceiver ¶
type Connection ¶
type Connection interface { ID() uint64 Remote() string Send(*RawMessage) error Recv() (*RawMessage, error) Close() error }
连接的抽象封装
type ConnectionConfig ¶
type DefaultMessageMux ¶
type DefaultMessageMux struct {
// contains filtered or unexported fields
}
缺省实现的消息路由集
func NewDefaultMessageMux ¶
func NewDefaultMessageMux() *DefaultMessageMux
func (*DefaultMessageMux) Handle ¶
func (mux *DefaultMessageMux) Handle(protoID uint32, h MessageHandler)
func (*DefaultMessageMux) HandleUnknown ¶
func (mux *DefaultMessageMux) HandleUnknown(h MessageHandler)
func (*DefaultMessageMux) Handler ¶
func (mux *DefaultMessageMux) Handler(protoID uint32) MessageHandler
type FuncUDPConnectionSend ¶ added in v0.0.2
UDP消息发送的调用函数原型
type Listener ¶
type Listener interface { Ctx() *ServerContext Addr() string //blocking function. 建议起goroutine Accepting(cbNewConn func(Connection)) Stop() }
监听器的抽象封装
func NewTCPListener ¶
func NewTCPListener(ctx *ServerContext) (Listener, error)
func NewUDPListener ¶ added in v0.0.2
func NewUDPListener(ctx *ServerContext) (Listener, error)
type MessageMux ¶
type MessageMux interface { Handle(uint32, MessageHandler) Handler(uint32) MessageHandler //如果注册了此方法, 那么所有未被路由命中的消息都将由此函数处理 HandleUnknown(MessageHandler) }
消息路由集定义
type MessageProtocol ¶
type MessageProtocol interface { Encode(msg Message) ([]byte, error) Decode(protoID uint32, bs []byte) (Message, error) New(protoID uint32) Message }
消息协议集
type MessageServeAsync ¶
type MessageServeAsync struct {
// contains filtered or unexported fields
}
消息服务异步型
func NewMessageServeAsync ¶
func NewMessageServeAsync(r *AsyncReceiver, mux MessageMux) *MessageServeAsync
func (*MessageServeAsync) Mux ¶
func (ms *MessageServeAsync) Mux() MessageMux
func (*MessageServeAsync) Processing ¶
func (ms *MessageServeAsync) Processing(ctx context.Context)
blocking. 建议加goroutine, 并使用context.WithCancel 持续地响应消息
func (*MessageServeAsync) Receiver ¶
func (ms *MessageServeAsync) Receiver() *AsyncReceiver
type MessageServeSync ¶
type MessageServeSync struct {
// contains filtered or unexported fields
}
消息服务同步型
func NewMessageServeSync ¶
func NewMessageServeSync(r *SyncReceiver, mux MessageMux) *MessageServeSync
func (*MessageServeSync) Mux ¶
func (ms *MessageServeSync) Mux() MessageMux
func (*MessageServeSync) Receiver ¶
func (ms *MessageServeSync) Receiver() *SyncReceiver
type RawUDPPacket ¶ added in v0.0.2
原始的UDP报文, 由远端地址和字节块组成
type Receiver ¶
type Receiver interface { Ctx() *ServerContext Conn() Connection Close() String() string Send(Message) error Recv() (Message, error) }
接收者抽象定义
type Server ¶
type Server struct { Ctx *ServerContext Listener Listener CBNewConnection func(Connection) // contains filtered or unexported fields }
服务实例
type ServerBuilder ¶
type ServerBuilder struct { Ctx *ServerContext //选用的监听器实现 NewListener func(*ServerContext) (Listener, error) //选用的接收者实现 NewReceiver func(*ServerContext, Connection) Receiver }
服务构造器, 用于配置具体实现的组合方案
type ServerContext ¶
type ServerContext struct { MessageProtocol MessageProtocol CB *Callback TCPListenerConfig *TCPListenerConfig TCPConnectionConfig *TCPConnectionConfig UDPListenerConfig *UDPListenerConfig UDPConnectionConfig *UDPConnectionConfig }
服务上下文 包含服务内部各组件的共享资源
type SyncReceiver ¶
type SyncReceiver struct {
// contains filtered or unexported fields
}
同步接收者 必须由用户代码Close, 才能关闭连接ss
func NewSyncReceiver ¶ added in v0.0.2
func NewSyncReceiver(ctx *ServerContext, conn Connection) *SyncReceiver
func (*SyncReceiver) Close ¶ added in v0.0.2
func (r *SyncReceiver) Close()
func (*SyncReceiver) Conn ¶ added in v0.0.2
func (r *SyncReceiver) Conn() Connection
func (*SyncReceiver) Ctx ¶ added in v0.0.2
func (r *SyncReceiver) Ctx() *ServerContext
func (*SyncReceiver) Recv ¶ added in v0.0.2
func (r *SyncReceiver) Recv() (Message, error)
阻塞同步的接收一个消息
func (*SyncReceiver) Send ¶ added in v0.0.2
func (r *SyncReceiver) Send(msg Message) error
阻塞同步的发送一个消息
func (*SyncReceiver) String ¶ added in v0.0.2
func (r *SyncReceiver) String() string
type TCPConnection ¶
type TCPConnection struct {
// contains filtered or unexported fields
}
连接的TCP封装
func NewTCPConnection ¶
func NewTCPConnection(cfg *TCPConnectionConfig, conn *net.TCPConn, id uint64) *TCPConnection
func (*TCPConnection) Close ¶
func (tc *TCPConnection) Close() error
func (*TCPConnection) ID ¶
func (tc *TCPConnection) ID() uint64
func (*TCPConnection) Remote ¶
func (tc *TCPConnection) Remote() string
type TCPConnectionConfig ¶
type TCPConnectionConfig struct {
*ConnectionConfig
}
type TCPListener ¶
type TCPListener struct {
// contains filtered or unexported fields
}
监听器的TCP封装 监听构造的连接封装为TCPConnection
func (*TCPListener) Accepting ¶
func (l *TCPListener) Accepting(cbNewConn func(Connection))
blocking. 监听等待循环
func (*TCPListener) Addr ¶
func (l *TCPListener) Addr() string
func (*TCPListener) Ctx ¶
func (l *TCPListener) Ctx() *ServerContext
func (*TCPListener) Stop ¶
func (l *TCPListener) Stop()
type TCPListenerConfig ¶
type TCPListenerConfig struct {
ListenAddress string
}
type UDPConnection ¶ added in v0.0.2
type UDPConnection struct {
// contains filtered or unexported fields
}
连接的UDP封装
func NewUDPConnection ¶ added in v0.0.2
func NewUDPConnection(cfg *UDPConnectionConfig, id uint64, addr *net.UDPAddr, fSend FuncUDPConnectionSend) *UDPConnection
func (*UDPConnection) Close ¶ added in v0.0.2
func (uc *UDPConnection) Close() error
func (*UDPConnection) ID ¶ added in v0.0.2
func (uc *UDPConnection) ID() uint64
func (*UDPConnection) IsClosed ¶ added in v0.0.2
func (uc *UDPConnection) IsClosed() bool
func (*UDPConnection) LastRecvTs ¶ added in v0.0.2
func (uc *UDPConnection) LastRecvTs() int64
最近一次接到消息的时间. 接到分片就算更新.
func (*UDPConnection) PushPacketClip ¶ added in v0.0.2
func (uc *UDPConnection) PushPacketClip(clip *UDPPacketClip) error
推入一个消息分片
func (*UDPConnection) Recv ¶ added in v0.0.2
func (uc *UDPConnection) Recv() (*RawMessage, error)
阻塞的接收消息
func (*UDPConnection) Remote ¶ added in v0.0.2
func (uc *UDPConnection) Remote() string
func (*UDPConnection) Send ¶ added in v0.0.2
func (uc *UDPConnection) Send(msg *RawMessage) error
不能并发调用
type UDPConnectionConfig ¶ added in v0.0.2
type UDPConnectionConfig struct { *ConnectionConfig CacheClipLimit int }
type UDPListener ¶ added in v0.0.2
type UDPListener struct {
// contains filtered or unexported fields
}
监听器的UDP封装
func (*UDPListener) Accepting ¶ added in v0.0.2
func (l *UDPListener) Accepting(cbNewConn func(Connection))
blocking. 监听等待循环
func (*UDPListener) Addr ¶ added in v0.0.2
func (l *UDPListener) Addr() string
func (*UDPListener) Ctx ¶ added in v0.0.2
func (l *UDPListener) Ctx() *ServerContext
func (*UDPListener) Stop ¶ added in v0.0.2
func (l *UDPListener) Stop()
type UDPListenerConfig ¶ added in v0.0.2
type UDPPacketClip ¶ added in v0.0.2
type UDPPacketClip struct { ConnID uint64 Addr *net.UDPAddr PacketSeq uint64 ClipLen uint16 ClipSeq uint16 Payload []byte }
UDP报文头顺序: uint64 连接ID uint64 消息序号 uint16 分片数 uint16 分片序号 [0, ~)
func (*UDPPacketClip) Decode ¶ added in v0.0.2
func (clip *UDPPacketClip) Decode(bs []byte) error
func (*UDPPacketClip) Encode ¶ added in v0.0.2
func (clip *UDPPacketClip) Encode() ([]byte, error)
Source Files ¶
Click to show internal directories.
Click to hide internal directories.