peer

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2019 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPeerDisconnected is the error to return when attempt to send message
	// to peer, but the peer was disconnected.
	ErrPeerDisconnected = errors.New("peer already disconnected")
)

Functions

func DisableLog

func DisableLog()

DisableLog disables all library log output. Logging output is disabled by default until either UseLogger or SetLogWriter are called.

func RandomUint64

func RandomUint64() uint64

RandomUint64 creates a random uint64 number.

func UseLogger

func UseLogger(logger elalog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using elalog.

Types

type Config

type Config struct {
	PID              PID
	Magic            uint32
	ProtocolVersion  uint32
	Services         uint64
	PingInterval     time.Duration
	SignNonce        func(nonce []byte) (signature [64]byte)
	PingNonce        func(pid PID) uint64
	PongNonce        func(pid PID) uint64
	MakeEmptyMessage func(cmd string) (p2p.Message, error)
	// contains filtered or unexported fields
}

Config is a descriptor which specifies the peer instance configuration.

func (*Config) AddMessageFunc

func (c *Config) AddMessageFunc(messageFunc MessageFunc)

type HostToNetAddrFunc

type HostToNetAddrFunc func(host string, port uint16, services uint64) (*p2p.NetAddress, error)

HostToNetAddrFunc is a func which takes a host, port, services and returns the netaddress.

type MessageFunc

type MessageFunc func(peer *Peer, msg p2p.Message)

MessageFunc is a message handler in peer's configuration

type PID

type PID [33]byte

PID is the encoded public key data used as peer's ID.

func (PID) Equal

func (p PID) Equal(o PID) bool

Equal returns if the passed PID is equal to origin PID.

type Peer

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

func NewInboundPeer

func NewInboundPeer(cfg *Config) *Peer

NewInboundPeer returns a new inbound peer. Use Start to begin processing incoming and outgoing messages.

func NewOutboundPeer

func NewOutboundPeer(cfg *Config, addr string) (*Peer, error)

NewOutboundPeer returns a new outbound peer.

func (*Peer) AddMessageFunc

func (p *Peer) AddMessageFunc(messageFunc MessageFunc)

AddMessageFunc add a new message handler for the peer.

func (*Peer) Addr

func (p *Peer) Addr() string

Addr returns the peer address.

This function is safe for concurrent access.

func (*Peer) AssociateConnection

func (p *Peer) AssociateConnection(conn net.Conn)

AssociateConnection associates the given conn to the peer. Calling this function when the peer is already connected will have no effect.

func (*Peer) Connected

func (p *Peer) Connected() bool

Connected returns whether or not the peer is currently connected.

This function is safe for concurrent access.

func (*Peer) Disconnect

func (p *Peer) Disconnect()

Disconnect disconnects the peer by closing the connection. Calling this function when the peer is already disconnected or in the process of disconnecting will have no effect.

func (*Peer) ID

func (p *Peer) ID() uint64

ID returns the peer id.

This function is safe for concurrent access.

func (*Peer) InQuit

func (p *Peer) InQuit() <-chan struct{}

InQuit returns the signal chan of message inHandler quit.

func (*Peer) Inbound

func (p *Peer) Inbound() bool

Inbound returns whether the peer is inbound.

This function is safe for concurrent access.

func (*Peer) LastPingMicros

func (p *Peer) LastPingMicros() int64

LastPingMicros returns the last ping micros of the remote peer.

This function is safe for concurrent access.

func (*Peer) LastPingTime

func (p *Peer) LastPingTime() time.Time

LastPingTime returns the last ping time of the remote peer.

This function is safe for concurrent access.

func (*Peer) LastRecv

func (p *Peer) LastRecv() time.Time

LastRecv returns the last recv time of the peer.

This function is safe for concurrent access.

func (*Peer) LastSend

func (p *Peer) LastSend() time.Time

LastSend returns the last send time of the peer.

This function is safe for concurrent access.

func (*Peer) LocalAddr

func (p *Peer) LocalAddr() net.Addr

LocalAddr returns the local address of the connection.

This function is safe fo concurrent access.

func (*Peer) NA

func (p *Peer) NA() *p2p.NetAddress

NA returns the peer network address.

This function is safe for concurrent access.

func (*Peer) OutQuit

func (p *Peer) OutQuit() <-chan struct{}

OutQuit returns the signal chan of message outHandler quit.

func (*Peer) PID

func (p *Peer) PID() PID

PID returns the peer public key id.

This function is safe for concurrent access.

func (*Peer) PK added in v0.3.0

func (p *Peer) PK() *crypto.PublicKey

PK returns the peer public key.

This function is safe for concurrent access.

func (*Peer) ProtocolVersion

func (p *Peer) ProtocolVersion() uint32

ProtocolVersion returns the negotiated peer protocol version.

This function is safe for concurrent access.

func (*Peer) Quit

func (p *Peer) Quit() <-chan struct{}

Quit returns the signal chan of peer quit.

func (*Peer) SendMessage

func (p *Peer) SendMessage(msg p2p.Message, doneChan chan<- error)

func (*Peer) Services

func (p *Peer) Services() uint64

Services returns the services flag of the remote peer.

This function is safe for concurrent access.

func (*Peer) StatsSnapshot

func (p *Peer) StatsSnapshot() *StatsSnap

StatsSnapshot returns a snapshot of the current peer flags and statistics.

This function is safe for concurrent access.

func (*Peer) String

func (p *Peer) String() string

String returns the peer's address and directionality as a human-readable string.

This function is safe for concurrent access.

func (*Peer) TimeConnected

func (p *Peer) TimeConnected() time.Time

TimeConnected returns the time at which the peer connected.

This function is safe for concurrent access.

func (*Peer) WaitForDisconnect

func (p *Peer) WaitForDisconnect()

WaitForDisconnect waits until the peer has completely disconnected and all resources are cleaned up. This will happen if either the local or remote side has been disconnected or the peer is forcibly disconnected via Disconnect.

type StatsSnap

type StatsSnap struct {
	ID             uint64
	PID            string
	Addr           string
	Services       uint64
	LastSend       time.Time
	LastRecv       time.Time
	ConnTime       time.Time
	Version        uint32
	Inbound        bool
	LastPingTime   time.Time
	LastPingMicros int64
}

StatsSnap is a snapshot of peer stats at a point in time.

Jump to

Keyboard shortcuts

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