Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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
var ErrMessageIsNil = errors.New("message is nil")
ErrMessageIsNil is returned when the message contains nil fields.
var ErrMultiAddressIsNil = errors.New("multi-address is nil")
ErrMultiAddressIsNil is returned when the multi-address contains nil fields.
var ErrRecvOnClosedStream = errors.New("receive on closed stream")
ErrRecvOnClosedStream is returned when a call to Stream.Recv happens on a closed Stream.
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 ¶
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 ¶
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.