Documentation
¶
Overview ¶
Package tunnel is the communication protocol between a server and its users, and between a server and its clients.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( Ping = NewCmd("ping", 1) Pong = NewCmd("pong", 2) )
Functions ¶
func DecodePayload ¶
DecodePayload decodes the payload into the provided value v of type T. It handles three cases: 1. If the payload is nil, v remains unchanged 2. If the payload is already type T, assigns directly to v 3. Otherwise, uses msgpack marshal/unmarshal to decode into v
Types ¶
type AppSide ¶
type AppSide string
AppSide tells NewSession which end of a connection the caller is.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
func NewSession ¶
NewSession creates a new tunnel session from a connection.
Parameters:
- conn: The connection to create session from, which must not be nil
- appSide: Indicates whether this is server or client side (Server/Client)
- cid: Optional client ID, only used on server side. If provided, will be used as the connection key
The connection key is the client ID on the server side when one is given, and otherwise the connection's local and remote addresses, such as "127.0.0.1:9000->127.0.0.1:52144": the pair names one end of one connection on the host, where the remote address alone does not, since every connection a client opens to one server has the same remote address, and two listeners may see the same remote address too.
Every session on one connection shares the connection's registration, and with it the locks that keep reads and writes whole. A key another connection holds is taken over: the newer connection replaces the older one, which is closed, the way MQTT treats a client ID that connects again; a client often reconnects before the server has noticed its older connection is gone.
Returns:
- *Session and nil if successful
- nil and error if conn is nil or appSide is neither Server nor Client
func (*Session) Close ¶
Close closes the session's connection, which ends every session on it, and takes the connection out of the registration List reads, unless another connection has taken its key over since.
func (*Session) Read ¶
Read reads and decodes an Event from the connection. The binary protocol format is:
- First 4 bytes: data size in BigEndian uint32
- Remaining bytes: msgpack encoded Event data
Concurrent reads take whole frames in turn.
Returns an error if:
- Failed to read the data size
- Data size is 0 or over 4 MiB, which no Write produces; the session is closed as well, since the peer is out of step with the protocol and, past an oversize header, the stream holds no header to read next
- Failed to read the event data
- Failed to unmarshal the data into Event
func (*Session) Write ¶
Write encodes and writes an Event to the connection. The binary protocol format is:
- First 4 bytes: data size in BigEndian uint32
- Remaining bytes: msgpack encoded Event data
If the Event.ID is empty, it will be set to a new UUID. The write operation is synchronized using a mutex to ensure atomic writes. Returns an error if:
- Failed to marshal the event
- The encoded event is over 4 MiB, in which case nothing is written
- Failed to write the data size
- Failed to write the event data