Documentation
¶
Index ¶
- Variables
- func EncodeMessage(msg Message) []byte
- func EraseMessages()
- func Now() time.Time
- func RegisterMessage(prefix MessagePrefix, msg interface{})
- func VerifyMessages()
- type Config
- type ConnectCallback
- type Connection
- type ConnectionPool
- func (pool *ConnectionPool) BroadcastMessage(msg Message) error
- func (pool *ConnectionPool) ClearStaleConnections(idleLimit time.Duration, reason DisconnectReason) error
- func (pool *ConnectionPool) Connect(address string) error
- func (pool *ConnectionPool) Disconnect(addr string, r DisconnectReason) error
- func (pool *ConnectionPool) GetConnection(addr string) (*Connection, error)
- func (pool *ConnectionPool) GetConnections() ([]Connection, error)
- func (pool *ConnectionPool) IsConnExist(addr string) (bool, error)
- func (pool *ConnectionPool) ListeningAddress() (net.Addr, error)
- func (pool *ConnectionPool) NewConnection(conn net.Conn, solicited bool) (*Connection, error)
- func (pool *ConnectionPool) Run() error
- func (pool *ConnectionPool) RunOffline() error
- func (pool *ConnectionPool) SendMessage(addr string, msg Message) error
- func (pool *ConnectionPool) SendPings(rate time.Duration, msg Message) error
- func (pool *ConnectionPool) Shutdown()
- func (pool *ConnectionPool) Size() (l int, err error)
- type DisconnectCallback
- type DisconnectReason
- type Message
- type MessageContext
- type MessagePrefix
- type SendResult
Constants ¶
This section is empty.
Variables ¶
var MessageIDMap = make(map[reflect.Type]MessagePrefix)
MessageIDMap maps message types to their ids
var MessageIDReverseMap = make(map[MessagePrefix]reflect.Type)
MessageIDReverseMap maps message ids to their types
Functions ¶
func EncodeMessage ¶
EncodeMessage packs a Message into []byte containing length, id and data
func RegisterMessage ¶
func RegisterMessage(prefix MessagePrefix, msg interface{})
RegisterMessage registers a message struct for recognition by the message handlers.
func VerifyMessages ¶
func VerifyMessages()
VerifyMessages calls logger.Panic if message registration violates sanity checks
Types ¶
type Config ¶
type Config struct {
// Address to listen on. Leave empty for arbitrary assignment
Address string
// Port to listen on. Set to 0 for arbitrary assignment
Port uint16
// Connection limits
MaxConnections int
// Messages greater than length are rejected and the sender disconnected
MaxMessageLength int
// Timeout is the timeout for dialing new connections. Use a
// timeout of 0 to ignore timeout.
DialTimeout time.Duration
// Timeout for reading from a connection. Set to 0 to default to the
// system's timeout
ReadTimeout time.Duration
// Timeout for writing to a connection. Set to 0 to default to the
// system's timeout
WriteTimeout time.Duration
// Broadcast result buffers
BroadcastResultSize int
// Individual connections' send queue size. This should be increased
// if send volume per connection is high, so as not to block
ConnectionWriteQueueSize int
// Triggered on client disconnect
DisconnectCallback DisconnectCallback
// Triggered on client connect
ConnectCallback ConnectCallback
// Print debug logs
DebugPrint bool
}
Config gnet config
type ConnectCallback ¶
ConnectCallback triggered on client connect
type Connection ¶
type Connection struct {
// Key in ConnectionPool.Pool
ID int
// TCP connection
Conn net.Conn
// Message buffer
Buffer *bytes.Buffer
// Reference back to ConnectionPool container
ConnectionPool *ConnectionPool
// Last time a message was fully parsed and handled
LastReceived time.Time
// Last time a message was sent to the connection
LastSent time.Time
// Message send queue.
WriteQueue chan Message
Solicited bool
}
Connection is stored by the ConnectionPool
func NewConnection ¶
func NewConnection(pool *ConnectionPool, id int, conn net.Conn, writeQueueSize int, solicited bool) *Connection
NewConnection creates a new Connection tied to a ConnectionPool
func (*Connection) Close ¶
func (conn *Connection) Close()
Close close the connection and write queue
func (*Connection) String ¶
func (conn *Connection) String() string
String returns connection address
type ConnectionPool ¶
type ConnectionPool struct {
// Configuration parameters
Config Config
// Channel for async message sending
SendResults chan SendResult
// contains filtered or unexported fields
}
ConnectionPool connection pool
func NewConnectionPool ¶
func NewConnectionPool(c Config, state interface{}) *ConnectionPool
NewConnectionPool creates a new ConnectionPool that will listen on Config.Port upon StartListen. State is an application defined object that will be passed to a Message's Handle().
func (*ConnectionPool) BroadcastMessage ¶
func (pool *ConnectionPool) BroadcastMessage(msg Message) error
BroadcastMessage sends a Message to all connections in the Pool.
func (*ConnectionPool) ClearStaleConnections ¶
func (pool *ConnectionPool) ClearStaleConnections(idleLimit time.Duration, reason DisconnectReason) error
ClearStaleConnections removes connections that have not sent a message in too long
func (*ConnectionPool) Connect ¶
func (pool *ConnectionPool) Connect(address string) error
Connect to an address
func (*ConnectionPool) Disconnect ¶
func (pool *ConnectionPool) Disconnect(addr string, r DisconnectReason) error
Disconnect removes a connection from the pool by address, and passes a Disconnection to the DisconnectCallback
func (*ConnectionPool) GetConnection ¶
func (pool *ConnectionPool) GetConnection(addr string) (*Connection, error)
GetConnection returns a connection copy if exist
func (*ConnectionPool) GetConnections ¶
func (pool *ConnectionPool) GetConnections() ([]Connection, error)
GetConnections returns an copy of pool connections
func (*ConnectionPool) IsConnExist ¶
func (pool *ConnectionPool) IsConnExist(addr string) (bool, error)
IsConnExist check if the connection of address does exist
func (*ConnectionPool) ListeningAddress ¶
func (pool *ConnectionPool) ListeningAddress() (net.Addr, error)
ListeningAddress returns address, on which the ConnectionPool listening on. It returns nil, and error if the ConnectionPool is not listening
func (*ConnectionPool) NewConnection ¶
func (pool *ConnectionPool) NewConnection(conn net.Conn, solicited bool) (*Connection, error)
NewConnection creates a new Connection around a net.Conn. Trying to make a connection to an address that is already connected will failed.
func (*ConnectionPool) RunOffline ¶
func (pool *ConnectionPool) RunOffline() error
RunOffline runs the pool in offline mode. No connections will be accepted, but strand requests are processed.
func (*ConnectionPool) SendMessage ¶
func (pool *ConnectionPool) SendMessage(addr string, msg Message) error
SendMessage sends a Message to a Connection and pushes the result onto the SendResults channel.
func (*ConnectionPool) SendPings ¶
func (pool *ConnectionPool) SendPings(rate time.Duration, msg Message) error
SendPings sends a ping if our last message sent was over pingRate ago
func (*ConnectionPool) Shutdown ¶
func (pool *ConnectionPool) Shutdown()
Shutdown gracefully shutdown the connection pool
func (*ConnectionPool) Size ¶
func (pool *ConnectionPool) Size() (l int, err error)
Size returns the pool size
type DisconnectCallback ¶
type DisconnectCallback func(addr string, reason DisconnectReason)
DisconnectCallback triggered on client disconnect
type DisconnectReason ¶
type DisconnectReason error
DisconnectReason is passed to ConnectionPool's DisconnectCallback
var ( // ErrDisconnectReadFailed also includes a remote closed socket ErrDisconnectReadFailed DisconnectReason = errors.New("Read failed") // ErrDisconnectWriteFailed write faile ErrDisconnectWriteFailed DisconnectReason = errors.New("Write failed") // ErrDisconnectSetReadDeadlineFailed set read deadline failed ErrDisconnectSetReadDeadlineFailed = errors.New("SetReadDeadline failed") // ErrDisconnectInvalidMessageLength invalid message length ErrDisconnectInvalidMessageLength DisconnectReason = errors.New("Invalid message length") // ErrDisconnectMalformedMessage malformed message ErrDisconnectMalformedMessage DisconnectReason = errors.New("Malformed message body") // ErrDisconnectUnknownMessage unknow message ErrDisconnectUnknownMessage DisconnectReason = errors.New("Unknown message ID") // ErrDisconnectWriteQueueFull write queue is full ErrDisconnectWriteQueueFull DisconnectReason = errors.New("Write queue full") // ErrDisconnectUnexpectedError unexpected error ErrDisconnectUnexpectedError DisconnectReason = errors.New("Unexpected error encountered") // ErrConnectionPoolClosed error message indicates the connection pool is closed ErrConnectionPoolClosed = errors.New("Connection pool is closed") )
type Message ¶
type Message interface {
// State is user-defined application state that is attached to the
// Dispatcher.
// Return a non-nil error from handle only if you've disconnected the
// client. You don't have to return the DisconnectReason but that may
// be the most convenient. If error is not nil, event buffer processing
// is aborted.
Handle(context *MessageContext, state interface{}) error
}
Message message interface
type MessageContext ¶
type MessageContext struct {
// Conn *Connection // connection message was received from
ConnID int // connection message was received from
Addr string
}
MessageContext message context
func NewMessageContext ¶
func NewMessageContext(conn *Connection) *MessageContext
NewMessageContext creates MessageContext
type MessagePrefix ¶
type MessagePrefix [messagePrefixLength]byte
MessagePrefix message prefix identifies a message
func MessagePrefixFromString ¶
func MessagePrefixFromString(prefix string) MessagePrefix
MessagePrefixFromString creates MessagePrefix from string
type SendResult ¶
SendResult result of a single message send
