wire

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: 14 Imported by: 19

Documentation

Overview

Package wire contains the basic wire communication infrastructure like wire message en- and decoding. The actual (de)serialization functions are found in package pkg/io.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodeMsg added in v0.9.0

func EncodeMsg(msg Msg, w io.Writer) error

EncodeMsg encodes a message into an io.Writer. It also encodes the message type whereas the Msg.Encode implementation is assumed not to write the type.

func IndexOfAddr added in v0.5.0

func IndexOfAddr(addrs []Address, addr Address) int

IndexOfAddr returns the index of the given address in the address slice, or -1 if it is not part of the slice.

func RegisterDecoder added in v0.4.0

func RegisterDecoder(t Type, decoder func(io.Reader) (Msg, error))

RegisterDecoder sets the decoder of messages of Type `t`.

func RegisterExternalDecoder added in v0.4.0

func RegisterExternalDecoder(t Type, decoder func(io.Reader) (Msg, error), name string)

RegisterExternalDecoder sets the decoder of messages of external type `t`. This is like RegisterDecoder but for message types not part of the Perun wire protocol and thus not known natively. This can be used by users of the framework to create additional message types and send them over the same peer connection. It also comes in handy to register types for testing.

func SetNewAddressFunc added in v0.10.1

func SetNewAddressFunc(f NewAddressFunc)

SetNewAddressFunc sets the address generator function.

Types

type Account added in v0.4.0

type Account interface {
	// Address used by this account.
	Address() Address
}

Account is a node's permanent Perun identity, which is used to establish authenticity within the Perun peer-to-peer network.

type AddrKey added in v0.10.1

type AddrKey string

AddrKey is a non-human readable representation of an `Address`. It can be compared and therefore used as a key in a map.

func Key added in v0.10.1

func Key(a Address) AddrKey

Key returns the `AddrKey` corresponding to the passed `Address`. The `Address` can be retrieved with `FromKey`. Panics when the `Address` can't be encoded.

type Address added in v0.4.0

type Address interface {
	// BinaryMarshaler marshals the address to binary.
	encoding.BinaryMarshaler
	// BinaryUnmarshaler unmarshals an address from binary.
	encoding.BinaryUnmarshaler
	// Equal returns wether the two addresses are equal.
	Equal(Address) bool
	// Cmp compares the byte representation of two addresses. For `a.Cmp(b)`
	// returns -1 if a < b, 0 if a == b, 1 if a > b.
	Cmp(Address) int
}

Address is a Perun node's network address, which is used as a permanent identity within the Perun peer-to-peer network. For now, it is based on type wallet.Address.

func NewAddress added in v0.9.0

func NewAddress() Address

NewAddress returns a new address.

type Addresses added in v0.4.0

type Addresses []Address

Addresses is a helper type for encoding and decoding address slices in situations where the length of the slice is known.

func (Addresses) Decode added in v0.5.0

func (a Addresses) Decode(r stdio.Reader) error

Decode decodes wallet addresses.

func (Addresses) Encode added in v0.5.0

func (a Addresses) Encode(w stdio.Writer) error

Encode encodes wire addresses.

type AddressesWithLen added in v0.4.0

type AddressesWithLen []Address

AddressesWithLen is a helper type for encoding and decoding address slices of unknown length.

func (*AddressesWithLen) Decode added in v0.5.0

func (a *AddressesWithLen) Decode(r stdio.Reader) error

Decode decodes a wallet address slice of unknown length.

func (AddressesWithLen) Encode added in v0.5.0

func (a AddressesWithLen) Encode(w stdio.Writer) error

Encode encodes wire addresses with length.

type AuthResponseMsg added in v0.4.0

type AuthResponseMsg struct{}

AuthResponseMsg is the response message in the peer authentication protocol.

This will be expanded later to contain signatures.

func (*AuthResponseMsg) Decode added in v0.4.0

func (m *AuthResponseMsg) Decode(r io.Reader) (err error)

Decode decodes an AuthResponseMsg from an io.Reader.

