Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NopConn ¶
func NopConn(rw io.ReadWriter) net.Conn
NopConn returns a net.Conn with a no-op LocalAddr, RemoteAddr, SetDeadline, SetWriteDeadline, SetReadDeadline, and Close methods wrapping the provided ReadWriter rw.
func Serve ¶
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 Events ¶
type Events struct {
// Serving fires when the server can accept connections. The server
// parameter has information and various utilities.
Serving func(server Server) (action Action)
// Opened 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.
Opened func(id int, info Info) (out []byte, opts Options, action Action)
// Closed fires when a connection has closed.
// The err parameter is the last known connection error.
Closed func(id int, err error) (action Action)
// Detached fires when a connection has been previously detached.
// Once detached it's up to the receiver of this event to manage the
// state of the connection. The Closed event will not be called for
// this connection.
// The conn parameter is a ReadWriteCloser that represents the
// underlying socket connection. It can be freely used in goroutines
// and should be closed when it's no longer needed.
Detached func(id int, rwc io.ReadWriteCloser) (action Action)
// Data 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.
Data func(id int, in []byte) (out []byte, action Action)
// Prewrite fires prior to every write attempt.
// The amount parameter is the number of bytes that will be attempted
// to be written to the connection.
Prewrite func(id int, amount int) (action Action)
// Postwrite fires immediately after every write attempt.
// The amount parameter is the number of bytes that was written to the
// connection.
// The remaining parameter is the number of bytes that still remain in
// the buffer scheduled to be written.
Postwrite func(id int, amount, remaining int) (action Action)
// Tick fires immediately after the server starts and will fire again
// following the duration specified by the delay return value.
Tick func() (delay time.Duration, action Action)
}
Events represents the server events for the Serve call. Each event has an Action return value that is used manage the state of the connection and server.
func Translate ¶
func Translate( events Events, should func(id int, info Info) bool, translate func(id int, rd io.ReadWriter) io.ReadWriter, ) Events
Translate provides a utility for performing byte level translation on the input and output streams for a connection. This is useful for things like compression, encryption, TLS, etc. The function wraps existing events and returns new events that manage the translation. The `should` parameter is an optional function that can be used to ignore or accept the translation for a specific connection. The `translate` parameter is a function that provides a ReadWriter for each new connection and returns a ReadWriter that performs the actual translation.
type Info ¶
type Info struct {
// Closing is true when the connection is about to close. Expect a Closed
// event to fire soon.
Closing bool
// AddrIndex is the index of server address that was passed to the Serve call.
AddrIndex int
// LocalAddr is the connection's local socket address.
LocalAddr net.Addr
// RemoteAddr is the connection's remote peer address.
RemoteAddr net.Addr
}
Info represents a information about the connection
type InputStream ¶
type InputStream struct {
// contains filtered or unexported fields
}
InputStream is a helper type for managing input streams inside the Data event.
func (*InputStream) Begin ¶
func (is *InputStream) Begin(packet []byte) (data []byte)
Begin accepts a new packet and returns a working sequence of unprocessed bytes.
func (*InputStream) End ¶
func (is *InputStream) End(data []byte)
End shift the stream to match the unprocessed data.
type Options ¶
type Options struct {
// TCPKeepAlive (SO_KEEPALIVE) socket option.
TCPKeepAlive time.Duration
// ReuseInputBuffer will forces the connection to share and reuse the
// same input packet buffer with all other connections that also use
// this option.
// Default value is false, which means that all input data which is
// passed to the Data event will be a uniquely copied []byte slice.
ReuseInputBuffer bool
}
Options are set when the client opens.
type Server ¶
type Server struct {
// The addrs parameter is an array of listening addresses that align
// with the addr strings passed to the Serve function.
Addrs []net.Addr
// Wake is a goroutine-safe function that triggers a Data event
// (with a nil `in` parameter) for the specified id. Not available for
// UDP connections.
Wake func(id int) (ok bool)
// Dial is a goroutine-safe function makes a connection to an external
// server and returns a new connection id. The new connection is added
// to the event loop and is managed exactly the same way as all the
// other connections. This operation only fails if the server/loop has
// been shut down. An `id` that is not zero means the operation succeeded
// and then there always be exactly one Opened and one Closed event
// following this call. Look for socket errors from the Closed event.
// Not available for UDP connections.
Dial func(addr string, timeout time.Duration) (id int)
}
Server represents a server context which provides information about the running server and has control functions for managing state.
Directories
¶
| Path | Synopsis |
|---|---|
|
fasthttp-server
command
|
|
|
net-echo-server
command
|
|
|
net-http-server
command
|
|
|
examples
|
|
|
echo-server
command
|
|
|
http-server
command
|
|
|
redis-server
command
|
|



