p2p

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2018 License: LGPL-3.0 Imports: 25 Imported by: 0

Documentation

Overview

Package p2p implements the Lemochain p2p network protocols.

Index

Constants

View Source
const (
	MaxReconnectCount int8 = 5
	MaxNodeCount           = 200

	WhiteFile = "whitelist"
	FindFile  = "findnode"
)
View Source
const NodeIDBits = 512

Variables

View Source
var (
	ErrMaxReconnect  = errors.New("reconnect has reached max count")
	ErrNoSpecialNode = errors.New("doesn't have this special node")
	ErrHasStared     = errors.New("has been started")
	ErrNotStart      = errors.New("not start")
)
View Source
var (
	ErrConnectSelf     = fmt.Errorf("can't connect yourself")
	ErrGenesisNotMatch = fmt.Errorf("can't match genesis block")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// private key
	PrivateKey *ecdsa.PrivateKey

	// max accept connection count
	MaxPeerNum int

	// 最大连接中的节点数
	MaxPendingPeerNum int // reserve

	// server's Name
	Name string

	// 黑名单
	NetRestrict *Netlist

	// 节点数据库路径
	NodeDatabase string

	// listen port
	Port int
}

Config holds Server options.

func (*Config) ListenAddr

func (config *Config) ListenAddr() string

type DialManager added in v1.0.1

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

func NewDialManager added in v1.0.1

func NewDialManager(handleConn HandleConnFunc, discover *DiscoverManager) *DialManager

func (*DialManager) Start added in v1.0.1

func (m *DialManager) Start() error

func (*DialManager) Stop added in v1.0.1

func (m *DialManager) Stop() error

type DiscoverManager added in v1.0.1

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

func NewDiscoverManager added in v1.0.1

func NewDiscoverManager(datadir string) *DiscoverManager

func (*DiscoverManager) AddNewList added in v1.0.1

func (m *DiscoverManager) AddNewList(nodes []string)

func (*DiscoverManager) GetNodesForDiscover added in v1.0.1

func (m *DiscoverManager) GetNodesForDiscover(sequence uint) []string

func (*DiscoverManager) SetConnectResult added in v1.0.1

func (m *DiscoverManager) SetConnectResult(node string, success bool) error

SetConnectResult set connect result

func (*DiscoverManager) SetDeputyNodes added in v1.0.1

func (m *DiscoverManager) SetDeputyNodes(nodes []string)

SetDeputyNodes add deputy nodes

func (*DiscoverManager) SetReconnect added in v1.0.1

func (m *DiscoverManager) SetReconnect(node string) error

SetReconnect start reconnect

func (*DiscoverManager) Start added in v1.0.1

func (m *DiscoverManager) Start() error

func (*DiscoverManager) Stop added in v1.0.1

func (m *DiscoverManager) Stop() error

type HandleConnFunc added in v1.0.1

type HandleConnFunc func(fd net.Conn, isSelfServer bool) error

type Msg

type Msg struct {
	Code       uint32
	Size       uint32 // size of the paylod
	Payload    io.Reader
	ReceivedAt time.Time
}

func (Msg) CheckCode

func (msg Msg) CheckCode() bool

CheckCode 检测code是否合法

func (Msg) Decode

func (msg Msg) Decode(data interface{}) error

Decode 将msg实际有效内容解码

func (Msg) Empty

func (msg Msg) Empty() bool

Empty 判断Msg是否为空消息

type Netlist

type Netlist []net.IPNet

Netlist is a list of IP networks.

func (*Netlist) Contains

func (l *Netlist) Contains(ip net.IP) bool

Contains reports whether the given IP is contained in the list.

type Node

type Node struct {
	IP       net.IP // len 4 for IPv4 or 16 for IPv6
	UDP, TCP uint16 // port numbers
	ID       NodeID // the node's public key
	// contains filtered or unexported fields
}

Node 一个网络节点

func NewNode

func NewNode(id NodeID, ip net.IP, udpPort, tcpPort uint16) *Node

NewNode 创建一个新的网络节点

type NodeID

type NodeID [NodeIDBits / 8]byte

NodeID 节点唯一编码

func PubkeyID

func PubkeyID(pub *ecdsa.PublicKey) NodeID

PubkeyID returns a marshaled representation of the given public key.

func (NodeID) PubKey

func (id NodeID) PubKey() (*ecdsa.PublicKey, error)

PubKey 根据NodeID获取公钥

func (NodeID) String

func (id NodeID) String() string

String 获取nodeid的string形式

type Peer

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

Peer represents a connected remote node.

func (*Peer) Close

func (p *Peer) Close()

func (*Peer) DisableReConnect

func (p *Peer) DisableReConnect()

func (*Peer) NeedReConnect

func (p *Peer) NeedReConnect() bool

func (*Peer) NodeID

func (p *Peer) NodeID() NodeID

获取Peer ID

func (*Peer) ReadMsg

func (p *Peer) ReadMsg() Msg

对外提供 供读取节点数据用

func (*Peer) RemoteAddr added in v1.0.1

func (p *Peer) RemoteAddr() string

func (*Peer) WriteMsg

func (p *Peer) WriteMsg(code uint32, content []byte) error

对外提供 提供写入数据到节点

type PeerConnInfo

type PeerConnInfo struct {
	LocalAddr  string `json:"localAddress"`
	RemoteAddr string `json:"remoteAddress"`
	NodeID     string `json:"nodeID"`
}

func (PeerConnInfo) MarshalJSON

func (p PeerConnInfo) MarshalJSON() ([]byte, error)

MarshalJSON marshals as JSON.

func (*PeerConnInfo) UnmarshalJSON

func (p *PeerConnInfo) UnmarshalJSON(input []byte) error

UnmarshalJSON unmarshals from JSON.

type PeerEventFlag

type PeerEventFlag int

对外节点通知类型

const (
	AddPeerFlag PeerEventFlag = iota
	DropPeerFlag
)

type PeerEventFn

type PeerEventFn func(peer *Peer, flag PeerEventFlag) error

type RawNode added in v1.0.1

type RawNode struct {
	NodeID      string
	Endpoint    string
	IsReconnect bool
	ConnCounter int8
	Sequence    int32 // fresh: >0; stale: <0; connecting: 0
}

type Server

type Server struct {
	Config // server的一些基本配置
	// contains filtered or unexported fields
}

Server manages all peer connections.

func NewServer added in v1.0.1

func NewServer(config Config, discover *DiscoverManager) *Server

func (*Server) Connect

func (srv *Server) Connect(node string)

func (*Server) Connections

func (srv *Server) Connections() []PeerConnInfo

func (*Server) Disconnect

func (srv *Server) Disconnect(node string) bool

func (*Server) HandleConn

func (srv *Server) HandleConn(fd net.Conn, isSelfServer bool) error

处理接收到的连接 服务端客户端均走此函数 isSelfServer == true ? server : client

func (*Server) Start

func (srv *Server) Start() error

启动服务器

func (*Server) Stop

func (srv *Server) Stop()

Jump to

Keyboard shortcuts

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