|
|
@@ -0,0 +1,89 @@
|
|
|
+package p2p
|
|
|
+
|
|
|
+import (
|
|
|
+ "errors"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+var (
|
|
|
+ ErrShuttingDown = errors.New("shutting down")
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ baseProtocolVersion = 5
|
|
|
+ baseProtocolLength = uint64(16)
|
|
|
+ baseProtocolMaxMsgSize = 2 * 1024
|
|
|
+
|
|
|
+ snappyProtocolVersion = 5
|
|
|
+
|
|
|
+ pingInterval = 15 * time.Second
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ // devp2p message codes
|
|
|
+ handshakeMsg = 0x00
|
|
|
+ discMsg = 0x01
|
|
|
+ pingMsg = 0x02
|
|
|
+ pongMsg = 0x03
|
|
|
+)
|
|
|
+
|
|
|
+// protoHandshake is the RLP structure of the protocol handshake.
|
|
|
+type protoHandshake struct {
|
|
|
+ Version uint64
|
|
|
+ Name string
|
|
|
+ //Caps []Cap
|
|
|
+ ListenPort uint64
|
|
|
+ ID []byte // secp256k1 public key
|
|
|
+
|
|
|
+ // Ignore additional fields (for forward compatibility).
|
|
|
+ //Rest []rlp.RawValue `rlp:"tail"`
|
|
|
+}
|
|
|
+
|
|
|
+type PeerEventType string
|
|
|
+
|
|
|
+const (
|
|
|
+ // PeerEventTypeAdd is the type of event emitted when a peer is added
|
|
|
+ // to a p2p.Server
|
|
|
+ PeerEventTypeAdd PeerEventType = "add"
|
|
|
+
|
|
|
+ // PeerEventTypeDrop is the type of event emitted when a peer is
|
|
|
+ // dropped from a p2p.Server
|
|
|
+ PeerEventTypeDrop PeerEventType = "drop"
|
|
|
+
|
|
|
+ // PeerEventTypeMsgSend is the type of event emitted when a
|
|
|
+ // message is successfully sent to a peer
|
|
|
+ PeerEventTypeMsgSend PeerEventType = "msgsend"
|
|
|
+
|
|
|
+ // PeerEventTypeMsgRecv is the type of event emitted when a
|
|
|
+ // message is received from a peer
|
|
|
+ PeerEventTypeMsgRecv PeerEventType = "msgrecv"
|
|
|
+)
|
|
|
+
|
|
|
+// PeerEvent is an event emitted when peers are either added or dropped from
|
|
|
+// a p2p.Server or when a message is sent or received on a peer connection
|
|
|
+type PeerEvent struct {
|
|
|
+ Type PeerEventType `json:"type"`
|
|
|
+ //Peer enode.ID `json:"peer"`
|
|
|
+ Error string `json:"error,omitempty"`
|
|
|
+ Protocol string `json:"protocol,omitempty"`
|
|
|
+ MsgCode *uint64 `json:"msg_code,omitempty"`
|
|
|
+ MsgSize *uint32 `json:"msg_size,omitempty"`
|
|
|
+ LocalAddress string `json:"local,omitempty"`
|
|
|
+ RemoteAddress string `json:"remote,omitempty"`
|
|
|
+}
|
|
|
+
|
|
|
+// Peer represents a connected remote node.
|
|
|
+type Peer struct {
|
|
|
+ //rw *conn
|
|
|
+ //running map[string]*protoRW
|
|
|
+ //log log.Logger
|
|
|
+ //created mclock.AbsTime
|
|
|
+
|
|
|
+ //wg sync.WaitGroup
|
|
|
+ protoErr chan error
|
|
|
+ closed chan struct{}
|
|
|
+ //disc chan DiscReason
|
|
|
+
|
|
|
+ // events receives message send / receive events if set
|
|
|
+ //events *event.Feed
|
|
|
+}
|