transportc

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2022 License: MIT Imports: 19 Imported by: 3

README

TranspoRTC

A ready-to-go pluggable transport utilizing WebRTC datachannel written in Go.

Design

Config

A Config defines the behavior of the transport. A Config could be used to configure:

  • Automatic signalling when establishing the PeerConnection
  • IP addresses to be used for ICE candidates
  • Port range for ICE candidates
  • UDP Mux for serving multiple connections over one UDP socket
Dialer

A Dialer is created from a Config and is used to dial one or more Conn backed by WebRTC DataChannel.

On its first call to Dial, the Dialer will create a new PeerConnection and DataChannel. On subsequent calls, the Dialer will reuse the existing PeerConnection and DataChannel.

Listener

A Listener is created from a Config and is used to listen for incoming Conn backed by WebRTC DataChannel. It looks for incoming SDP offers to establish new PeerConnections and also looks for incoming DataChannels on existing PeerConnections.

One Listener can maintain multiple PeerConnections and on each PeerConnection multiple DataChannels may co-exist.

A Listener requires a valid SignalMethod to function.

Conn

A Conn is created from a Dialer and is used to send and receive messages. Each Conn is backed by a single WebRTC DataChannel.

Documentation

Index

Constants

View Source
const (
	MTU_DEFAULT              = 1024
	MAX_RECV_TIMEOUT_DEFAULT = time.Second * 10
)
View Source
const (
	CONN_DEFAULT_MTU         = 65536
	CONN_IDLE_TIMEOUT        = 30 * time.Second
	CONN_DEFAULT_CONCURRENCY = 4
)
View Source
const (
	DEFAULT_ACCEPT_TIMEOUT = 10 * time.Second
)

Variables

View Source
var (
	// ErrOfferNotReady is returned by ReadOffer when no offer is available.
	ErrOfferNotReady = errors.New("offer not ready")

	// ErrInvalidOfferID is returned by Answer/ReadAnswer when the offer ID is invalid.
	ErrInvalidOfferID = errors.New("invalid offer ID")

	// ErrAnswerNotReady is returned by ReadAnswer when the offerID is valid but
	// an associated answer is not received yet.
	ErrAnswerNotReady = errors.New("answer not ready")
)
View Source
var (
	ErrBrokenDialer = errors.New("dialer need to be recreated")
)

Functions

This section is empty.

Types

type Addr added in v0.3.2

type Addr struct {
	Hostname string
	Port     uint16
}

func (*Addr) Network added in v0.3.2

func (*Addr) Network() string

func (*Addr) String added in v0.3.2

func (a *Addr) String() string

type Config

type Config struct {
	// CandidateNetworkTypes restricts ICE agent to gather
	// on only selected types of networks.
	CandidateNetworkTypes []webrtc.NetworkType

	// InterfaceFilter restricts ICE agent to gather ICE candidates
	// on only selected interfaces.
	InterfaceFilter func(interfaceName string) (allowed bool)

	// IPs includes a slice of IP addresses and one single ICE Candidate Type.
	// If set, will add these IPs as ICE Candidates
	IPs *NAT1To1IPs

	// ListenerDTLSRole defines the DTLS role when Listening.
	// MUST be either DTLSRoleClient or DTLSRoleServer, as defined in RFC4347
	// DTLSRoleClient will send the ClientHello and start the handshake.
	// DTLSRoleServer will wait for the ClientHello.
	ListenerDTLSRole DTLSRole

	Logger logging.Logger

	// PortRange is the range of ports to use for the DataChannel.
	PortRange *PortRange

	// ReusePeerConnection indicates whether to reuse the same PeerConnection
	// if possible, when Dialer dials multiple times.
	//
	// If set to true, Dialer.Dial() creates a new DataChannel on the same PeerConnection.
	// Otherwise, Dialer.Dial() negotiates for a new PeerConnection and creates a new DataChannel on it.
	ReusePeerConnection bool

	// Signal offers the automatic signaling when establishing the DataChannel.
	Signal Signal

	Timeout time.Duration

	// UDPMux allows serving multiple DataChannels over the one or more pre-established UDP socket.
	UDPMux ice.UDPMux

	// WebRTCConfiguration is the configuration for the underlying WebRTC PeerConnection.
	WebRTCConfiguration webrtc.Configuration
}

