network

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicStat

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

BasicStat stores metadata of a Conn or a Stream.

func NewStat

func NewStat(direction Direction, establishedTime time.Time, extra map[interface{}]interface{}) *BasicStat

NewStat create a new BasicStat instance.

func (*BasicStat) Direction

func (s *BasicStat) Direction() Direction

Direction return the direction of the connection there are two types of link directions: Inbound and Outbound

func (*BasicStat) EstablishedTime

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

EstablishedTime return the time the connection was established

func (*BasicStat) Extra

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

Extra return information in the state

func (*BasicStat) IsClosed

func (s *BasicStat) IsClosed() bool

IsClosed return whether it is closed

func (*BasicStat) SetClosed

func (s *BasicStat) SetClosed()

SetClosed set to close

type Conn

type Conn interface {
	io.Closer
	Stat
	// LocalAddr is the local net multi-address of the connection.
	LocalAddr() ma.Multiaddr
	// LocalNetAddr is the local net address of the connection.
	LocalNetAddr() net.Addr
	// LocalPeerID is the local peer id of the connection.
	LocalPeerID() peer.ID
	// RemoteAddr is the remote net multi-address of the connection.
	RemoteAddr() ma.Multiaddr
	// RemoteNetAddr is the remote net address of the connection.
	RemoteNetAddr() net.Addr
	// RemotePeerID is the remote peer id of the connection.
	RemotePeerID() peer.ID
	// Network is the network instance who create this connection.
	Network() Network
	// CreateSendStream try to open a sending stream with the connection.
	CreateSendStream() (SendStream, error)
	// AcceptReceiveStream accept a receiving stream with the connection.
	// It will block until a new receiving stream accepted or connection closed.
	AcceptReceiveStream() (ReceiveStream, error)
	// CreateBidirectionalStream try to open a bidirectional stream with the connection.
	CreateBidirectionalStream() (Stream, error)
	// AcceptBidirectionalStream accept a bidirectional stream with the connection.
	// It will block until a new bidirectional stream accepted or connection closed.
	AcceptBidirectionalStream() (Stream, error)

	// SetDeadline used by the relay
	SetDeadline(t time.Time) error

	// SetReadDeadline used by the relay
	SetReadDeadline(t time.Time) error

	// SetWriteDeadline used by the relay
	SetWriteDeadline(t time.Time) error
}

Conn defined a connection with remote peer.

type ConnHandler

type ConnHandler func(conn Conn) (bool, error)

ConnHandler is a function for handling connections.

type Dialer

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

Dialer provides a way to establish a connection with others.

type Direction

type Direction int

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

String returns a string representation of the connection direction

type Listener

type Listener interface {
	io.Closer
	// Listen will run a task that start create listeners with the given
	// addresses waiting for accepting inbound connections.
	Listen(ctx context.Context, addresses ...ma.Multiaddr) error
	// ListenAddresses return the list of the local addresses for listeners.
	ListenAddresses() []ma.Multiaddr
	// RelayListen continuously monitor relay communication
	RelayListen(listener net.Listener)
}

Listener provides a way to accept connections established with others.

type Network

type Network interface {
	Dialer
	Listener
	io.Closer
	// SetNewConnHandler register a ConnHandler to handle the connection established.
	SetNewConnHandler(handler ConnHandler)
	// Disconnect a connection.
	Disconnect(conn Conn) error
	// Closed return whether network closed.
	Closed() bool
	// LocalPeerID return the local peer id.
	LocalPeerID() peer.ID
	// RelayDial using conn and addr of the relay peer, get the relay conn
	RelayDial(conn Conn, rAddr ma.Multiaddr) (Conn, error)
	// GetTempListenAddresses return the local addr dial success
	GetTempListenAddresses() []ma.Multiaddr
	// AddTempListenAddresses add the remote addr accept success
	AddTempListenAddresses([]ma.Multiaddr)
	// DirectListen listen without check and support listen repeat
	DirectListen(ctx context.Context, addrs ...ma.Multiaddr) error
}

Network is a state machine interface 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 defined a way to receive data.

type SendStream

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

SendStream is an interface defined a way to send data.

type Stat

type Stat interface {
	// Direction return the direction of the connection
	Direction() Direction
	// EstablishedTime return the time the connection was established
	EstablishedTime() time.Time
	// Extra return information in the state
	Extra() map[interface{}]interface{}
	// SetClosed set to close
	SetClosed()
	// IsClosed return whether it is closed
	IsClosed() bool
}

Stat is an interface for storing metadata of a Conn or a Stream.

type Stream

type Stream interface {
	SendStream
	ReceiveStream
}

Stream is an interface defined both ways to send and receive data.

Jump to

Keyboard shortcuts

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