peer

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2022 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create added in v0.4.0

func Create(ctx context.Context, reader *Reader, writer *Writer)

Create two-way communication with a peer. This function will allow both goroutines to run as long as no errors are encountered. Once the first error comes through, the context is canceled, and both goroutines are cleaned up.

Types

type Connection

type Connection struct {
	net.Conn
	// contains filtered or unexported fields
}

Connection holds the TCP connection to another node, and it's known protocol magic. The `net.Conn` is guarded by a mutex, to allow both multicast and one-to-one communication between peers.

func NewConnection added in v0.4.3

func NewConnection(conn net.Conn, gossip *protocol.Gossip) *Connection

NewConnection creates a peer connection struct.

func (*Connection) Addr

func (c *Connection) Addr() string

Addr returns the peer's address as a string.

func (*Connection) ReadMessage

func (c *Connection) ReadMessage() ([]byte, error)

ReadMessage reads from the connection.

func (*Connection) Write

func (c *Connection) Write(b []byte) (int, error)

Write a message to the connection. Conn needs to be locked, as this function can be called both by the WriteLoop, and by the writer on the ring buffer.

type Connector added in v0.4.3

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

Connector is responsible for accepting incoming connection requests, and establishing outward connections with desired peers.

func NewConnector added in v0.4.3

func NewConnector(eb eventbus.Broker, gossip *protocol.Gossip, port string,
	processor *MessageProcessor, services protocol.ServiceFlag,
	connectFunc connectFunc,
) *Connector

NewConnector creates a new peer connector, and spawns a goroutine that will accept incoming connection requests on the current address, with the given port.

func (*Connector) Close added in v0.4.3

func (c *Connector) Close() error

Close the listener.

func (*Connector) Connect added in v0.4.3

func (c *Connector) Connect(addr string) error

Connect dials a connection with its string, then on succession we pass the connection and the address to the OnConn method.

func (*Connector) Dial added in v0.4.3

func (c *Connector) Dial(addr string) (net.Conn, error)

Dial dials up a connection, given its address string.

func (*Connector) GetConnectionsCount added in v0.4.3

func (c *Connector) GetConnectionsCount() int

GetConnectionsCount returns the amount of active connections the node has.

func (*Connector) ProcessNewAddress added in v0.4.3

func (c *Connector) ProcessNewAddress(srcPeerID string, m message.Message) ([]bytes.Buffer, error)

ProcessNewAddress will handle a new Addr message from the network. Satisfies the peer.ProcessorFunc interface.

type GossipConnector added in v0.3.0

type GossipConnector struct {
	*Connection
}

GossipConnector calls Gossip.Process on the message stream incoming from the ringbuffer. It absolves the function previously carried over by the Gossip preprocessor.

func (*GossipConnector) Write added in v0.3.0

func (g *GossipConnector) Write(b []byte, _ *message.Metadata, priority byte) (int, error)

type MessageProcessor added in v0.4.0

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

MessageProcessor is connected to all of the processing units that are tied to the peer. It sends an incoming message in the right direction, according to its topic.

func NewMessageProcessor added in v0.4.0

func NewMessageProcessor(bus eventbus.Broker) *MessageProcessor

NewMessageProcessor returns an initialized MessageProcessor.

func (*MessageProcessor) Collect added in v0.4.0

func (m *MessageProcessor) Collect(srcPeerID string, packet []byte, respRingBuf *ring.Buffer, services protocol.ServiceFlag, metadata *message.Metadata) ([]bytes.Buffer, error)

Collect a message from the network. The message is unmarshaled and passed down to the processing function.

func (*MessageProcessor) Register added in v0.4.0

func (m *MessageProcessor) Register(topic topics.Topic, fn ProcessorFunc)

Register a method to a certain topic. This method will be called when a message of the given topic is received.

type ProcessorFunc added in v0.4.0

type ProcessorFunc func(srcPeerID string, m message.Message) ([]bytes.Buffer, error)

ProcessorFunc defines an interface for callbacks which can be registered to the MessageProcessor, in order to process messages from the network.

type Reader

type Reader struct {
	*Connection
	// contains filtered or unexported fields
}

Reader abstracts all of the logic and fields needed to receive messages from other network nodes.

func (*Reader) Accept

func (p *Reader) Accept(services protocol.ServiceFlag) error

Accept will perform the protocol handshake with the peer.

func (*Reader) Handshake

func (p *Reader) Handshake(services protocol.ServiceFlag) error

Handshake with another peer.

func (*Reader) ReadLoop

func (p *Reader) ReadLoop(ctx context.Context, ringBuf *ring.Buffer)

ReadLoop will block on the read until a message is read, or until the deadline is reached. Should be called in a go-routine, after a successful handshake with a peer. Eventual duplicated messages are silently discarded.

type ReaderFactory added in v0.4.0

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

ReaderFactory is responsible for spawning peers. It provides them with the reference to the message processor, which will process the received messages.

func NewReaderFactory added in v0.4.0

func NewReaderFactory(processor *MessageProcessor) *ReaderFactory

NewReaderFactory returns an initialized ReaderFactory.

func (*ReaderFactory) SpawnReader added in v0.4.0

func (f *ReaderFactory) SpawnReader(conn *Connection) *Reader

SpawnReader returns a Reader. It will still need to be launched by running ReadLoop in a goroutine.

type VersionMessage

type VersionMessage struct {
	Version   *protocol.Version
	Timestamp int64
	Services  protocol.ServiceFlag
}

VersionMessage is a version message on the dusk wire protocol.

type Writer

type Writer struct {
	*Connection
	// contains filtered or unexported fields
}

Writer abstracts all of the logic and fields needed to write messages to other network nodes.

func NewWriter

func NewWriter(conn *Connection, subscriber eventbus.Subscriber, keepAlive ...time.Duration) *Writer

NewWriter returns a Writer. It will still need to be initialized by subscribing to the gossip topic with a stream handler, and by running the WriteLoop in a goroutine.

func (*Writer) Connect

func (w *Writer) Connect(services protocol.ServiceFlag) error

Connect will perform the protocol handshake with the peer. If successful...

func (*Writer) Handshake

func (w *Writer) Handshake(services protocol.ServiceFlag) error

Handshake with another peer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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