Config is the configuration for the Dialer and Listener.

func (*Config) BuildSettingEngine

func (c *Config) BuildSettingEngine() (webrtc.SettingEngine, error)

BuildSettingEngine builds a SettingEngine from the configuration.

func (*Config) NewDialer

func (c *Config) NewDialer() (*Dialer, error)

NewDialer creates a new Dialer from the given configuration.

func (*Config) NewListener

func (c *Config) NewListener() (*Listener, error)

NewListener creates a new Listener from the given configuration.

type Conn

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

Conn defines a connection based on a dedicated datachannel. Conn interfaces net.Conn.

func NewConn added in v0.3.2

func NewConn(dataChannel io.ReadWriteCloser, maxConcurrency int) *Conn

BuildConningle builds a Conningle from an existing datachannel.

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

LocalAddr returns the address of Local ICE Candidate selected for the datachannel

func (*Conn) Read

func (c *Conn) Read(p []byte) (n int, err error)

Read reads data from the connection (underlying datachannel). It blocks until read deadline is reached, data is received in read buffer or error occurs.

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr returns the address of Remote ICE Candidate selected for the datachannel

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

SetDeadline sets the deadline for future Read and Write calls.

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the deadline for future Read calls.

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the deadline for future Write calls.

func (*Conn) Write

func (c *Conn) Write(p []byte) (n int, err error)

Write writes data to the connection (underlying datachannel). It blocks until write deadline is reached, data is accepted by write buffer or error occurs.

type DTLSRole

type DTLSRole = webrtc.DTLSRole

RFC 4347

const (
	// DTLSRoleAuto defines the DTLS role is determined based on
	// the resolved ICE role: the ICE controlled role acts as the DTLS
	// client and the ICE controlling role acts as the DTLS server.
	DTLSRoleAuto DTLSRole = iota + 1

	// DTLSRoleClient defines the DTLS client role.
	DTLSRoleClient

	// DTLSRoleServer defines the DTLS server role.
	DTLSRoleServer
)

From pion/webrtc

type DebugSignal

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

DebugSignal implements a minimalistic signaling method used for debugging purposes.

func NewDebugSignal

func NewDebugSignal(bufferSize int) *DebugSignal

NewDebugSignal creates a new DebugSignal.

func (*DebugSignal) Answer

func (ds *DebugSignal) Answer(offerID uint64, answer []byte) error

Answer implements Signal.Answer. It writes the SDP answer to answers channel.

func (*DebugSignal) Offer added in v0.3.2

func (ds *DebugSignal) Offer(offerBody []byte) (uint64, error)

Offer implements Signal.Offer. It writes the SDP offer to offers channel.

func (*DebugSignal) ReadAnswer added in v0.3.2

func (ds *DebugSignal) ReadAnswer(offerID uint64) ([]byte, error)

ReadAnswer implements Signal.ReadAnswer It reads the SDP answer from answers channel.

func (*DebugSignal) ReadOffer added in v0.3.2

func (ds *DebugSignal) ReadOffer() (uint64, []byte, error)

ReadOffer implements Signal.ReadOffer It reads the SDP offer from offers channel.

type Dialer

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

Dialer can dial a remote peer.

If the SignalMethod is set, the Offer/Answer exchange per new PeerConnection will be done automatically.

func (*Dialer) Close

func (d *Dialer) Close() error

Close closes the WebRTC PeerConnection and with it all the WebRTC DataChannels under it.

SHOULD be called when done using the transport.

func (*Dialer) Dial

