Documentation
¶
Index ¶
- Constants
- Variables
- func SetLogger(logger ILogger)
- func SetPbUnserializeHook(hook FuncPbUnserializeHook)
- type ConnEvent
- type Connection
- func (c *Connection) ApplyReadDeadline()
- func (c *Connection) Close()
- func (c *Connection) Free()
- func (c *Connection) GetConn() net.Conn
- func (c *Connection) GetConnId() int
- func (c *Connection) GetLocalAddress() string
- func (c *Connection) GetReadTimeoutSec() int
- func (c *Connection) GetRemoteAddress() string
- func (c *Connection) GetStatus() int32
- func (c *Connection) GetUnpacker() IUnpacker
- func (c *Connection) GetUserdata() interface{}
- func (c *Connection) ResetReadDeadline()
- func (c *Connection) Send(msg []byte, f int64) error
- func (c *Connection) SendPb(pb proto.Message, f int64) error
- func (c *Connection) SetConnId(id int)
- func (c *Connection) SetReadTimeoutSec(sec int)
- func (c *Connection) SetSyncExecuteFunc(fn FuncSyncExecute) FuncSyncExecute
- func (c *Connection) SetUnpacker(unpacker IUnpacker)
- func (c *Connection) SetUserdata(ud interface{})
- type FuncPbUnserializeHook
- type FuncSyncExecute
- type IEventHandler
- type IEventQueue
- type ILogger
- type IStreamProtocol
- type IUnpacker
- type StreamProtocol2
- type StreamProtocol4
- type TCPNetwork
- func (t *TCPNetwork) Connect(addr string) (*Connection, error)
- func (t *TCPNetwork) DisconnectAllConnectionsClient()
- func (t *TCPNetwork) DisconnectAllConnectionsServer()
- func (t *TCPNetwork) GetEventQueue() <-chan *ConnEvent
- func (t *TCPNetwork) GetReadTimeoutSec() int
- func (t *TCPNetwork) GetStreamProtocol() IStreamProtocol
- func (t *TCPNetwork) Listen(addr string) error
- func (t *TCPNetwork) Pop() *ConnEvent
- func (t *TCPNetwork) Push(evt *ConnEvent)
- func (t *TCPNetwork) ServeWithHandler(handler IEventHandler)
- func (t *TCPNetwork) SetReadTimeoutSec(sec int)
- func (t *TCPNetwork) SetStreamProtocol(sp IStreamProtocol)
- func (t *TCPNetwork) Shutdown()
- type TCPNetworkConf
Constants ¶
const ( KConnEvent_None = iota KConnEvent_Connected KConnEvent_Disconnected KConnEvent_Data KConnEvent_Pb KConnEvent_Close KConnEvent_Total )
All connection event
const ( KConnFlag_CopySendBuffer = 1 << iota // Copy the send buffer KConnFlag_NoHeader // Do not append stream header )
Send method flag
const ( KLogLevelDebug = iota KLogLevelInfo KLogLevelWarn KLogLevelError KLogLevelFatal )
Variables ¶
var ( ErrConnIsClosed = errors.New("Connection is closed") ErrConnSendTimeout = errors.New("Connection send timeout") )
Functions ¶
func SetPbUnserializeHook ¶
func SetPbUnserializeHook(hook FuncPbUnserializeHook)
SetPbUnserializeHook set the global protobuf unserialize function
Types ¶
type ConnEvent ¶
type ConnEvent struct { EventType int Conn *Connection Data []byte Userdata interface{} PbM proto.Message }
ConnEvent represents a event occurs on a connection, such as connected, disconnected or data arrived
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection is a wrap for net.Conn and process read and write task of the conn When event occurs, it will call the eventQueue to dispatch event
func (*Connection) ApplyReadDeadline ¶
func (c *Connection) ApplyReadDeadline()
ApplyReadDeadline set the read deadline seconds
func (*Connection) Close ¶
func (c *Connection) Close()
Close the connection, routine safe, send task in the queue will be sent before closing the connection
func (*Connection) Free ¶
func (c *Connection) Free()
Free the connection. When don't need conection to send any thing, free it, DO NOT call it on multi routines
func (*Connection) GetConn ¶
func (c *Connection) GetConn() net.Conn
GetConn get the raw net.Conn interface
func (*Connection) GetConnId ¶
func (c *Connection) GetConnId() int
GetConnId get the connection's id
func (*Connection) GetLocalAddress ¶
func (c *Connection) GetLocalAddress() string
GetLocalAddress return the local address of the connection
func (*Connection) GetReadTimeoutSec ¶
func (c *Connection) GetReadTimeoutSec() int
GetReadTimeoutSec get the read deadline for the connection
func (*Connection) GetRemoteAddress ¶
func (c *Connection) GetRemoteAddress() string
GetRemoteAddress return the remote address of the connection
func (*Connection) GetStatus ¶
func (c *Connection) GetStatus() int32
GetStatus get the connection's status
func (*Connection) GetUnpacker ¶
func (c *Connection) GetUnpacker() IUnpacker
GetUnpacker you can get the unpacker you set
func (*Connection) GetUserdata ¶
func (c *Connection) GetUserdata() interface{}
GetUserdata get the userdata you set
func (*Connection) ResetReadDeadline ¶
func (c *Connection) ResetReadDeadline()
ResetReadDeadline reset the read deadline
func (*Connection) Send ¶
func (c *Connection) Send(msg []byte, f int64) error
Send the buffer with KConnFlag flag
func (*Connection) SendPb ¶
func (c *Connection) SendPb(pb proto.Message, f int64) error
SendPb send protocol buffer message
func (*Connection) SetConnId ¶
func (c *Connection) SetConnId(id int)
SetConnId set the connection's id
func (*Connection) SetReadTimeoutSec ¶
func (c *Connection) SetReadTimeoutSec(sec int)
SetReadTimeoutSec set the read deadline for the connection
func (*Connection) SetSyncExecuteFunc ¶
func (c *Connection) SetSyncExecuteFunc(fn FuncSyncExecute) FuncSyncExecute
SetSyncExecuteFunc , you can set a callback that you can synchoronously process the event in every connection's event routine If the callback function return true, the event will not be dispatched
func (*Connection) SetUnpacker ¶
func (c *Connection) SetUnpacker(unpacker IUnpacker)
SetUnpacker you can set a custom binary stream unpacker on the connection
func (*Connection) SetUserdata ¶
func (c *Connection) SetUserdata(ud interface{})
SetUserdata set the userdata you need
type FuncPbUnserializeHook ¶
FuncPbUnserializeHook is a function to unserialize binary data to protobuf message
type FuncSyncExecute ¶
Sync event callback If return true, this event will not be sent to event channel If return false, this event will be sent to event channel again
type IEventHandler ¶
type IEventHandler interface { OnConnected(evt *ConnEvent) OnDisconnected(evt *ConnEvent) OnRecv(evt *ConnEvent) }
IEventHandler is callback interface to process connection's event
type IEventQueue ¶
IEventQueue queues all connection's events
type IStreamProtocol ¶
type IStreamProtocol interface { // Init Init() // Get the header length of the stream GetHeaderLength() uint32 // Read the header length of the stream UnserializeHeader([]byte) uint32 // Format header SerializeHeader([]byte) []byte }
IStreamProtocol implement the protocol of the binary data stream for unpacking packet
type IUnpacker ¶
type IUnpacker interface {
Unpack(*Connection, []byte) ([]byte, error)
}
IUnpacker unpack the binary stream to replace the internal unpack process
type StreamProtocol2 ¶
type StreamProtocol2 struct { }
StreamProtocol2 implement a simple binary stream protocol with 2 bytes header as packet length Binary format : | 2 byte (total stream length) | data ... (total stream length - 2) |
stream protocol interface for 2 bytes header
func NewStreamProtocol2 ¶
func NewStreamProtocol2() *StreamProtocol2
func (*StreamProtocol2) GetHeaderLength ¶
func (s *StreamProtocol2) GetHeaderLength() uint32
func (*StreamProtocol2) Init ¶
func (s *StreamProtocol2) Init()
func (*StreamProtocol2) SerializeHeader ¶
func (s *StreamProtocol2) SerializeHeader(body []byte) []byte
func (*StreamProtocol2) UnserializeHeader ¶
func (s *StreamProtocol2) UnserializeHeader(buf []byte) uint32
type StreamProtocol4 ¶
type StreamProtocol4 struct { }
StreamProtocol4 implement a simple binary stream protocol with 4 bytes header as packet length Binary format : | 4 byte (total stream length) | data ... (total stream length - 4) |
implement default stream protocol stream protocol interface for 4 bytes header
func NewStreamProtocol4 ¶
func NewStreamProtocol4() *StreamProtocol4
NewStreamProtocol4 creates a StreamProtocol4
func (*StreamProtocol4) GetHeaderLength ¶
func (s *StreamProtocol4) GetHeaderLength() uint32
GetHeaderLength return the header length
func (*StreamProtocol4) SerializeHeader ¶
func (s *StreamProtocol4) SerializeHeader(body []byte) []byte
func (*StreamProtocol4) UnserializeHeader ¶
func (s *StreamProtocol4) UnserializeHeader(buf []byte) uint32
type TCPNetwork ¶
type TCPNetwork struct { Conf TCPNetworkConf // contains filtered or unexported fields }
TCPNetwork manages all server and client connections
func NewTCPNetwork ¶
func NewTCPNetwork(eventQueueSize int, sp IStreamProtocol) *TCPNetwork
NewTCPNetwork creates a TCPNetwork object
func (*TCPNetwork) Connect ¶
func (t *TCPNetwork) Connect(addr string) (*Connection, error)
Connect the remote server
func (*TCPNetwork) DisconnectAllConnectionsClient ¶
func (t *TCPNetwork) DisconnectAllConnectionsClient()
DisconnectAllConnectionsClient disconnect all connections on client side
func (*TCPNetwork) DisconnectAllConnectionsServer ¶
func (t *TCPNetwork) DisconnectAllConnectionsServer()
DisconnectAllConnectionsServer disconnect all connections on server side
func (*TCPNetwork) GetEventQueue ¶
func (t *TCPNetwork) GetEventQueue() <-chan *ConnEvent
GetEventQueue get the event queue channel
func (*TCPNetwork) GetReadTimeoutSec ¶
func (t *TCPNetwork) GetReadTimeoutSec() int
GetReadTimeoutSec returns the read timeout seconds of current TCPNetwork
func (*TCPNetwork) GetStreamProtocol ¶
func (t *TCPNetwork) GetStreamProtocol() IStreamProtocol
GetStreamProtocol returns the stream protocol of current TCPNetwork
func (*TCPNetwork) Listen ¶
func (t *TCPNetwork) Listen(addr string) error
Listen an address to accept client connection
func (*TCPNetwork) Push ¶
func (t *TCPNetwork) Push(evt *ConnEvent)
Push implements the IEventQueue interface
func (*TCPNetwork) ServeWithHandler ¶
func (t *TCPNetwork) ServeWithHandler(handler IEventHandler)
ServeWithHandler process all events in the event queue and dispatch to the IEventHandler
func (*TCPNetwork) SetReadTimeoutSec ¶
func (t *TCPNetwork) SetReadTimeoutSec(sec int)
SetReadTimeoutSec sets the read timeout seconds of current TCPNetwork
func (*TCPNetwork) SetStreamProtocol ¶
func (t *TCPNetwork) SetStreamProtocol(sp IStreamProtocol)
SetStreamProtocol sets the stream protocol of current TCPNetwork
func (*TCPNetwork) Shutdown ¶
func (t *TCPNetwork) Shutdown()
Shutdown frees all connection and stop the listener
type TCPNetworkConf ¶
type TCPNetworkConf struct {
SendBufferSize int
}
TCPNetworkConf config the TCPNetwork