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.
Types ¶
type Config ¶
type Config struct {
// PID is the public key id of this server.
PID peer.PID
// MagicNumber is the peer-to-peer network ID to connect to.
MagicNumber uint32
// ProtocolVersion represent the protocol version you are supporting.
ProtocolVersion uint32
// Services represent which services you are supporting.
Services uint64
// DefaultPort defines the default peer-to-peer port for the network.
DefaultPort uint16
// 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
// SignNonce will be invoked when creating a version message to do the
// protocol negotiate. The passed nonce is a 32 bytes length random value,
// and returns the signature of the nonce value to proof you have the right
// of the PID(public key) you've provided.
SignNonce func(nonce []byte) (signature [64]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 )
type Notifier ¶
type Notifier struct {
// contains filtered or unexported fields
}
func NewNotifier ¶
func NewNotifier(flags NotifyFlag, notify func(flag NotifyFlag)) *Notifier
func (*Notifier) OnDonePeer ¶
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 IP address. It can be host:port format,
// or host only and use the DefaultPort passed by server config.
Addr string
}
PeerAddr represent a connect peer's ID and it's IP 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
// ConnectPeers let server connect the peers in the given addrList, and
// disconnect peers that not in the addrList.
ConnectPeers(addrList []PeerAddr)
// 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 addr list. It's read only.
OnConnectPeers(addrList map[peer.PID]PeerAddr)
// 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.
Click to show internal directories.
Click to hide internal directories.