peer.go 2.2 KB

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