transport

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2018 License: Apache-2.0 Imports: 13 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 message courier (e.g. an industrial message 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 := &message.Message{}

// 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

This section is empty.

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 Factory

type Factory interface {
	Create(conn net.PacketConn, proxy relay.Proxy) (Transport, error)
}

Factory allows to create new Transport.

func NewKCPTransportFactory added in v0.0.3

func NewKCPTransportFactory() Factory

NewKCPTransportFactory creates new Factory of kcpTransport.

func NewUTPTransportFactory

func NewUTPTransportFactory() Factory

NewUTPTransportFactory creates new Factory of utpTransport.

type Future

type Future interface {

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

	// Actor returns the initiator of the message.
	Actor() *node.Node

	// Request returns origin request.
	Request() *message.Message

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

	// SetResult makes message to appear in result channel.
	SetResult(*message.Message)

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

Future is network response future.

func NewFuture

func NewFuture(requestID message.RequestID, actor *node.Node, msg *message.Message, cancelCallback CancelCallback) Future

NewFuture creates new Future.

type Transport

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

	// SendResponse sends message for request with passed request id.
	SendResponse(message.RequestID, *message.Message) error

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

	// Stop gracefully stops listening.
	Stop()

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

	// Messages returns channel to listen incoming messages.
	Messages() <-chan *message.Message

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

Transport is an interface for network transport.

func NewKCPTransport added in v0.0.3

func NewKCPTransport(conn net.PacketConn, proxy relay.Proxy) (Transport, error)

NewKCPTransport creates kcpTransport.

func NewUTPTransport

func NewUTPTransport(conn net.PacketConn, proxy relay.Proxy) (Transport, error)

NewUTPTransport creates utpTransport.

Jump to

Keyboard shortcuts

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