network

package
v2.6.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 2 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attr added in v2.3.0

type Attr interface {
	// Set sets the value of the attribute with the given key.
	Set(key, value any)
	// Get returns the value of the attribute with the given key and whether it exists.
	Get(key any) (any, bool)
	// Del deletes the attribute with the given key and reports whether the deletion succeeded.
	Del(key any) bool
	// Clear removes all attributes.
	Clear()
	// Visit calls fn for every attribute and stops when fn returns false.
	Visit(fn func(key, value any) bool)
}

Attr is the attribute interface of a connection.

type Client

type Client interface {
	// Dial dials a connection.
	//
	// When addr is empty the address from the client options is used. The connect handler runs on
	// the connection read goroutine before the first frame is read, so it may run concurrently with
	// or after Dial returns. A transport that reports a connection through the connect handler and
	// then fails to complete Dial must deliver the disconnect handler for that connection so
	// consumers can release it.
	Dial(addr ...string) (Conn, error)
	// Protocol returns the protocol name.
	Protocol() string
	// OnConnect registers the handler invoked when a connection is opened.
	OnConnect(handler ConnectHandler)
	// OnHeartbeat registers the handler invoked when a heartbeat is received.
	OnHeartbeat(handler HeartbeatHandler)
	// OnReceive registers the handler invoked when a message is received.
	OnReceive(handler ReceiveHandler)
	// OnHandshake registers the handler invoked when a handshake frame is received.
	OnHandshake(handler HandshakeHandler)
	// OnDisconnect registers the handler invoked when a connection is closed.
	OnDisconnect(handler DisconnectHandler)
}

Client is the client interface.

type CloseHandler

type CloseHandler func()

CloseHandler is the handler invoked when the server stops.

type Conn

type Conn interface {
	// ID returns the connection ID.
	ID() int64
	// UID returns the user ID.
	UID() int64
	// Attr returns the attribute interface of the connection.
	Attr() Attr
	// Bind binds the connection to the given user ID.
	Bind(uid int64) error
	// Unbind unbinds the user ID from the connection.
	Unbind() error
	// Push sends a message with low priority.
	//
	// The caller controls when buf is released when the send fails.
	Push(buf buffer.Buffer) error
	// State returns the connection state.
	State() ConnState
	// Close closes the connection.
	//
	// When force is true the connection is closed forcibly.
	Close(force ...bool) error
	// CloseIf closes the connection forcibly when it is still the connection with the given ID.
	//
	// Server connection objects are recycled and reused, so a caller that may hold a stale
	// reference must use this method instead of Close to avoid closing a newer connection that
	// reuses the same object. It reports ErrConnectionClosed when the connection no longer
	// carries the given ID.
	CloseIf(id int64) error
	// LocalIP returns the local IP address.
	LocalIP() (string, error)
	// LocalAddr returns the local address.
	LocalAddr() (net.Addr, error)
	// RemoteIP returns the remote IP address.
	RemoteIP() (string, error)
	// RemoteAddr returns the remote address.
	RemoteAddr() (net.Addr, error)
}

Conn is the connection interface.

type ConnState

type ConnState int32

ConnState is the connection state.

const (
	ConnOpened ConnState = iota + 1 // Connection opened
	ConnHanged                      // Connection hanged
	ConnClosed                      // Connection closed
)

type ConnectHandler

type ConnectHandler func(conn Conn)

ConnectHandler is the handler invoked when a connection is opened.

The connect, heartbeat, receive and handshake handlers all run on the connection read goroutine, so closing the connection or stopping the server synchronously from any of them would deadlock; perform those operations from another goroutine. The connect handler runs before the first frame is read and, on the client side, may run concurrently with or after Dial returns.

type DisconnectHandler

type DisconnectHandler func(conn Conn)

DisconnectHandler is the handler invoked when a connection is closed.

It runs after the connection read goroutine has exited and after the connect handler has returned, so it never runs concurrently with the receive or connect handler of the same connection. Releasing per-connection state (sessions, buffers, rate limiter buckets) here is therefore safe. On the client side it may run before Dial returns.

type HandshakeHandler added in v2.6.3

type HandshakeHandler func(conn Conn, buf buffer.Buffer)

HandshakeHandler is the handler invoked when a handshake frame is received.

Handshake frames are marked by a reserved bit of the packet header instead of a route, and they are dispatched before the receive handler. It runs on the connection read goroutine; see ConnectHandler for the synchronous-close caveat. The user layer controls when buf is released to avoid a memory leak.

type HeartbeatHandler added in v2.6.0

type HeartbeatHandler func(conn Conn, heartbeatTime int64)

HeartbeatHandler is the handler invoked when a heartbeat is received.

It runs on the connection read goroutine; see ConnectHandler for the synchronous-close caveat. heartbeatTime is the time in nanoseconds at which the client received the heartbeat sent by the server.

type ReceiveHandler

type ReceiveHandler func(conn Conn, buf buffer.Buffer)

ReceiveHandler is the handler invoked when a message is received.

It runs on the connection read goroutine; see ConnectHandler for the synchronous-close caveat. The user layer controls when buf is released to avoid a memory leak.

type Server

type Server interface {
	// Addr returns the listen address.
	Addr() string
	// Start starts the server.
	Start() error
	// Stop stops the server.
	Stop() error
	// Protocol returns the protocol name.
	Protocol() string
	// OnStart registers the handler invoked when the server starts.
	OnStart(handler StartHandler)
	// OnStop registers the handler invoked when the server stops.
	OnStop(handler CloseHandler)
	// OnConnect registers the handler invoked when a connection is opened.
	OnConnect(handler ConnectHandler)
	// OnHeartbeat registers the handler invoked when a heartbeat is received.
	OnHeartbeat(handler HeartbeatHandler)
	// OnReceive registers the handler invoked when a message is received.
	OnReceive(handler ReceiveHandler)
	// OnHandshake registers the handler invoked when a handshake frame is received.
	OnHandshake(handler HandshakeHandler)
	// OnDisconnect registers the handler invoked when a connection is closed.
	OnDisconnect(handler DisconnectHandler)
}

Server is the server interface.

type StartHandler

type StartHandler func()

StartHandler is the handler invoked when the server starts.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL