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.
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 ¶
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 ListenAndWaitUntilReady ¶ added in v0.7.5
func NewConnection ¶
func NewConnection(cfg configuration.Transport) (net.PacketConn, string, error)
NewConnection creates new Connection from configuration and returns connection and public address
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() network.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.
type Transport ¶
type Transport interface { // SendRequest sends packet to destination. Sequence number is generated automatically. SendRequest(context.Context, *packet.Packet) (Future, error) // SendResponse sends response packet for request with passed request id. SendResponse(context.Context, network.RequestID, *packet.Packet) error // SendPacket low-level send packet without requestId and without spawning a waiting future SendPacket(ctx context.Context, p *packet.Packet) error // Listen starts thread to listen incoming packets. Listen(ctx context.Context, started chan struct{}) error // Stop gracefully stops listening. Stop() // Close disposing all transport underlying structures after stopped 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 ¶
NewTransport creates new Transport with particular configuration
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package connection encapsulates connection creation process and provides connection factories.
|
Package connection encapsulates connection creation process and provides connection factories. |
Package host is a fundamental part of networking system.
|
Package host is a fundamental part of networking system. |
Package packet provides network messaging protocol and serialization layer.
|
Package packet provides network messaging protocol and serialization layer. |
Package relay is an implementation of relay mechanism.
|
Package relay is an implementation of relay mechanism. |
Package resolver provides interface (and default implementation) to retrieve public network address.
|
Package resolver provides interface (and default implementation) to retrieve public network address. |