func (*AuthResponseMsg) Encode added in v0.4.0

func (m *AuthResponseMsg) Encode(w io.Writer) error

Encode encodes this AuthResponseMsg into an io.Writer.

func (*AuthResponseMsg) Type added in v0.4.0

func (m *AuthResponseMsg) Type() Type

Type returns AuthResponse.

type Bus added in v0.4.0

type Bus interface {
	Publisher

	// SubscribeClient should route all messages with clientAddr as recipient to
	// the provided Consumer. Every address may only be subscribed to once.
	SubscribeClient(c Consumer, clientAddr Address) error
}

A Bus is a central message bus over which all clients of a channel network communicate. It is used as the transport layer abstraction for the client.Client.

func NewHybridBus added in v0.9.1

func NewHybridBus(buses ...Bus) Bus

NewHybridBus creates a hybrid bus that sends and receives messages over multiple buses. Publishing a message over a hybrid bus will attempt to publish it over all its sub-buses simultaneously. Subscribing a client to the hybrid bus will subscribe it to all its sub-buses. The sub-buses can still be accessed independently for more fine-grained control.

Once any bus indicates success in publishing a message, the publishing operations on all other buses are cancelled. This bus is best used with a LocalBus and one network bus, otherwise, with multiple non-instantaneous buses, messages might get sent multiple times. Buses that indicate success immediately (such as when caching outbound messages) can also cause problems if they cancel non-immediate buses.

type Cache added in v0.4.0

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

Cache is a message cache.

func MakeCache added in v0.9.0

func MakeCache() Cache

MakeCache creates a new cache.

func (*Cache) Cache added in v0.4.0

func (c *Cache) Cache(p *Predicate)

Cache is a message cache. The default value is a valid empty cache.

func (*Cache) Flush added in v0.4.0

func (c *Cache) Flush()

Flush empties the message cache and removes all predicates.

func (*Cache) Messages added in v0.9.0

func (c *Cache) Messages(p Predicate) []*Envelope

Messages retrieves all messages from the cache that match the predicate. They are removed from the Cache.

func (*Cache) Put added in v0.4.0

func (c *Cache) Put(e *Envelope) bool

Put puts the message into the cache if it matches any active predicate. If it matches several predicates, it is still only added once to the cache.

func (*Cache) Release added in v0.9.0

func (c *Cache) Release(p *Predicate)

Release releases the cache predicate.

func (*Cache) Size added in v0.4.0

func (c *Cache) Size() int

Size returns the number of messages held in the message cache.

type Cacher added in v0.4.0

type Cacher interface {
	// Cache should enable the caching of messages
	Cache(*Predicate)
}

A Cacher has the Cache method to enable caching of messages.

type Consumer added in v0.4.0

type Consumer interface {
	// The producer calls OnClose() to unregister the Consumer after it is
	// closed.
	sync.OnCloser
	// Put is called by the emitter when relaying a message.
	Put(*Envelope)
}

Consumer consumes messages fed into it via Put().

type Envelope added in v0.4.0

type Envelope struct {
	Sender    Address // Sender of the message.
	Recipient Address // Recipient of the message.
	// Msg contained in this Envelope. Not embedded so Envelope doesn't implement Msg.
	Msg Msg
}

An Envelope encapsulates a message with routing information, that is, the sender and intended recipient.

type EnvelopeSerializer added in v0.9.0

type EnvelopeSerializer interface {
	Encode(w io.Writer, env *Envelope) error
	Decode(r io.Reader) (*Envelope, error)
}

EnvelopeSerializer serializes/deserializes envelopes into/from streams.

type LocalBus added in v0.4.0

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

LocalBus is a bus that only sends message in the same process.

func NewLocalBus added in v0.4.0

func NewLocalBus() *LocalBus

NewLocalBus creates a new local bus, which only targets receivers that lie within the same process.

func (*LocalBus) Publish added in v0.4.0

func (h *LocalBus) Publish(ctx context.Context, e *Envelope) error

