p2p

package
v1.0.1-txapi Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2018 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
)
View Source
const Dirname = "p2p"

Variables

This section is empty.

Functions

func PutUint24

func PutUint24(buf []byte, v uint32)

bigEnd

func Uint24

func Uint24(buf []byte) uint32

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
	Port            uint               // TCP and UDP listen port
	DataDir         string             // the directory for storing node table, default is "~/viteisbest/p2p"
	PrivateKey      ed25519.PrivateKey // use for encrypt message, the corresponding public key use for NodeID
	Protocols       []*Protocol        // protocols server supported
	BootNodes       []string           // nodes as discovery seed
	StaticNodes     []string           // nodes to connect
}

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 Discovery

type Discovery interface {
	Start() error
	Stop()
	SubNodes(ch chan<- *discovery.Node)
	UnSubNodes(ch chan<- *discovery.Node)
	Mark(id discovery.NodeID, lifetime int64)
	Block(id discovery.NodeID, ip net.IP)
	Need(n uint)
	Nodes() []*discovery.Node
}

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

@section NodeInfo

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) Has

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

func (*PeerSet) Info

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

func (*PeerSet) Size

func (s *PeerSet) Size() uint

func (*PeerSet) Traverse

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

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 struct {
	*Config

	StaticNodes []*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) Nodes

func (svr *Server) Nodes() (urls []string)

func (*Server) Peers

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

func (*Server) PeersCount

func (svr *Server) PeersCount() uint

func (*Server) Start

func (svr *Server) Start() error

func (*Server) Stop

func (svr *Server) Stop()

func (*Server) URL

func (svr *Server) URL() string

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