p2p

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2019 License: LGPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewNodeSet

func NewNodeSet() *nodeSet

NewNodeSet creates new nodeSet

func NewPeerSet

func NewPeerSet() *peerSet

NewPeerSet returns peerSet pointer

func SendMessage

func SendMessage(writer MsgWriter, code uint16, payload []byte) error

SendMessage send message to peer

Types

type Cap

type Cap struct {
	Name    string
	Version uint
}

Cap is the structure of a peer capability.

func (Cap) String

func (cap Cap) String() string

type Config

type Config struct {
	// p2p.server will listen for incoming tcp connections. And it is for udp address used for Kad protocol
	ListenAddr string `json:"address"`

	// NetworkID used to define net type, for example main net and test net.
	NetworkID string `json:"networkID"`

	// static nodes which will be connected to find more nodes when the node started
	StaticNodes []*discovery.Node `json:"staticNodes"`

	// SubPrivateKey which will be make PrivateKey
	SubPrivateKey string `json:"privateKey"`

	// PrivateKey private key for p2p module, do not use it as any accounts
	PrivateKey *ecdsa.PrivateKey `json:"-"`
}

Config is the Configuration of p2p

type Message

type Message struct {
	Code       uint16 // message code, defined in each protocol
	Payload    []byte
	ReceivedAt time.Time
}

Message exposed for high level layer to receive

func (*Message) UnZip

func (msg *Message) UnZip() error

UnZip the message whether it is compressed or not.

func (*Message) Zip

func (msg *Message) Zip() error

Zip compress message when the length of payload is greater than zipBytesLimit

type MsgReadWriter

type MsgReadWriter interface {
	MsgReader
	MsgWriter
}

MsgReadWriter provides reading and writing of encoded messages. Implementations should ensure that ReadMsg and WriteMsg can be called simultaneously from multiple goroutines.

type MsgReader

type MsgReader interface {
	// ReadMsg read a message. It will block until send the message out or get errors
	ReadMsg() (*Message, error)
}

MsgReader interface

type MsgWriter

type MsgWriter interface {
	// WriteMsg sends a message. It will block until the message's
	// Payload has been consumed by the other end.
	//
	// Note that messages can be sent only once because their
	// payload reader is drained.
	WriteMsg(*Message) error
}

MsgWriter interface

type Peer

type Peer struct {
	Node *discovery.Node // remote peer that this peer connects
	// contains filtered or unexported fields
}

Peer represents a connected remote node.

func NewPeer

func NewPeer(conn *connection, log *log.SeeleLog, node *discovery.Node) *Peer

NewPeer creates and returns a new peer.

func (*Peer) Disconnect

func (p *Peer) Disconnect(reason string)

Disconnect terminates the peer connection with the given reason. It returns immediately and does not wait until the connection is closed.

func (*Peer) Info

func (p *Peer) Info() *PeerInfo

Info returns data of the peer but not contain id and name.

func (*Peer) LocalAddr

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

LocalAddr returns the local address of the network connection.

func (*Peer) RemoteAddr

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

RemoteAddr returns the remote address of the network connection.

type PeerInfo

type PeerInfo struct {
	ID      string   `json:"id"`   // Unique of the node
	Caps    []string `json:"caps"` // Sum-protocols advertised by this particular peer
	Network struct {
		LocalAddress  string `json:"localAddress"`  // Local endpoint of the TCP data connection
		RemoteAddress string `json:"remoteAddress"` // Remote endpoint of the TCP data connection
	} `json:"network"`
	Protocols map[string]interface{} `json:"protocols"` // Sub-protocol specific metadata fields
	Shard     uint                   `json:"shard"`     // shard id of the node
}

PeerInfo represents a short summary of a connected peer

type PeerInfos

type PeerInfos []PeerInfo

PeerInfos array of PeerInfo for sort alphabetically by node identifier

func (PeerInfos) Len

func (p PeerInfos) Len() int

func (PeerInfos) Less

func (p PeerInfos) Less(i, j int) bool

func (PeerInfos) Swap

func (p PeerInfos) Swap(i, j int)

type ProtoHandShake

type ProtoHandShake struct {
	Caps      []Cap
	NodeID    common.Address
	Params    []byte
	NetworkID string
}

ProtoHandShake handshake message for two peer to exchange base information TODO add public key or other information for encryption?

type Protocol

type Protocol struct {
	// Name should contain the official protocol name,
	// often a three-letter word.
	Name string

	// Version should contain the version number of the protocol.
	Version uint

	// Length should contain the number of message codes used by the protocol.
	Length uint16

	// AddPeer find a new peer will call this method
	AddPeer func(peer *Peer, rw MsgReadWriter) bool

	// DeletePeer this method will be called when a peer is disconnected
	DeletePeer func(peer *Peer)

	// GetPeer this method will be called for get peer information
	GetPeer func(address common.Address) interface{}
}

Protocol base class for high level transfer protocol.

type Server

type Server struct {
	// Config fields may not be modified while the server is running.
	Config

	// MaxPendingPeers is the maximum number of peers that can be pending in the
	// handshake phase, counted separately for inbound and outbound connections.
	// Zero defaults to preset values.
	MaxPendingPeers int

	// Protocols should contain the protocols supported by the server.
	Protocols []Protocol

	SelfNode *discovery.Node
	// contains filtered or unexported fields
}

Server manages all p2p peer connections.

func NewServer

func NewServer(genesis core.GenesisInfo, config Config, protocols []Protocol) *Server

NewServer initialize a server

func (*Server) PeerCount

func (srv *Server) PeerCount() int

PeerCount return the count of peers

func (*Server) PeersInfo

func (srv *Server) PeersInfo() []PeerInfo

PeersInfo returns an array of metadata objects describing connected peers.

func (*Server) SetMaxActiveConnections added in v1.0.4

func (srv *Server) SetMaxActiveConnections(maxActiveConns int)

func (*Server) SetMaxConnections added in v1.0.4

func (srv *Server) SetMaxConnections(maxConns int)

func (*Server) Start

func (srv *Server) Start(nodeDir string, shard uint) (err error)

Start starts the server.

func (*Server) Stop

func (srv *Server) Stop()

Stop terminates the execution of the p2p server

func (*Server) Wait

func (srv *Server) Wait()

Wait wait for server until it exit

Directories

Path Synopsis
* * @file * @copyright defined in go-seele/LICENSE
* * @file * @copyright defined in go-seele/LICENSE

Jump to

Keyboard shortcuts

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