Publish implements wire.Bus.Publish. It returns only once the recipient received the message or the context times out.

func (*LocalBus) SubscribeClient added in v0.4.0

func (h *LocalBus) SubscribeClient(c Consumer, receiver Address) error

SubscribeClient implements wire.Bus.SubscribeClient. There can only be one subscription per receiver address. When the Consumer closes, its subscription is removed.

type Msg added in v0.4.0

type Msg interface {
	// Type returns the message's type.
	Type() Type
}

Msg is the top-level abstraction for all messages sent between Perun nodes.

func DecodeMsg added in v0.9.0

func DecodeMsg(r io.Reader) (Msg, error)

DecodeMsg decodes a message from an io.Reader.

func NewAuthResponseMsg added in v0.4.0

func NewAuthResponseMsg(_ Account) Msg

NewAuthResponseMsg creates an authentication response message.

type NewAddressFunc added in v0.10.1

type NewAddressFunc = func() Address

NewAddressFunc is an address generator function.

type PingMsg added in v0.4.0

type PingMsg struct {
	PingPongMsg
}

PingMsg is a ping request. It contains the time at which it was sent, so that the recipient can also measure the time it took to transmit the ping request.

func NewPingMsg added in v0.4.0

func NewPingMsg() *PingMsg

NewPingMsg creates a new Ping message.

func (*PingMsg) Type added in v0.4.0

func (m *PingMsg) Type() Type

Type returns Ping.

type PingPongMsg added in v0.9.0

type PingPongMsg struct {
	Created time.Time
}

PingPongMsg is the common message type used by ping and pong msgs, since both the messages are essentially the same.

func (*PingPongMsg) Decode added in v0.9.0

func (m *PingPongMsg) Decode(reader io.Reader) error

Decode implements msg.Decode.

func (PingPongMsg) Encode added in v0.9.0

func (m PingPongMsg) Encode(writer io.Writer) error

Encode implements msg.Encode.

type PongMsg added in v0.4.0

type PongMsg struct {
	PingPongMsg
}

PongMsg is the response to a ping message. It contains the time at which it was sent, so that the recipient knows how long the ping request took to be transmitted, and how quickly the response was sent.

func NewPongMsg added in v0.4.0

func NewPongMsg() *PongMsg

NewPongMsg creates a new Pong message.

func (*PongMsg) Type added in v0.4.0

func (m *PongMsg) Type() Type

Type returns Pong.

type Predicate added in v0.4.0

type Predicate = func(*Envelope) bool

A Predicate defines a message filter.

type Publisher added in v0.4.0

type Publisher interface {
	// Publish should return nil when the message was delivered (outgoing) or is
	// guaranteed to be eventually delivered (cached), depending on the goal of the
	// implementation.
	Publish(context.Context, *Envelope) error
}

A Publisher allows to publish a message in a messaging network.

type Receiver added in v0.4.0

type Receiver struct {
	sync.Closer
	// contains filtered or unexported fields
}

Receiver is a helper object that can subscribe to different message categories from multiple peers. Receivers must only be used by a single execution context at a time. If multiple contexts need to access a peer's messages, then multiple receivers have to be created.

func NewReceiver added in v0.4.0

func NewReceiver() *Receiver

NewReceiver creates a new receiver.

func (*Receiver) Next added in v0.4.0

func (r *Receiver) Next(ctx context.Context) (*Envelope, error)

Next returns a channel to the next message.

func (*Receiver) Put added in v0.4.0

func (r *Receiver) Put(e *Envelope)

Put puts a new message into the queue.

type Relay added in v0.4.0

type Relay struct {
	sync.Closer
	// contains filtered or unexported fields
}

Relay handles (un)registering Consumers for a message Relay's messages.

func NewRelay added in v0.4.0

func NewRelay() *Relay

NewRelay returns a new Relay which logs unhandled messages.

func (*Relay) Cache added in v0.4.0

func (p *Relay) Cache(predicate *Predicate)

Cache enables caching of messages that don't match any consumer. They are only cached if they match the given predicate.

