p2p

package
v1.2.3-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2019 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxPeers        uint = 50
	DefaultMaxPendingPeers uint = 20
	DefaultMaxInboundRatio uint = 2
	DefaultPort            uint = 8483
	DefaultNetID                = network.Aquarius
	DefaultMinPeers             = 5
)
View Source
const DefaultAddr = "0.0.0.0:8483"
View Source
const Dirname = "p2p"

Variables

This section is empty.

Functions

func WriteMsg

func WriteMsg(writer io.Writer, msg *Msg) (err error)

Types

type Cmd

type Cmd = uint16

type CmdSet

type CmdSet = uint32

@section CmdSetID

type Config

type Config struct {
	Discovery       bool
	Name            string
	NetID           network.ID         // which network server runs on
	MaxPeers        uint               // max peers can be connected
	MaxPendingPeers uint               // max peers waiting for connect
	MaxInboundRatio uint               // max inbound peers: MaxPeers / MaxInboundRatio
	Addr            string             // TCP and UDP listen port
	DataDir         string             // the directory for storing node table, default is "~/viteisbest/p2p"
	PeerKey         ed25519.PrivateKey // use for encrypt message, the corresponding public key use for NodeID
	ExtNodeData     []byte             // extension data for Node
	Protocols       []*Protocol        // protocols server supported
	BootNodes       []string           // nodes as discovery seed
	StaticNodes     []string           // nodes to connect
}

Config is the essential configuration to create a p2p.server

func EnsureConfig

func EnsureConfig(cfg *Config) *Config

type ConnProperty

type ConnProperty struct {
	LocalID    string `json:"localID"`
	LocalIP    net.IP `json:"localIP"`
	LocalPort  uint16 `json:"localPort"`
	RemoteID   string `json:"remoteID"`
	RemoteIP   net.IP `json:"remoteIP"`
	RemotePort uint16 `json:"remotePort"`
}

@section ConnProperty

func (*ConnProperty) Deproto

func (cp *ConnProperty) Deproto(pb *protos.ConnProperty)

func (*ConnProperty) Deserialize

func (cp *ConnProperty) Deserialize(buf []byte) error

func (*ConnProperty) Proto

func (cp *ConnProperty) Proto() *protos.ConnProperty

func (*ConnProperty) Serialize

func (cp *ConnProperty) Serialize() ([]byte, error)

type DiscReason

type DiscReason uint64

@section peer error

const (
	DiscRequested DiscReason = iota + 1
	DiscNetworkError
	DiscAllProtocolDone
	DiscProtocolError
	DiscUselessPeer
	DiscTooManyPeers
	DiscTooManyInboundPeers
	DiscAlreadyConnected
	DiscIncompatibleVersion
	DiscInvalidIdentity
	DiscQuitting
	DiscSelf
	DiscReadTimeout
	DiscReadError
	DiscResponseTimeout
	DiscUnKnownProtocol
	DiscHandshakeFail
)

func DeserializeDiscReason

func DeserializeDiscReason(buf []byte) (reason DiscReason, err error)

func (DiscReason) Deserialize

func (d DiscReason) Deserialize(buf []byte) error

just implement Serializable interface

func (DiscReason) Error

func (d DiscReason) Error() string

func (DiscReason) Serialize

func (d DiscReason) Serialize() ([]byte, error)

func (DiscReason) String

func (d DiscReason) String() string

type Handshake

type Handshake struct {
	// peer name, use for readability and log
	Name string
	// peer remoteID
	ID discovery.NodeID
	// command set supported
	CmdSets []CmdSet
	// peer`s IP
	RemoteIP net.IP
	// peer`s Port
	RemotePort uint16
	// tell peer our tcp listen port, could use for update discovery info
	Port uint16
}

handshake message

func (*Handshake) Deserialize

func (hs *Handshake) Deserialize(buf []byte) error

func (*Handshake) Serialize

func (hs *Handshake) Serialize() ([]byte, error)

type Msg

type Msg struct {
	CmdSet     CmdSet
	Cmd        Cmd
	Id         uint64 // as message context
	Payload    []byte
	ReceivedAt time.Time
	SendAt     time.Time
}

@section Msg

func NewMsg

func NewMsg() *Msg

func PackMsg

func PackMsg(cmdSetId CmdSet, cmd Cmd, id uint64, s Serializable) (*Msg, error)

