net

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: Apache-2.0 Imports: 13 Imported by: 12

Documentation

Overview

Package net contains the abstract communication logic between peers.

Index

Constants

View Source
const (
	// PublishAttempts defines how many attempts a Bus.Publish call can take
	// to succeed.
	PublishAttempts = 3
	// PublishCooldown defines how long should be waited before Bus.Publish is
	// called again in case it failed.
	PublishCooldown = 3 * time.Second
)

Variables

This section is empty.

Functions

func ExchangeAddrsActive

func ExchangeAddrsActive(ctx context.Context, id wire.Account, peer wire.Address, conn Conn) error

ExchangeAddrsActive executes the active role of the address exchange protocol. It is executed by the person that dials.

In the future, it will be extended to become a proper authentication protocol. The protocol will then exchange Perun addresses and establish authenticity.

func ExchangeAddrsPassive

func ExchangeAddrsPassive(ctx context.Context, id wire.Account, conn Conn) (wire.Address, error)

ExchangeAddrsPassive executes the passive role of the address exchange protocol. It is executed by the person that listens for incoming connections.

func IsAuthenticationError added in v0.7.0

func IsAuthenticationError(err error) bool

IsAuthenticationError returns true if the error was a AuthenticationError.

func NewAuthenticationError added in v0.7.0

func NewAuthenticationError(sender, receiver, own wire.Address, msg string) error

NewAuthenticationError creates a new AuthenticationError.

Types

type AuthenticationError added in v0.7.0

type AuthenticationError struct {
	Sender, Receiver, Own wire.Address
}

AuthenticationError describes an error which occures when the ExchangeAddrs protcol fails because it got a different Address than expected.

func (*AuthenticationError) Error added in v0.7.0

func (e *AuthenticationError) Error() string

type Bus

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

Bus implements the wire.Bus interface using network connections.

func NewBus

func NewBus(id wire.Account, d Dialer, s wire.EnvelopeSerializer) *Bus

NewBus creates a new network bus. The dialer and listener are used to establish new connections internally, while id is this node's identity.

func (*Bus) Close

func (b *Bus) Close() error

Close closes the bus and terminates its goroutines.

func (*Bus) Listen

func (b *Bus) Listen(l Listener)

Listen listens for incoming connections to add to the Bus.

func (*Bus) Publish

func (b *Bus) Publish(ctx context.Context, e *wire.Envelope) (err error)

Publish sends an envelope to its recipient. Automatically establishes a communication channel to the recipient using the bus' dialer. Only returns when the context is aborted or the envelope was sent successfully.

func (*Bus) SubscribeClient

func (b *Bus) SubscribeClient(c wire.Consumer, addr wire.Address) error

SubscribeClient subscribes a new client to the bus. Duplicate subscriptions are forbidden and will cause a panic. The supplied consumer will receive all messages that are sent to the requested address.

type Conn

type Conn interface {
	// Recv receives an envelope from the peer.
	// If an error occurs, the connection must close itself.
	Recv() (*wire.Envelope, error)
	// Send sends an envelope to the peer.
	// If an error occurs, the connection must close itself.
	Send(*wire.Envelope) error
	// Close closes the connection and aborts any ongoing Send() and Recv()
	// calls.
	//
	// Repeated calls to Close() result in an error.
	Close() error
}

Conn is a connection to a peer, and can send wire messages. The Send and Recv methods do not have to be reentrant, but calls to Close that happen in other threads must interrupt ongoing Send and Recv calls. This is the default behavior for sockets.

func NewIoConn

func NewIoConn(conn io.ReadWriteCloser, serializer wire.EnvelopeSerializer) Conn

NewIoConn creates a peer message connection from an io stream.

type Dialer

type Dialer interface {
	// Dial creates a connection to a peer. The passed context is used to abort
	// the dialing process. The returned connection might not belong to the
	// requested address.
	//
	// `ser` is used for message serialization.
	//
	// Dial needs to be reentrant, and concurrent calls to Close() must abort
	// any ongoing Dial() calls.
	Dial(ctx context.Context, addr wire.Address, ser wire.EnvelopeSerializer) (Conn, error)
	// Close aborts any ongoing calls to Dial().
	//
	// Close() needs to be reentrant, and repeated calls to Close() need to
	// return an error.
	Close() error
}