func (*Relay) Close added in v0.4.0

func (p *Relay) Close() error

Close closes the relay.

func (*Relay) Put added in v0.4.0

func (p *Relay) Put(e *Envelope)

Put puts an Envelope in the relay.

func (*Relay) ReleaseCache added in v0.9.0

func (p *Relay) ReleaseCache(predicate *Predicate)

ReleaseCache disable caching for the given predicate.

func (*Relay) SetDefaultMsgHandler added in v0.4.0

func (p *Relay) SetDefaultMsgHandler(handler func(*Envelope))

SetDefaultMsgHandler sets the default message handler.

func (*Relay) Subscribe added in v0.4.0

func (p *Relay) Subscribe(c Consumer, predicate Predicate) error

Subscribe adds a Consumer to the subscriptions. If the Consumer is already subscribed, Subscribe panics. If the producer is closed, Subscribe returns an error. Otherwise, Subscribe returns nil.

type ShutdownMsg added in v0.4.0

type ShutdownMsg struct {
	Reason string
}

ShutdownMsg is sent when orderly shutting down a connection.

Reason should be a UTF-8 encodable string.

func (*ShutdownMsg) Decode added in v0.4.0

func (m *ShutdownMsg) Decode(r io.Reader) error

Decode implements msg.Decode.

func (*ShutdownMsg) Encode added in v0.4.0

func (m *ShutdownMsg) Encode(w io.Writer) error

Encode implements msg.Encode.

func (*ShutdownMsg) Type added in v0.4.0

func (m *ShutdownMsg) Type() Type

Type implements msg.Type.

type Subscriber added in v0.4.0

type Subscriber interface {
	// Subscribe adds a Consumer to the subscriptions.
	// If the Consumer is already subscribed, Subscribe panics.
	Subscribe(Consumer, Predicate) error
}

A Subscriber allows to subscribe Consumers, which will receive messages that match a predicate.

type Type added in v0.4.0

type Type uint8

Type is an enumeration used for (de)serializing messages and identifying a message's Type.

const (
	Ping Type = iota
	Pong
	Shutdown
	AuthResponse
	LedgerChannelProposal
	LedgerChannelProposalAcc
	SubChannelProposal
	SubChannelProposalAcc
	VirtualChannelProposal
	VirtualChannelProposalAcc
	ChannelProposalRej
	ChannelUpdate
	VirtualChannelFundingProposal
	VirtualChannelSettlementProposal
	ChannelUpdateAcc
	ChannelUpdateRej
	ChannelSync
	LastType // upper bound on the message types of the Perun wire protocol
)

Enumeration of message categories known to the Perun framework.

func (Type) String added in v0.4.0

func (t Type) String() string

String returns the name of a message type if it is valid and name known or otherwise its numerical representation.

func (Type) Valid added in v0.4.0

func (t Type) Valid() bool

Valid checks whether a decoder is known for the type.

Directories

Path Synopsis
net
Package net contains the abstract communication logic between peers.
Package net contains the abstract communication logic between peers.
simple
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.
test
Package test contains the testing types for wire/net.
Package test contains the testing types for wire/net.
Package perunio contains functionality for the serialization of standard Go types.
Package perunio contains functionality for the serialization of standard Go types.
serializer
Package serializer implements the wire serializer interface using the perunio encoding format.
Package serializer implements the wire serializer interface using the perunio encoding format.
test
Package test contains test helper functions for running generic tests for types implementing perunio serializer.
Package test contains test helper functions for running generic tests for types implementing perunio serializer.
Package protobuf implements the wire serializer interface using the protocol buffers serialization protocol.
Package protobuf implements the wire serializer interface using the protocol buffers serialization protocol.
test
Package test contains test helper functions for running generic tests for protobuf serialization.
Package test contains test helper functions for running generic tests for protobuf serialization.
Package test contains implementations of the peer interfaces that are useful for testing.
Package test contains implementations of the peer interfaces that are useful for testing.

Jump to

Keyboard shortcuts

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