Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
Address string
PrivateKey *crypto.PrivateKey
BootstrapNodes []string
MaxPeers int
ReconnectTimes int
ConnectTimeInterval int
KeepAliveInterval int
KeepAliveTimes int
MinPeers int
Protocols []Protocol
RouteAddress []string
}
Config is the p2p network configuration
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig defines the default network configuration
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection represents a tcp client
type EncHandshake ¶
EncHandshake is encryption handshake. implement the interface of Protocol
func GetEncHandshake ¶
func GetEncHandshake() *EncHandshake
GetEncHandshake returns enchandshake message
type Msg ¶
Msg on network
func (*Msg) Deserialize ¶
Deserialize deserialize bytes to message
type MsgReadWriter ¶
MsgReadWriter is the interface that groups the p2p message Read and Write methods.
type Peer ¶
type Peer struct {
ID PeerID
LastActiveTime time.Time
Address string
Conn net.Conn
// contains filtered or unexported fields
}
Peer represents a peer in blockchain
func (*Peer) GetPeerAddress ¶
GetPeerAddress returns local peer address info
type ProtoHandshake ¶
ProtoHandshake is protocol handshake. implement the interface of Protocol
func GetProtoHandshake ¶
func GetProtoHandshake() *ProtoHandshake
GetProtoHandshake returns protocol handshake
type Protocol ¶
type Protocol struct {
BaseCmd uint8
Name string
Version string
Run func(p *Peer, rw MsgReadWriter) error
}
Protocol raw structure
type Server ¶
type Server struct {
Config
// contains filtered or unexported fields
}
Server represents a p2p network server
func NewServer ¶
func NewServer(db *db.BlockchainDB, cfg *Config) *Server
NewServer returns a new p2p server
func (*Server) GetLocalPeer ¶
GetLocalPeer returns local peer info
type TCPServer ¶
type TCPServer struct {
// contains filtered or unexported fields
}
TCPServer represents a tcp server
func (*TCPServer) OnClientClose ¶
func (srv *TCPServer) OnClientClose(callback func(c *Connection))
OnClientClose called when client closed
func (*TCPServer) OnNewClient ¶
func (srv *TCPServer) OnNewClient(callback func(c *Connection))
OnNewClient called when new client connect
func (*TCPServer) OnNewMessage ¶
func (srv *TCPServer) OnNewMessage(callback func(*Connection, *Msg))
OnNewMessage called when received a new message from client