p2p

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2019 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ConnectionTimeout is the duration we timeout peer connections.
	ConnectionTimeout = time.Second * 30
)

Variables

View Source
var (
	// ErrSendMessageFailed is the error to reply when send message to peer
	// failed.
	ErrSendMessageFailed = errors.New("send message failed")
)

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 NewServer

func NewServer(origCfg *Config) (*server, error)

NewServer returns a new server instance by the given config. Use start to begin accepting connections from peers.

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 {
	// DataDir is the data path to store peer addresses etc.
	DataDir string

	// PID is the public key id of this server.
	PID peer.PID

	// EnableHub indicates whether or not to enable the hub service.
	EnableHub bool

	// Localhost represents the local host IP or name of this peer.
	Localhost string

	// MagicNumber is the peer-to-peer network ID to connect to.
	MagicNumber uint32

	// DefaultPort defines the default peer-to-peer port for the network.
	DefaultPort uint16

	// TimeSource defines the median time source to use for things such as
	// view changing.
	TimeSource dtime.MedianTimeSource

	// ConnectTimeout is the duration before we timeout a dial to peer.
	ConnectTimeout time.Duration

	// PingInterval is the interval of time to wait in between sending ping
	// messages.
	PingInterval time.Duration

	// Sign will be invoked when creating a signature of the data content.
	Sign func(data []byte) (signature []byte)

	// PingNonce will be invoked before send a ping message to the connect peer
	// with the given PID, to get the nonce value within the ping message.
	PingNonce func(pid peer.PID) uint64

	// PongNonce will be invoked before send a pong message to the connect peer
	// with the given PID, to get the nonce value within the pong message.
	PongNonce func(pid peer.PID) uint64

	// MakeEmptyMessage will be invoked to creates a message of the appropriate
	// concrete type based on the command.
	MakeEmptyMessage func(command string) (p2p.Message, error)

	// HandleMessage will be invoked to handle the received message from
	// connected peers.  The peer's public key id will be pass together with
	// the received message.
	HandleMessage func(pid peer.PID, msg p2p.Message)

	// StateNotifier notifies the server peer state changes.
	StateNotifier StateNotifier
}

Config is a descriptor which specifies the server instance configuration.

type ConnState added in v0.3.0

type ConnState uint8

ConnState indicates the peer connection state.

const (
	// CSNoneConnection indicates the peer has no connection.
	CSNoneConnection ConnState = iota

	// CSOutboundOnly indicates the peer has outbound connection only.
	CSOutboundOnly

	// CSInboundOnly indicates the peer has inbound connection only.
	CSInboundOnly

	// CS2WayConnection indicates the peer have both inbound and outbound
	// connections.
	CS2WayConnection
)

func (ConnState) String added in v0.3.0

func (cs ConnState) String() string

String returns the ConnState in human-readable form.

type Notifier

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

func NewNotifier

func NewNotifier(flags NotifyFlag, notify func(flag NotifyFlag)) *Notifier

func (*Notifier) OnConnectPeers

func (n *Notifier) OnConnectPeers(peers []peer.PID)

func (*Notifier) OnDonePeer

func (n *Notifier) OnDonePeer(pid peer.PID)

func (*Notifier) OnNewPeer

func (n *Notifier) OnNewPeer(pid peer.PID)

type NotifyFlag

type NotifyFlag int64

NotifyFlag identifies notifies should be callback.

const (
	// NFNetStabled is a flag to indicate network stabled.
	NFNetStabled NotifyFlag = 1 << iota

	// NFBadNetwork is a flag to indicate network unstable.
	NFBadNetwork
)

func (NotifyFlag) String

func (f NotifyFlag) String() string

String returns the NotifyFlag in human-readable form.

type Peer

type Peer interface {
	// PID returns the peer's public key id.
	PID() peer.PID

	// ToPeer returns the real peer instance.
	ToPeer() *peer.Peer
}

Peer represent the connected peer.

type PeerAddr

type PeerAddr struct {
	// PID is the peer's public key id.
	PID peer.PID

	// Addr is the peer's network address in host:port format.
	Addr string
}

PeerAddr represent a DPOS peer's ID and it's network address

type PeerInfo added in v0.3.0

type PeerInfo struct {
	// PID is the peer's public key id.
	PID peer.PID

	// Addr is the peer's IP address.  It can be host:port format,
	// or host only and use the DefaultPort passed by server config.
	Addr string

	// State is the peer's connection state.
	State ConnState
}

PeerInfo represent the peer info of the connect peers.

type Server

type Server interface {
	// Start begins accepting connections from peers.
	Start()

	// Stop gracefully shuts down the server by stopping and disconnecting all
	// peers and the main listener.
	Stop() error

	// AddAddr adds an arbiter address into AddrManager.
	AddAddr(pid peer.PID, addr string)

	// ConnectPeers let server connect the peers in the given list, and
	// disconnect peers that not in the list.
	ConnectPeers(peers []peer.PID)

	// SendMessageToPeer send a message to the peer with the given id, error
	// will be returned if there is no matches, or fail to send the message.
	SendMessageToPeer(pid peer.PID, msg p2p.Message) error

	// BroadcastMessage sends msg to all peers currently connected to the server
	// except those in the passed peers to exclude.
	BroadcastMessage(msg p2p.Message, exclPeers ...peer.PID)

	// ConnectedPeers returns an array consisting of all connected peers.
	ConnectedPeers() []Peer

	// DumpPeersInfo returns a list of connect peers information.  This is a
	// high cost method, should not be called frequently.
	DumpPeersInfo() []*PeerInfo
}

Server provides a server handling connections to and from peers.

type StateNotifier

type StateNotifier interface {
	// OnConnectPeers will be invoked when server received a connect peers
	// message.
	//
	// Notify: do not modify the invoked list.  It's read only.
	OnConnectPeers(peers []peer.PID)

	// OnNewPeer will be invoked when a new peer negotiated.
	OnNewPeer(pid peer.PID)

	// OnDonePeer will be invoked when a peer disconnected.
	OnDonePeer(pid peer.PID)
}

StateNotifier notifies the server peer state changes.

Directories

Path Synopsis
Conn is a wrapper of the origin network connection.
Conn is a wrapper of the origin network connection.

Jump to

Keyboard shortcuts

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