tunnel

package
v0.0.0-...-8c930d8 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

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

View Source
var (
	Ping = NewCmd("ping", 1)
	Pong = NewCmd("pong", 2)
)

Functions

func DecodePayload

func DecodePayload[T any](payload any, v *T) error

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

func List

func List() map[any]any

List returns the registered connections by their keys, see NewSession.

Types

type AppSide

type AppSide string

AppSide tells NewSession which end of a connection the caller is.

const (
	Server AppSide = "server"
	Client AppSide = "client"
)

type Cmd

type Cmd uint32

func NewCmd

func NewCmd(name string, value uint32) Cmd

NewCmd build a Cmd. custom always greater than 1000

func (Cmd) String

func (c Cmd) String() string

type Event

type Event struct {
	ID    string `json:"id,omitempty" msgpack:"id,omitempty"`   // event id
	Cmd   Cmd    `json:"cmd,omitempty" msgpack:"cmd,omitempty"` // event cmd
	Error string `json:"error,omitempty" msgpack:"error,omitempty"`

	Payload any `json:"payload,omitempty" msgpack:"payload,omitempty"`
}

type Session

type Session struct {
	// contains filtered or unexported fields
}

func NewSession

func NewSession(conn *net.TCPConn, appSide AppSide, cid ...string) (*Session, error)

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

func (s *Session) Close() error

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

func (s *Session) Read() (*Event, error)

Read reads and decodes an Event from the connection. The binary protocol format is:

  1. First 4 bytes: data size in BigEndian uint32
  2. 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

func (s *Session) Write(event *Event) error

Write encodes and writes an Event to the connection. The binary protocol format is:

  1. First 4 bytes: data size in BigEndian uint32
  2. 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

Jump to

Keyboard shortcuts

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