tunnel

package
v0.10.14 Latest Latest
Warning

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

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

Documentation

Overview

Package tunnel is communication protocol between with server and user, server and client.

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 will list all tunnel session connection.

Types

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 any, appSide consts.AppSide, cid ...string) (*Session, error)

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

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

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

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
  • 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
⌂ 换网址