transport

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2018 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package transport provides network transport interface. It allows to abstract our network from physical transport. It can either be IP based network or any other kind of packet courier (e.g. an industrial packet bus).

Package exports simple interfaces for easily defining new transports.

For now we provide two implementations of transport. The default is UTPTransport which using BitTorrent µTP protocol. The second one is KCPTransport based on KCP protocol and supports packet level encryption.

Usage:

var conn net.PacketConn
// get udp connection anywhere

tp, _ := transport.NewUTPTransport(conn)
msg := &packet.Packet{}

// Send the async queries and wait for a future
future, err := tp.SendRequest(msg)
if err != nil {
	panic(err)
}

select {
case response := <-future.Result():
	// Channel was closed
	if response == nil {
		panic("chanel closed unexpectedly")
	}

	// do something with response

case <-time.After(1 * time.Second):
	future.Cancel()
}

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTimeout is returned when the operation timeout is exceeded.
	ErrTimeout = errors.New("timeout")
	// ErrChannelClosed is returned when the input channel is closed.
	ErrChannelClosed = errors.New("channel closed")
)

Functions

func AtomicLoadAndIncrementUint64

func AtomicLoadAndIncrementUint64(addr *uint64) uint64

AtomicLoadAndIncrementUint64 performs CAS loop, increments counter and returns old value.

Types

type CancelCallback

type CancelCallback func(Future)

CancelCallback is a callback function executed when cancelling Future.

type Future

type Future interface {

	// ID returns packet sequence number.
	ID() packet.RequestID

	// Actor returns the initiator of the packet.
	Actor() *host.Host

	// Request returns origin request.
	Request() *packet.Packet

	// Result is a channel to listen for future result.
	Result() <-chan *packet.Packet

	// SetResult makes packet to appear in result channel.
	SetResult(*packet.Packet)

	// GetResult gets the future result from Result() channel with a timeout set to `duration`.
	GetResult(duration time.Duration) (*packet.Packet, error)

	// Cancel closes all channels and cleans up underlying structures.
	Cancel()
}

Future is network response future.

func NewFuture

func NewFuture(requestID packet.RequestID, actor *host.Host, msg *packet.Packet, cancelCallback CancelCallback) Future

NewFuture creates new Future.

type Transport

type Transport interface {
	// SendRequest sends packet to destination. Sequence number is generated automatically.
	SendRequest(*packet.Packet) (Future, error)

	// SendResponse sends packet for request with passed request id.
	SendResponse(packet.RequestID, *packet.Packet) error

	// Start starts thread to listen incoming packets.
	Start() error

	// Stop gracefully stops listening.
	Stop()

	// Close disposing all transport underlying structures after stop are called.
	Close()

	// Packets returns channel to listen incoming packets.
	Packets() <-chan *packet.Packet

	// Stopped returns signal channel to support graceful shutdown.
	Stopped() <-chan bool

	// PublicAddress returns PublicAddress
	PublicAddress() string
}

Transport is an interface for network transport.

func NewTransport added in v0.0.5

func NewTransport(cfg configuration.Transport, proxy relay.Proxy) (Transport, error)

NewTransport creates new Transport with particular configuration

Jump to

Keyboard shortcuts

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