Documentation
¶
Overview ¶
Package tunnel is communication protocol between with server and user, server and client.
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 Session ¶
type Session struct {
// contains filtered or unexported fields
}
func NewSession ¶
NewSession creates a new tunnel session from a connection. Currently only supports TCP connections (*net.TCPConn).
Parameters:
- _conn: The connection to create session from, must be *net.TCPConn
- appSide: Indicates whether this is server or client side (consts.Server/consts.Client)
- cid: Optional client ID, only used on server side. If provided, will be used as the connection key
The session tracking differs between server and client:
- Server side: Uses remote IP or client ID(if provided) as the connection key
- Client side: Uses local port as the connection key
Each connection is associated with a locker to ensure thread-safe reads/writes. If a connection with the same key already exists, the existing locker will be reused.
Returns:
- *Session and nil if successful
- nil and error if connection type not supported or other errors occur
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
Returns an error if:
- Failed to read the data size
- Data size is 0
- 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
- Failed to write the data size
- Failed to write the event data