func (d *Dialer) Dial(label string) (net.Conn, error)

Dial connects to a remote peer with SDP-based negotiation.

Internally calls DialContext with context.Background().

The returned connection is backed by a DataChannel created by the caller with the SDP role as OFFERER as defined in RFC3264. If SignalMethod is set, the Offer/Answer exchange per new PeerConnection will be done automatically. Otherwise, it is recommended to call NewPeerConnection and exchange the SDP offer/answer manually before dialing.

func (*Dialer) DialContext

func (d *Dialer) DialContext(ctx context.Context, label string) (net.Conn, error)

DialContext connects to a remote peer with SDP-based negotiation using the provided context.

The provided Context must be non-nil. If the context expires before the connection is complete, an error is returned. Once successfully connected, any expiration of the context will not affect the connection.

The returned connection is backed by a DataChannel created by the caller with the SDP role as OFFERER as defined in RFC3264. If SignalMethod is set, the Offer/Answer exchange per new PeerConnection will be done automatically. Otherwise, it is recommended to call NewPeerConnection and exchange the SDP offer/answer manually before dialing.

func (*Dialer) SendOffer added in v0.3.2

func (d *Dialer) SendOffer(ctx context.Context) (uint64, error)

SendOffer creates a local offer and sets it as the local description, then signals the offer to the remote peer and return the offer ID.

Automatically called by startPeerConnection when Dialer.signal is set.

func (*Dialer) SetAnswer

func (d *Dialer) SetAnswer(ctx context.Context, offerID uint64) error

SetAnswer reads the answer from the signaler and sets it as the remote description.

Automatically called by startPeerConnection when Dialer.signal is set.

type Listener

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

Listener listens for new PeerConnections and saves all incoming datachannel from peers for later use.

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

Accept accepts a new connection from the listener.

It does not establish new connections. These connections are from the pool filled automatically by acceptLoop.

func (*Listener) Addr added in v0.3.2

func (*Listener) Addr() net.Addr

Addr is unimplemented

func (*Listener) Close added in v0.3.2

func (l *Listener) Close() error

Close closes the listener and all peer connections

func (*Listener) Start

func (l *Listener) Start() error

type ListenerRunningStatus

type ListenerRunningStatus = uint32
const (
	LISTENER_NEW ListenerRunningStatus = iota
	LISTENER_RUNNING
	LISTENER_SUSPENDED
	LISTENER_STOPPED
)

type NAT1To1IPs

type NAT1To1IPs struct {
	IPs  []string
	Type webrtc.ICECandidateType
}

NAT1To1IPs consists of a slice of IP addresses and one single ICE Candidate Type. Use this struct to set the IPs to be used as ICE Candidates.

type PortRange

type PortRange struct {
	Min uint16
	Max uint16
}

PortRange specifies the range of ports to use for ICE Transports.

type Signal added in v0.3.2

type Signal interface {
	// Offer submits a SDP offer generated by offerer to be read by the answerer.
	//
	// The caller is expected to keep the offerID for as a reference to the offer
	// when retrieving the answer later.
	Offer(offer []byte) (offerID uint64, err error)

	// ReadOffer reads the next SDP offer from the answerer.
	//
	// If no offer is available, ReadOffer may block until an offer is available
	// or return ErrOfferNotReady.
	ReadOffer() (offerID uint64, offer []byte, err error)

	// Answer submits a SDP answer generated by answerer to be read by the offerer.
	//
	// The caller is expected to provide the offerID returned by ReadOffer in order to
	// associate the answer with a previously submitted offer.
	Answer(offerID uint64, answer []byte) error

	// ReadAnswer reads the answer associated with the offerID.
	//
	// If an associated answer is not available, ReadAnswer may block until an answer
	// is available or return ErrAnswerNotReady.
	ReadAnswer(offerID uint64) ([]byte, error)
}

Signal defines the interface for signalling, i.e., exchanging SDP offers and answers between two peers.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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