func ReadMsg

func ReadMsg(reader io.Reader) (msg *Msg, err error)

func (*Msg) Recycle

func (msg *Msg) Recycle()

type MsgReadWriter

type MsgReadWriter interface {
	MsgReader
	MsgWriter
}

type MsgReader

type MsgReader interface {
	ReadMsg() (*Msg, error)
}

type MsgWriter

type MsgWriter interface {
	WriteMsg(*Msg) error
}

type NodeInfo

type NodeInfo struct {
	ID        string     `json:"remoteID"`
	Name      string     `json:"name"`
	Url       string     `json:"url"`
	NetID     network.ID `json:"netId"`
	Address   *address   `json:"address"`
	Protocols []string   `json:"protocols"`
}

NodeInfo represent current p2p node

type P2PVersion

type P2PVersion = uint32
const Version P2PVersion = 1

type Peer

type Peer struct {
	Created time.Time
	// contains filtered or unexported fields
}

func NewPeer

func NewPeer(conn *transport, ourSet []*Protocol) (*Peer, error)

func (*Peer) CmdSets

func (p *Peer) CmdSets() []CmdSet

func (*Peer) Disconnect

func (p *Peer) Disconnect(reason DiscReason)

func (*Peer) GetConnProperty

func (p *Peer) GetConnProperty() *ConnProperty

func (*Peer) ID

func (p *Peer) ID() discovery.NodeID

func (*Peer) IP

func (p *Peer) IP() net.IP

func (*Peer) Info

func (p *Peer) Info() *PeerInfo

func (*Peer) Name

func (p *Peer) Name() string

func (*Peer) RemoteAddr

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

func (*Peer) String

func (p *Peer) String() string

type PeerInfo

type PeerInfo struct {
	ID      string   `json:"id"`
	Name    string   `json:"name"`
	CmdSets []string `json:"cmdSets"`
	Address string   `json:"address"`
	Inbound bool     `json:"inbound"`
}

@section PeerInfo

type PeerSet

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

@section PeerSet

func NewPeerSet

func NewPeerSet() *PeerSet

func (*PeerSet) Add

func (s *PeerSet) Add(p *Peer)

func (*PeerSet) Del

func (s *PeerSet) Del(p *Peer)

func (*PeerSet) DisconnectAll added in v1.2.0

func (s *PeerSet) DisconnectAll()

func (*PeerSet) Has

func (s *PeerSet) Has(id discovery.NodeID) bool

func (*PeerSet) Info

func (s *PeerSet) Info() []*PeerInfo

func (*PeerSet) Size

func (s *PeerSet) Size() int

type Plugin added in v1.2.0

type Plugin interface {
	Start(p2p Server) error
	Stop()
}

type ProtoFrame

type ProtoFrame struct {
	*Protocol

	Received  map[Cmd]uint64
	Discarded map[Cmd]uint64
	Send      map[Cmd]uint64
	// contains filtered or unexported fields
}

func (*ProtoFrame) ReadMsg

func (pf *ProtoFrame) ReadMsg() (msg *Msg, err error)

func (*ProtoFrame) WriteMsg

func (pf *ProtoFrame) WriteMsg(msg *Msg) (err error)

type Protocol

type Protocol struct {
	// description of the protocol
	Name string
	// use for message command set, should be unique
	ID CmdSet
	// read and write Msg with rw
	Handle func(p *Peer, rw *ProtoFrame) error
}

@section protocol

func (*Protocol) String

func (p *Protocol) String() string

type Serializable

type Serializable interface {
	Serialize() ([]byte, error)
	Deserialize(buf []byte) error
}

type Server

type Server interface {
	Start() error
	Stop()
	AddPlugin(plugin Plugin)
	Connect(id discovery.NodeID, addr *net.TCPAddr)
	Peers() []*PeerInfo
	PeersCount() uint
	NodeInfo() *NodeInfo
	Available() bool
	Nodes() (urls []string)
	SubNodes(ch chan<- *discovery.Node)
	UnSubNodes(ch chan<- *discovery.Node)
	URL() string
	Config() *Config
	Block(id discovery.NodeID, ip net.IP, err error)
}

func New

func New(cfg *Config) (Server, error)

type Transport

type Transport interface {
	MsgReadWriter
	Close()
	Handshake(our *Handshake) (their *Handshake, err error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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