libp2p

package module
v0.3.13 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2020 License: MIT Imports: 0 Imported by: 2

README

libp2p

  1. 用于分布式点对点传输
  2. 支持多种传输协议
  3. url格式的地址,如:tcp://[email protected]:54300
  4. 不同节点可以使用不同的协议
  5. 同时方便扩展新的协议
  6. 支持插件式功能扩展
  7. 当前支持的协议有:tcp,udp,ws,s2s
  8. tcp:最基本用法,可靠连接
  9. udp:允许丢包,避免阻塞
  10. ws: 基于http的Websocket连接,可以穿透防火墙,方便部署到http server的后端
  11. s2s:udp server to udp server,只会占用一个端口,连接数量无限,方便NAT穿透
  12. 使用方式可以参考examples/chat

Documentation

Index

Constants

View Source
const EnvConnectID = "cid"

EnvConnectID the env key of ConnectID,the connection have different id

Variables

This section is empty.

Functions

This section is empty.

Types

type Addr

type Addr interface {
	String() string
	Scheme() string
	User() string
	Host() string
	IsServer() bool
	UpdateUser(string)
	SetServer()
}

Addr the address of connection

type Conn

type Conn interface {
	// Read reads data from the connection.
	// Read can be made to time out and return an Error with Timeout() == true
	// after a fixed time limit; see SetDeadline and SetReadDeadline.
	Read(b []byte) (n int, err error)

	// Write writes data to the connection.
	// Write can be made to time out and return an Error with Timeout() == true
	// after a fixed time limit; see SetDeadline and SetWriteDeadline.
	Write(b []byte) (n int, err error)

	// Close closes the connection.
	// Any blocked Read or Write operations will be unblocked and return errors.
	Close() error

	// LocalAddr returns the local network address.
	LocalAddr() Addr

	// RemoteAddr returns the remote network address.
	RemoteAddr() Addr
}

Conn is a generic stream-oriented network connection.

Multiple goroutines may invoke methods on a Conn simultaneously.

type ConnPool

type ConnPool interface {
	Listen(address string, handle func(conn Conn)) error
	Dial(address string) (Conn, error)
	// close the listener
	Close()
}

ConnPool connection pool,address such as:"tcp://127.0.0.1:1111"

type ConnPoolMgr

type ConnPoolMgr interface {
	RegConnPool(scheme string, c ConnPool)
	Listen(address string, handle func(conn Conn)) error
	Dial(address string) (Conn, error)
	// close the listener
	Close()
}

ConnPoolMgr manager of connection pool,address such as:"tcp://127.0.0.1:1111"

type CryptoMgr

type CryptoMgr interface {
	SetPrivKey(typ string, key []byte) error
	GetType() string
	Sign(data []byte) []byte
	Verify(typ string, data, sig, pubKey []byte) bool
	Register(k SignKey)
	GetPublic() []byte
}

CryptoMgr crypto manager

type Event

type Event interface {
	// encode by gob
	Reply(interface{}) error
	GetMessage() interface{}
	GetSession() Session
	GetPeerID() []byte
}

Event interface of event

type IPlugin

type IPlugin interface {
	// Callback for when the network starts listening for peers.
	Startup(net Network)

	// Callback for when the network stops listening for peers.
	Cleanup(net Network)

	// Callback for when an incoming message is received. Return true
	// if the plugin will intercept messages to be processed.
	Receive(e Event) error

	// RecInternalMsg receive internal message
	RecInternalMsg(InterMsg) error

	// Callback for when a peer connects to the network.
	PeerConnect(s Session)

	// Callback for when a peer disconnects from the network.
	PeerDisconnect(s Session)
}

IPlugin is used to proxy callbacks to a particular Plugin instance.

type InterMsg

type InterMsg interface {
	GetType() string
	GetMsg() interface{}
}

InterMsg internal message

type Network

type Network interface {
	// address:  tcp://127.0.0.1:80
	NewSession(address string) (Session, error)
	RegistPlugin(IPlugin)
	SendInternalMsg(InterMsg)
	GetAddress() string
	Close()
}

Network interface of network manager

type Plugin

type Plugin struct{}

Plugin is an abstract class which all plugins extend.

func (*Plugin) Cleanup

func (*Plugin) Cleanup(net Network)

Cleanup is called only once after network stops listening

func (*Plugin) PeerConnect

func (*Plugin) PeerConnect(s Session)

PeerConnect is called every time a Session is initialized and connected

func (*Plugin) PeerDisconnect

func (*Plugin) PeerDisconnect(s Session)

PeerDisconnect is called every time a Session connection is closed

func (*Plugin) RecInternalMsg

func (*Plugin) RecInternalMsg(InterMsg) error

RecInternalMsg receive internal message

func (*Plugin) Receive

func (*Plugin) Receive(e Event) error

Receive is called every time when messages are received

func (*Plugin) Startup

func (*Plugin) Startup(net Network)

Startup is called only once when the plugin is loaded

type Session

type Session interface {
	// encode by gob
	Send(interface{}) error
	GetPeerAddr() Addr
	GetSelfAddr() Addr
	SetEnv(key, value string)
	GetEnv(key string) string
	Close()
}

Session interface of session

type SignKey

type SignKey interface {
	GetType() string
	Verify(data, sig, pubKey []byte) bool
	// Cryptographically sign the given bytes
	Sign(data, privKey []byte) []byte
	// Return a public key paired with this private key
	GetPublic(privKey []byte) []byte
}

SignKey interface define of crypto key

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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