peer.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package p2p
  2. import (
  3. "blockchain-go/p2p/enode"
  4. "errors"
  5. "time"
  6. )
  7. var (
  8. ErrShuttingDown = errors.New("shutting down")
  9. )
  10. const (
  11. baseProtocolVersion = 5
  12. baseProtocolLength = uint64(16)
  13. baseProtocolMaxMsgSize = 2 * 1024
  14. snappyProtocolVersion = 5
  15. pingInterval = 15 * time.Second
  16. )
  17. const (
  18. // devp2p message codes
  19. handshakeMsg = 0x00
  20. discMsg = 0x01
  21. pingMsg = 0x02
  22. pongMsg = 0x03
  23. )
  24. // protoHandshake is the RLP structure of the protocol handshake.
  25. type protoHandshake struct {
  26. Version uint64
  27. Name string
  28. Caps []Cap
  29. ListenPort uint64
  30. ID []byte // secp256k1 public key
  31. // Ignore additional fields (for forward compatibility).
  32. //Rest []rlp.RawValue `rlp:"tail"`
  33. }
  34. type PeerEventType string
  35. const (
  36. // PeerEventTypeAdd is the type of event emitted when a peer is added
  37. // to a p2p.Server
  38. PeerEventTypeAdd PeerEventType = "add"
  39. // PeerEventTypeDrop is the type of event emitted when a peer is
  40. // dropped from a p2p.Server
  41. PeerEventTypeDrop PeerEventType = "drop"
  42. // PeerEventTypeMsgSend is the type of event emitted when a
  43. // message is successfully sent to a peer
  44. PeerEventTypeMsgSend PeerEventType = "msgsend"
  45. // PeerEventTypeMsgRecv is the type of event emitted when a
  46. // message is received from a peer
  47. PeerEventTypeMsgRecv PeerEventType = "msgrecv"
  48. )
  49. // PeerEvent is an event emitted when peers are either added or dropped from
  50. // a p2p.Server or when a message is sent or received on a peer connection
  51. type PeerEvent struct {
  52. Type PeerEventType `json:"type"`
  53. Peer enode.ID `json:"peer"`
  54. Error string `json:"error,omitempty"`
  55. Protocol string `json:"protocol,omitempty"`
  56. MsgCode *uint64 `json:"msg_code,omitempty"`
  57. MsgSize *uint32 `json:"msg_size,omitempty"`
  58. LocalAddress string `json:"local,omitempty"`
  59. RemoteAddress string `json:"remote,omitempty"`
  60. }
  61. // Peer represents a connected remote node.
  62. type Peer struct {
  63. //rw *conn
  64. //running map[string]*protoRW
  65. //log log.Logger
  66. //created mclock.AbsTime
  67. //wg sync.WaitGroup
  68. //protoErr chan error
  69. //closed chan struct{}
  70. //disc chan DiscReason
  71. // events receives message send / receive events if set
  72. //events *event.Feed
  73. }