protocol.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package p2p
  17. import (
  18. "blockchain-go/p2p/enode"
  19. "blockchain-go/p2p/enr"
  20. )
  21. // Protocol represents a P2P subprotocol implementation.
  22. type Protocol struct {
  23. // Name should contain the official protocol name,
  24. // often a three-letter word.
  25. Name string
  26. // Version should contain the version number of the protocol.
  27. Version uint
  28. // Length should contain the number of message codes used
  29. // by the protocol.
  30. Length uint64
  31. // Run is called in a new goroutine when the protocol has been
  32. // negotiated with a peer. It should read and write messages from
  33. // rw. The Payload for each message must be fully consumed.
  34. //
  35. // The peer connection is closed when Start returns. It should return
  36. // any protocol-level error (such as an I/O error) that is
  37. // encountered.
  38. Run func(peer *Peer, rw MsgReadWriter) error
  39. // NodeInfo is an optional helper method to retrieve protocol specific metadata
  40. // about the host node.
  41. NodeInfo func() interface{}
  42. // PeerInfo is an optional helper method to retrieve protocol specific metadata
  43. // about a certain peer in the network. If an info retrieval function is set,
  44. // but returns nil, it is assumed that the protocol handshake is still running.
  45. PeerInfo func(id enode.ID) interface{}
  46. // DialCandidates, if non-nil, is a way to tell Server about protocol-specific nodes
  47. // that should be dialed. The server continuously reads nodes from the iterator and
  48. // attempts to create connections to them.
  49. DialCandidates enode.Iterator
  50. // Attributes contains protocol specific information for the node record.
  51. Attributes []enr.Entry
  52. }
  53. func (p Protocol) cap() Cap {
  54. return Cap{p.Name, p.Version}
  55. }