Dialer is an interface that allows creating a connection to a peer via its Perun address. The established connections are not authenticated yet.

type Endpoint

type Endpoint struct {
	Address wire.Address // The Endpoint's Perun address.
	// contains filtered or unexported fields
}

Endpoint is an authenticated connection to a Perun node. It contains the node's identity. Endpoints are thread-safe. Endpoints must not be created manually. The creation of Endpoints is handled by the Registry, which tracks all existing Endpoints. The registry, in turn, is used by the Bus.

Sending messages to a node is done via the Send() method. To receive messages from an Endpoint, use the Receiver helper type (by subscribing).

func (*Endpoint) Close

func (p *Endpoint) Close() (err error)

Close closes the Endpoint's connection. A closed Endpoint is no longer usable.

func (*Endpoint) Send

func (p *Endpoint) Send(ctx context.Context, e *wire.Envelope) error

Send sends a single message to an Endpoint. Fails if the Endpoint is closed via Close() or the transmission fails.

The passed context is used to timeout the send operation. If the context times out, the Endpoint is closed.

func (*Endpoint) String

func (p *Endpoint) String() string

String returns the Endpoint's address string.

type EndpointRegistry

type EndpointRegistry struct {
	log.Embedding
	perunsync.Closer
	// contains filtered or unexported fields
}

EndpointRegistry is a peer Endpoint registry and manages the establishment of new connections and acts as a dictionary for looking up established connections. It should not be used manually, but only internally by a wire.Bus.

func NewEndpointRegistry

func NewEndpointRegistry(
	id wire.Account,
	onNewEndpoint func(wire.Address) wire.Consumer,
	dialer Dialer,
	ser wire.EnvelopeSerializer,
) *EndpointRegistry

NewEndpointRegistry creates a new registry. The provided callback is used to set up new peer's subscriptions and it is called before the peer starts receiving messages.

func (*EndpointRegistry) Close

func (r *EndpointRegistry) Close() (err error)

Close closes the registry's dialer and all its peers.

func (*EndpointRegistry) Endpoint added in v0.9.0

func (r *EndpointRegistry) Endpoint(ctx context.Context, addr wire.Address) (*Endpoint, error)

Endpoint looks up an Endpoint via its perun address. If the Endpoint does not exist yet, it is dialed. Does not return until the peer is dialed or the context is closed.

func (*EndpointRegistry) Has

func (r *EndpointRegistry) Has(addr wire.Address) bool

Has return true if and only if there is a peer with the given address in the registry. The function does not differentiate between regular and placeholder peers.

func (*EndpointRegistry) Listen

func (r *EndpointRegistry) Listen(listener Listener)

Listen starts listening for incoming connections on the provided listener and currently just automatically accepts them after successful authentication. This function does not start go routines but instead should be started by the user as `go registry.Listen()`.

func (*EndpointRegistry) NumPeers

func (r *EndpointRegistry) NumPeers() int

NumPeers returns the current number of peers in the registry including placeholder peers (cf. Registry.Get).

type Listener

type Listener interface {
	// Accept accepts an incoming connection, which still has to perform
	// authentication to exchange addresses.
	//
	// `ser` specifies the message serialization format.
	//
	// This function does not have to be reentrant, but concurrent calls to
	// Close() must abort ongoing Accept() calls. Accept() must only return
	// errors after Close() was called or an unrecoverable fatal error occurred
	// in the Listener and it is closed.
	Accept(ser wire.EnvelopeSerializer) (Conn, error)
	// Close closes the listener and aborts any ongoing Accept() call.
	Close() error
}

Listener is an interface that allows listening for peer incoming connections. The accepted connections still need to be authenticated.

Directories

Path Synopsis
Package simple contains simplistic implementation for the wire.Dialer and wire.Listener interfaces.
Package simple contains simplistic implementation for the wire.Dialer and wire.Listener interfaces.
Package test contains the testing types for wire/net.
Package test contains the testing types for wire/net.

Jump to

Keyboard shortcuts

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