stream

package
v0.0.0-...-55eee13 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2018 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyRegistered = errors.New("client has already been registered with the server")

ErrAlreadyRegistered is returned when a client tries to register again with the same server

View Source
var ErrMessageIsNil = errors.New("message is nil")

ErrMessageIsNil is returned when the message contains nil fields.

View Source
var ErrMultiAddressIsNil = errors.New("multi-address is nil")

ErrMultiAddressIsNil is returned when the multi-address contains nil fields.

View Source
var ErrRecvOnClosedStream = errors.New("receive on closed stream")

ErrRecvOnClosedStream is returned when a call to Stream.Recv happens on a closed Stream.

View Source
var ErrSendOnClosedStream = errors.New("send on closed stream")

ErrSendOnClosedStream is returned when a call to Stream.Send happens on a closed Stream.

Functions

This section is empty.

Types

type ChannelHub

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

A ChannelHub will store a map of all active channelStreams between identity.Addresses and ensures that the mapping is symmetrical.

func NewChannelHub

func NewChannelHub() ChannelHub

NewChannelHub returns a ChannelHub with no connections in the map.

type Client

type Client interface {

	// Connect to a Server identified by an identity.MultiAddress. Returns a
	// Stream for sending, and receiving, Messages to, and from, the Server.
	// The context.Context can be used to close the Stream.
	Connect(ctx context.Context, multiAddr identity.MultiAddress) (Stream, error)
}

Client is an interface for connecting to a Server.

type Message

type Message interface {
	encoding.BinaryMarshaler
	encoding.BinaryUnmarshaler

	// Types that can be used in an Stream must implement this pass through
	// method. It only exists to restrict Stream to types that have been
	// explicitly marked as compatible to avoid programmer error.
	IsMessage()
}

Message is an interface for data that can be sent over a bidirectional stream between nodes.

type Server

type Server interface {

	// Listen for a connection from a Client identified by an identity.Address.
	// Returns a Stream for sending, and receiving, Messages to, and from, the
	// Client. The context.Context can be used to close the Stream.
	Listen(ctx context.Context, addr identity.Address) (Stream, error)
}

Server is an interface for accepting connections from a Client.

type Stream

type Stream interface {
	// Send a Message on the Stream. Calls to Stream.Send might be blocking
	// depending on the underlying implementation.
	Send(Message) error

	// Recv a Message from the Stream. Calls to Stream.Recv will block until
	// a Message is received.
	Recv(Message) error
}

Stream is an interface for sending and receiving Messages over a bidirectional stream. It abstracts over the client and server architecture.

type Streamer

type Streamer interface {

	// Open a Stream to an identity.MultiAddress by listening for a Client
	// connection, or connecting to a Server. Calls to Streamer.Open are
	// blocking. The context.Context can be used to close the Stream.
	Open(ctx context.Context, multiAddr identity.MultiAddress) (Stream, error)
}

Streamer abstracts over the Client and Server model. By comparing identity.Addresses it determines whether opening a Stream should be done by listening for a connection as a Server, or connecting to a Server as a Client.

func NewChannelStreamer

func NewChannelStreamer(addr identity.Address, hub *ChannelHub) Streamer

NewChannelStreamer returns a Streamer that uses channel to implement the Stream interface. Streams are recycled whenever multiple connections between two identity.Addresses is needed.

func NewStreamRecycler

func NewStreamRecycler(streamer Streamer) Streamer

NewStreamRecycler returns a Streamer that wraps another Streamer. It will use the wrapper Streamer to open and close Streams, but will recycle existing Streams when multiple connections to the same identity.Address are needed. Streams opened will be safe for concurrent use whenever the wrapped Streamer can open Streams that are safe for concurrent use.

func NewStreamer

func NewStreamer(addr identity.Address, client Client, server Server) Streamer

NewStreamer returns a Streamer that uses an identity.Address to identify itself. It will use the Client to connect streams when opening streams to an identity.Address greater than its own, and it will use the Server to listen for connections when opening streams to an identity.Address lower than its own.

Jump to

Keyboard shortcuts

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