Documentation ¶
Index ¶
- Constants
- Variables
- func GenAccountKey(account string) string
- func GenPeerIDKey(id peer.ID) string
- func GeneratePemKeyFromNetKey(path string) error
- func GetKeyPairFromPath(path string) (crypto.PrivKey, error)
- func GetPeerIDByAddress(peerAddr string) (peer.ID, error)
- func GetPemKeyPairFromPath(path string) (crypto.PrivKey, error)
- func Key(account string) string
- func NewP2PServerV2() netBase.Network
- func NewTLS(path, serviceName string) func(key crypto.PrivKey) (*Transport, error)
- type BucketsFilter
- type BucketsFilterWithFactor
- type MultiStrategy
- type NearestBucketFilter
- type P2PServerV2
- func (p *P2PServerV2) Context() *netBase.NetCtx
- func (p *P2PServerV2) GetPeerIdByAccount(account string) (peer.ID, error)
- func (p *P2PServerV2) HandleMessage(stream netBase.Stream, msg *protos.CoreMessage) error
- func (p *P2PServerV2) Init(ctx *netBase.NetCtx) error
- func (p *P2PServerV2) NewSubscriber(typ protos.CoreMessage_MessageType, v interface{}, ...) netBase.Subscriber
- func (p *P2PServerV2) PeerID() string
- func (p *P2PServerV2) PeerInfo() protos.PeerInfo
- func (p *P2PServerV2) Register(sub netBase.Subscriber) error
- func (p *P2PServerV2) SendMessage(ctx xctx.Context, msg *protos.CoreMessage, optFunc ...netBase.OptionFunc) error
- func (p *P2PServerV2) SendMessageWithResponse(ctx xctx.Context, msg *protos.CoreMessage, optFunc ...netBase.OptionFunc) ([]*protos.CoreMessage, error)
- func (p *P2PServerV2) Start()
- func (p *P2PServerV2) Stop()
- func (p *P2PServerV2) UnRegister(sub netBase.Subscriber) error
- type PeerFilter
- type ReadCloser
- type StaticNodeStrategy
- type StreamImpl
- func (s *StreamImpl) Close()
- func (s *StreamImpl) HandleMessage(msg *protos.CoreMessage) error
- func (s *StreamImpl) LoopRecv()
- func (s *StreamImpl) MultiAddr() multiaddr.Multiaddr
- func (s *StreamImpl) PeerID() string
- func (s *StreamImpl) Send(msg *protos.CoreMessage) error
- func (s *StreamImpl) SendMessage(ctx xctx.Context, msg *protos.CoreMessage) error
- func (s *StreamImpl) SendMessageWithResponse(ctx xctx.Context, msg *protos.CoreMessage) (*protos.CoreMessage, error)
- func (s *StreamImpl) Start()
- func (s *StreamImpl) Valid() bool
- type StreamLimit
- type StreamPool
- func (sp *StreamPool) AddStream(ctx xctx.Context, stream *StreamImpl) error
- func (sp *StreamPool) DelStream(stream *StreamImpl) error
- func (sp *StreamPool) Get(ctx xctx.Context, peerId peer.ID) (*StreamImpl, error)
- func (sp *StreamPool) NewStream(ctx xctx.Context, netStream libnet.Stream) (*StreamImpl, error)
- type Transport
- type WriteCloser
Constants ¶
const ID = "/tls/1.0.0"
ID is the protocol ID (used when negotiating with multistream)
const (
ServerName = "p2pv2"
)
Variables ¶
var ( ErrGenerateOpts = errors.New("generate host opts error") ErrCreateHost = errors.New("create host error") ErrCreateKadDht = errors.New("create kad dht error") ErrCreateStreamPool = errors.New("create stream pool error") ErrCreateBootStrap = errors.New("create bootstrap error pool error") ErrConnectBootStrap = errors.New("error to connect to all bootstrap") ErrLoadAccount = errors.New("load account error") ErrStoreAccount = errors.New("dht store account error") ErrConnect = errors.New("connect all boot and static peer error") ErrEmptyPeer = errors.New("empty peer") ErrNoResponse = errors.New("no response") )
define errors
var ( ErrNewStream = errors.New("new stream error") ErrStreamNotValid = errors.New("stream not valid") ErrNoneMessageType = errors.New("none message type") )
define base errors
var (
ErrStreamPoolFull = errors.New("stream pool is full")
)
define base errors
Functions ¶
func GenAccountKey ¶
func GenPeerIDKey ¶
func GeneratePemKeyFromNetKey ¶
GeneratePemKeyFromNetKey get pem format private key from net private key
func GetKeyPairFromPath ¶
GetKeyPairFromPath get xuper net key from file path
func GetPeerIDByAddress ¶
GetPeerIDByAddress return peer ID corresponding to peerAddr
func GetPemKeyPairFromPath ¶
GetPemKeyPairFromPath get xuper pem private key from file path
func NewP2PServerV2 ¶
NewP2PServerV2 create P2PServerV2 instance
Types ¶
type BucketsFilter ¶
type BucketsFilter struct {
// contains filtered or unexported fields
}
BucketsFilter define filter that get all peers in buckets
type BucketsFilterWithFactor ¶
type BucketsFilterWithFactor struct {
// contains filtered or unexported fields
}
BucketsFilterWithFactor define filter that get a certain percentage peers in each bucket
func (*BucketsFilterWithFactor) Filter ¶
func (nf *BucketsFilterWithFactor) Filter() ([]peer.ID, error)
Filter 从每个Bucket中挑选占比Factor个peers进行广播 对于每一个Bucket,平均分成若干块,每个块抽取若干个节点
*|<---------------- Bucket ---------------->| *-------------------------------------------- *| | | | | | *-------------------------------------------- * split1 split2 split3 split4 split5
type MultiStrategy ¶
type MultiStrategy struct {
// contains filtered or unexported fields
}
MultiStrategy a peer filter that contains multiple filters
func NewMultiStrategy ¶
func NewMultiStrategy(filters []PeerFilter, peerIDs []peer.ID) *MultiStrategy
NewMultiStrategy create instance of MultiStrategy
type NearestBucketFilter ¶
type NearestBucketFilter struct {
// contains filtered or unexported fields
}
NearestBucketFilter define filter that get nearest peers from a specified peer ID
type P2PServerV2 ¶
type P2PServerV2 struct {
// contains filtered or unexported fields
}
P2PServerV2 is the node in the libnet
func (*P2PServerV2) Context ¶
func (p *P2PServerV2) Context() *netBase.NetCtx
func (*P2PServerV2) GetPeerIdByAccount ¶
func (p *P2PServerV2) GetPeerIdByAccount(account string) (peer.ID, error)
func (*P2PServerV2) HandleMessage ¶
func (p *P2PServerV2) HandleMessage(stream netBase.Stream, msg *protos.CoreMessage) error
func (*P2PServerV2) Init ¶
func (p *P2PServerV2) Init(ctx *netBase.NetCtx) error
Init initialize p2p server using given config
func (*P2PServerV2) NewSubscriber ¶
func (p *P2PServerV2) NewSubscriber(typ protos.CoreMessage_MessageType, v interface{}, opts ...netBase.SubscriberOption) netBase.Subscriber
func (*P2PServerV2) PeerInfo ¶
func (p *P2PServerV2) PeerInfo() protos.PeerInfo
func (*P2PServerV2) Register ¶
func (p *P2PServerV2) Register(sub netBase.Subscriber) error
Register register message subscriber to handle messages
func (*P2PServerV2) SendMessage ¶
func (p *P2PServerV2) SendMessage(ctx xctx.Context, msg *protos.CoreMessage, optFunc ...netBase.OptionFunc) error
SendMessage send message to peers using given filter strategy
func (*P2PServerV2) SendMessageWithResponse ¶
func (p *P2PServerV2) SendMessageWithResponse(ctx xctx.Context, msg *protos.CoreMessage, optFunc ...netBase.OptionFunc) ([]*protos.CoreMessage, error)
SendMessageWithResponse send message to peers using given filter strategy, expect response from peers 客户端再使用该方法请求带返回的消息时,最好带上log_id, 否则会导致收消息时收到不匹配的消息而影响后续的处理
func (*P2PServerV2) UnRegister ¶
func (p *P2PServerV2) UnRegister(sub netBase.Subscriber) error
UnRegister remove message subscriber
type PeerFilter ¶
PeerFilter the interface for filter peers
type ReadCloser ¶
func NewDelimitedReader ¶
func NewDelimitedReader(r io.Reader, maxSize int) ReadCloser
type StaticNodeStrategy ¶
type StaticNodeStrategy struct {
// contains filtered or unexported fields
}
StaticNodeStrategy a peer filter that contains strategy nodes
type StreamImpl ¶
type StreamImpl struct {
// contains filtered or unexported fields
}
Stream is the IO wrapper for underly P2P connection
func NewStream ¶
func NewStream(ctx *netBase.NetCtx, netStream libnet.Stream, dispatcher netBase.Dispatcher, host host.Host) (*StreamImpl, error)
NewStream create Stream instance
func (*StreamImpl) HandleMessage ¶
func (s *StreamImpl) HandleMessage(msg *protos.CoreMessage) error
func (*StreamImpl) LoopRecv ¶
func (s *StreamImpl) LoopRecv()
LoopRecv loop to read data from stream
func (*StreamImpl) MultiAddr ¶
func (s *StreamImpl) MultiAddr() multiaddr.Multiaddr
MultiAddr get multi addr
func (*StreamImpl) Send ¶
func (s *StreamImpl) Send(msg *protos.CoreMessage) error
func (*StreamImpl) SendMessage ¶
func (s *StreamImpl) SendMessage(ctx xctx.Context, msg *protos.CoreMessage) error
SendMessage will send a message to a peer
func (*StreamImpl) SendMessageWithResponse ¶
func (s *StreamImpl) SendMessageWithResponse(ctx xctx.Context, msg *protos.CoreMessage) (*protos.CoreMessage, error)
SendMessageWithResponse will send a message to a peer and wait for response
func (*StreamImpl) Valid ¶
func (s *StreamImpl) Valid() bool
type StreamLimit ¶
type StreamLimit struct {
// contains filtered or unexported fields
}
StreamLimit limit the peerID amount of same ip
func (*StreamLimit) AddStream ¶
func (sl *StreamLimit) AddStream(addrStr string, peerID peer.ID) bool
AddStream used to add the amount of same ip, plus one per call
func (*StreamLimit) DelStream ¶
func (sl *StreamLimit) DelStream(addrStr string)
DelStream used to dec the amount of same ip, dec one per call
func (*StreamLimit) GetStreams ¶
func (sl *StreamLimit) GetStreams() []string
GetStreams get all NetURLs from effective streams
func (*StreamLimit) Init ¶
func (sl *StreamLimit) Init(ctx *netBase.NetCtx)
Init initialize the StreamLimit
type StreamPool ¶
type StreamPool struct {
// contains filtered or unexported fields
}
StreamPool manage all the stream
func NewStreamPool ¶
func NewStreamPool(ctx *netBase.NetCtx, ho host.Host, dispatcher netBase.Dispatcher) (*StreamPool, error)
NewStreamPool create StreamPool instance
func (*StreamPool) AddStream ¶
func (sp *StreamPool) AddStream(ctx xctx.Context, stream *StreamImpl) error
AddStream used to add a new P2P stream into pool
func (*StreamPool) DelStream ¶
func (sp *StreamPool) DelStream(stream *StreamImpl) error
DelStream delete a stream
func (*StreamPool) Get ¶
func (sp *StreamPool) Get(ctx xctx.Context, peerId peer.ID) (*StreamImpl, error)
Get will probe and return a stream
func (*StreamPool) NewStream ¶
func (sp *StreamPool) NewStream(ctx xctx.Context, netStream libnet.Stream) (*StreamImpl, error)
Add used to add a new net stream into pool
type Transport ¶
type Transport struct {
// contains filtered or unexported fields
}
Transport constructs secure communication sessions for a peer.
type WriteCloser ¶
func NewDelimitedWriter ¶
func NewDelimitedWriter(w io.Writer) WriteCloser