Documentation
¶
Index ¶
- Variables
- func Serve(eventHandler EventHandler, addr string, opts ...Option) error
- type Action
- type Conn
- type EventHandler
- type EventServer
- func (es *EventServer) OnClosed(c Conn, err error) (action Action)
- func (es *EventServer) OnInitComplete(svr Server) (action Action)
- func (es *EventServer) OnOpened(c Conn) (out []byte, action Action)
- func (es *EventServer) PreWrite()
- func (es *EventServer) React(c Conn) (out []byte, action Action)
- func (es *EventServer) Tick() (delay time.Duration, action Action)
- type IEventLoopGroup
- type Option
- type Options
- type Server
Constants ¶
This section is empty.
Variables ¶
var ( // ErrClosing indicates this server is closing. ErrClosing = errors.New("closing event-loop") )
Functions ¶
func Serve ¶
func Serve(eventHandler EventHandler, addr string, opts ...Option) error
Serve starts handling events for the specified addresses.
Addresses should use a scheme prefix and be formatted like `tcp://192.168.0.10:9851` or `unix://socket`. Valid network schemes:
tcp - bind to both IPv4 and IPv6 tcp4 - IPv4 tcp6 - IPv6 udp - bind to both IPv4 and IPv6 udp4 - IPv4 udp6 - IPv6 unix - Unix Domain Socket
The "tcp" network scheme is assumed when one is not specified.
Types ¶
type Conn ¶
type Conn interface {
// Context returns a user-defined context.
Context() interface{}
// SetContext sets a user-defined context.
SetContext(interface{})
// LocalAddr is the connection's local socket address.
LocalAddr() net.Addr
// RemoteAddr is the connection's remote peer address.
RemoteAddr() net.Addr
// ReadPair reads all data from ring buffer.
ReadPair() ([]byte, []byte)
// ResetBuffer resets the ring buffer.
ResetBuffer()
// ReadN reads bytes with the given length from ring buffer.
ReadN(int) (int, []byte, []byte)
// ShiftN shifts the "read" pointer in ring buffer with the given length.
ShiftN(int)
// ReadBytes reads all data and return a new slice.
ReadBytes() []byte
// AyncWrite writes data asynchronously.
AsyncWrite([]byte)
}
Conn is an gnet connection.
type EventHandler ¶ added in v1.0.0
type EventHandler interface {
// OnInitComplete fires when the server can accept connections. The server
// parameter has information and various utilities.
OnInitComplete(server Server) (action Action)
// OnOpened fires when a new connection has opened.
// The info parameter has information about the connection such as
// it's local and remote address.
// Use the out return value to write data to the connection.
// The opts return value is used to set connection options.
OnOpened(c Conn) (out []byte, action Action)
// OnClosed fires when a connection has closed.
// The err parameter is the last known connection error.
OnClosed(c Conn, err error) (action Action)
// PreWrite fires just before any data is written to any client socket.
PreWrite()
// React fires when a connection sends the server data.
// The in parameter is the incoming data.
// Use the out return value to write data to the connection.
React(c Conn) (out []byte, action Action)
// Tick fires immediately after the server starts and will fire again
// following the duration specified by the delay return value.
Tick() (delay time.Duration, action Action)
}
EventHandler represents the server events' callbacks for the Serve call. Each event has an Action return value that is used manage the state of the connection and server.
type EventServer ¶ added in v1.0.0
type EventServer struct {
}
EventServer is a built-in implementation of EventHandler which sets up each method with a default implementation, you can compose it with your own implementation of EventHandler when you don't want to implement all methods in EventHandler.
func (*EventServer) OnClosed ¶ added in v1.0.0
func (es *EventServer) OnClosed(c Conn, err error) (action Action)
OnClosed fires when a connection has closed. The err parameter is the last known connection error.
func (*EventServer) OnInitComplete ¶ added in v1.0.0
func (es *EventServer) OnInitComplete(svr Server) (action Action)
OnInitComplete fires when the server can accept connections. The server parameter has information and various utilities.
func (*EventServer) OnOpened ¶ added in v1.0.0
func (es *EventServer) OnOpened(c Conn) (out []byte, action Action)
OnOpened fires when a new connection has opened. The info parameter has information about the connection such as it's local and remote address. Use the out return value to write data to the connection. The opts return value is used to set connection options.
func (*EventServer) PreWrite ¶ added in v1.0.0
func (es *EventServer) PreWrite()
PreWrite fires just before any data is written to any client socket.
type IEventLoopGroup ¶ added in v1.0.0
type IEventLoopGroup interface {
// contains filtered or unexported methods
}
IEventLoopGroup represents a set of event-loops.
type Option ¶ added in v1.0.0
type Option func(opts *Options)
Option is a function that will set up option.
func WithOptions ¶ added in v1.0.0
WithOptions sets up all options.
func WithTCPKeepAlive ¶ added in v1.0.0
WithTCPKeepAlive ...
type Options ¶
type Options struct {
// Multicore indicates whether the server will be effectively created with multi-cores, if so,
// then you must take care with synchonizing memory between all event callbacks, otherwise,
// it will run the server with single thread. The number of threads in the server will be automatically
// assigned to the value of runtime.NumCPU().
Multicore bool
//ReusePort indicates whether to set up the SO_REUSEPORT option.
ReusePort bool
Ticker bool
TCPKeepAlive time.Duration
}
Options are set when the client opens.
type Server ¶
type Server struct {
// Multicore indicates whether the server will be effectively created with multi-cores, if so,
// then you must take care with synchonizing memory between all event callbacks, otherwise,
// it will run the server with single thread. The number of threads in the server will be automatically
// assigned to the value of runtime.NumCPU().
Multicore bool
// The addrs parameter is an array of listening addresses that align
// with the addr strings passed to the Serve function.
Addr net.Addr
// NumLoops is the number of loops that the server is using.
NumLoops int
}
Server represents a server context which provides information about the running server and has control functions for managing state.



