p2p

package
v0.0.3-chain Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2018 License: GPL-3.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxPeers        uint      = 50
	DefaultMaxPendingPeers uint      = 20
	DefaultMaxInboundRatio uint      = 2
	DefaultPort            uint      = 8483
	DefaultNetID           NetworkID = Aquarius
)
View Source
const P2PDir = "p2p"
View Source
const Version uint64 = 2

Variables

This section is empty.

Functions

func WriteMsg

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

Types

type AsyncMsgConn

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

func NewAsyncMsgConn

func NewAsyncMsgConn(fd net.Conn, handler func(msg *Msg)) *AsyncMsgConn

create an AsyncMsgConn, fd is the basic connection

func (*AsyncMsgConn) Close

func (c *AsyncMsgConn) Close(err error)

func (*AsyncMsgConn) Handshake

func (c *AsyncMsgConn) Handshake(data []byte) (their *Handshake, err error)

send Handshake data, after signature with ed25519 algorithm

func (*AsyncMsgConn) Send

func (c *AsyncMsgConn) Send(cmdset, cmd, id uint64, s Serializable) bool

func (*AsyncMsgConn) SendMsg

func (c *AsyncMsgConn) SendMsg(msg *Msg) bool

send a message asynchronously, put message into a internal buffered channel before send it if the internal channel is full, return false

func (*AsyncMsgConn) Start

func (c *AsyncMsgConn) Start()

type CmdSet

type CmdSet struct {
	ID   uint64
	Name string
}

func (*CmdSet) Deserialize

func (s *CmdSet) Deserialize(buf []byte) error

func (*CmdSet) Proto

func (s *CmdSet) Proto() *protos.CmdSet

func (*CmdSet) Serialize

func (s *CmdSet) Serialize() ([]byte, error)

func (*CmdSet) String

func (s *CmdSet) String() string

type Config

type Config struct {
	Name            string
	NetID           NetworkID          // 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
	Port            uint               // TCP and UDP listen port
	Database        string             // the directory for storing node table
	PrivateKey      ed25519.PrivateKey // use for encrypt message, the corresponding public key use for NodeID
	Protocols       []*Protocol        // protocols server supported
	BootNodes       []string
}

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"`
}

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
	DiscNetworkError
	DiscProtocolError
	DiscUselessPeer
	DiscTooManyPeers
	DiscTooManyPassivePeers
	DiscAlreadyConnected
	DiscIncompatibleVersion
	DiscInvalidIdentity
	DiscQuitting
	DiscUnexpectedIdentity
	DiscSelf
	DiscReadTimeout
	DiscResponseTimeout
	DiscSubprotocolError = 0x10
)

func DeserializeDiscReason

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

func ReadDiscReason

func ReadDiscReason(r io.Reader) (DiscReason, 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 Discovery

type Discovery interface {
	Lookup(discovery.NodeID) []*discovery.Node
	Resolve(discovery.NodeID) *discovery.Node
	RandomNodes([]*discovery.Node) int
	Start()
	Stop()
	SubNodes(ch chan<- *discovery.Node)
}

type Handshake

type Handshake struct {
	Version uint64
	// peer name, use for readability and log
	Name string
	// running at which network
	NetID NetworkID
	// peer remoteID
	ID discovery.NodeID
	// command set supported
	CmdSets []*CmdSet
	// peer`s IP
	RemoteIP net.IP
	// peer`s Port
	RemotePort 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 {
	CmdSetID   uint64
	Cmd        uint64
	Id         uint64 // as message context
	Size       uint64 // how many bytes in payload, used to quickly determine whether payload is valid
	Payload    []byte
	ReceivedAt time.Time
}

@section Msg

func PackMsg

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

func ReadMsg

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

func (*Msg) Discard

func (msg *Msg) Discard() (err error)

func (*Msg) String

func (msg *Msg) String() string

type MsgReadWriter

type MsgReadWriter interface {
	MsgReader
	MsgWriter
}

type MsgReader

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

type MsgRw

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

the most basic Msg reader & writer, Thread unsafe

func (*MsgRw) ReadMsg

func (rw *MsgRw) ReadMsg() (msg *Msg, err error)

func (*MsgRw) WriteMsg

func (rw *MsgRw) WriteMsg(msg *Msg) (err error)

type MsgWriter

type MsgWriter interface {
	WriteMsg(*Msg) error
}

type NetworkID

type NetworkID uint64

@section NetworkID

const (
	MainNet NetworkID = iota + 1
	TestNet
	Aquarius
	Pisces
	Aries
	Taurus
	Gemini
	Cancer
	Leo
	Virgo
	Libra
	Scorpio
	Sagittarius
	Capricorn
)

func (NetworkID) String

func (i NetworkID) String() string

type NodeInfo

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

@section NodeInfo

type Peer

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

@section Peer

func NewPeer

func NewPeer(conn *conn, 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:"remoteID"`
	Name    string   `json:"name"`
	CmdSets []string `json:"caps"`
	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) error

func (*PeerSet) Clear

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

func (*PeerSet) Del

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

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

func (*PeerSet) Traverse

func (s *PeerSet) Traverse(fn func(id discovery.NodeID, p *Peer))

type Protocol

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

@section protocol

func (*Protocol) CmdSet

func (p *Protocol) CmdSet() *CmdSet

func (*Protocol) String

func (p *Protocol) String() string

type Serializable

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

type Server

type Server struct {
	*Config

	BootNodes []*discovery.Node
	// contains filtered or unexported fields
}

func New

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

func (*Server) Available

func (svr *Server) Available() bool

func (*Server) NodeInfo

func (svr *Server) NodeInfo() *NodeInfo

func (*Server) Peers

func (svr *Server) Peers() []*PeerInfo

func (*Server) PeersCount

func (svr *Server) PeersCount() (amount int)

func (*Server) Start

func (svr *Server) Start() error

func (*Server) Stop

func (svr *Server) Stop()

func (*Server) URL

func (svr *Server) URL() string

type Task

type Task interface {
	Perform(a *agent)
}

@section Task

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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