network

package
v0.0.0-...-d151d1d Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicStatus

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

BasicStatus stores metadata of a Connection or a Stream.

func NewStatus

func NewStatus(direction Direction, establishedTime time.Time, extra map[any]any) *BasicStatus

NewStatus creates a new BasicStatus instance.

func (*BasicStatus) Closed

func (s *BasicStatus) Closed() bool

Closed returns whether the connection is closed.

func (*BasicStatus) Direction

func (s *BasicStatus) Direction() Direction

Direction returns the direction of the connection.

func (*BasicStatus) EstablishedTime

func (s *BasicStatus) EstablishedTime() time.Time

EstablishedTime returns the time when the connection was established.

func (*BasicStatus) Extra

func (s *BasicStatus) Extra() map[interface{}]interface{}

Extra returns the extra metadata of the connection.

func (*BasicStatus) SetClosed

func (s *BasicStatus) SetClosed()

SetClosed marks the connection as closed.

type Connection

type Connection interface {
	io.Closer
	Status

	// LocalAddr returns the local net multi-address of the connection.
	LocalAddr() ma.Multiaddr

	// LocalNetAddr returns the local net address of the connection.
	LocalNetAddr() net.Addr

	// LocalPeerID returns the local peer ID of the connection.
	LocalPeerID() peer.ID

	// RemoteAddr returns the remote net multi-address of the connection.
	RemoteAddr() ma.Multiaddr

	// RemoteNetAddr returns the remote net address of the connection.
	RemoteNetAddr() net.Addr

	// RemotePeerID returns the remote peer ID of the connection.
	RemotePeerID() peer.ID

	// Network returns the network instance that created this connection.
	Network() Network

	// OpenSendStream tries to open a sending stream with the connection.
	OpenSendStream() (SendStream, error)

	// AcceptReceiveStream accepts a receiving stream with the connection.
	// It will block until a new receiving stream is accepted or the connection is closed.
	AcceptReceiveStream() (ReceiveStream, error)
}

Connection defines a connection with a remote peer.

type Dialer

type Dialer interface {
	// Dial tries to establish an outbound connection with the remote address.
	Dial(ctx context.Context, remoteAddr ma.Multiaddr) (Connection, error)
}

Dialer provides a way to establish a connection with others.

type Direction

type Direction uint8

Direction of a connection or a stream.

const (
	// Unknown is the default direction.
	Unknown Direction = iota
	// Inbound is for when the remote peer initiated a connection.
	Inbound
	// Outbound is for when the local peer initiated a connection.
	Outbound
)

func (Direction) String

func (d Direction) String() string

type Listener

type Listener interface {
	io.Closer

	// Listen runs a task that creates listeners with the given addresses and waits for accepting inbound connections.
	// It returns an error if there is any issue starting the listeners.
	Listen(ctx context.Context, addresses ...ma.Multiaddr) error

	// ListenAddresses returns the list of local addresses for the listeners.
	ListenAddresses() []ma.Multiaddr
}

Listener provides a way to accept connections established with others.

type Network

type Network interface {
	Dialer
	Listener
	io.Closer

	// AcceptedConnChan returns a channel for notifying about new connections.
	// Whenever a new connection is accepted or dialed, Network should write the new connection to chan.
	AcceptedConnChan() <-chan Connection

	// Disconnect closes a connection.
	Disconnect(conn Connection) error

	// Closed returns whether the network is closed.
	Closed() bool

	// LocalPeerID returns the local peer ID.
	LocalPeerID() peer.ID

	// Logger returns the logger instance.
	Logger() *rainbowlog.Logger
}

Network is a state machine interface that provides a Dialer and a Listener to build a network.

type ReceiveStream

type ReceiveStream interface {
	io.Reader
	// contains filtered or unexported methods
}

ReceiveStream is an interface that defines a way to receive data.

type SendStream

type SendStream interface {
	io.Writer
	// contains filtered or unexported methods
}

SendStream is an interface that defines a way to send data.

type Status

type Status interface {
	Direction() Direction
	EstablishedTime() time.Time
	Extra() map[any]any
	SetClosed()
	Closed() bool
}

Status is an interface for storing metadata of a Connection or a Stream.

Jump to

Keyboard shortcuts

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