protocol.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. "fmt"
  19. "github.com/ethereum/go-ethereum/p2p/enode"
  20. "github.com/ethereum/go-ethereum/p2p/enr"
  21. )
  22. // Protocol represents a P2P subprotocol implementation.
  23. type Protocol struct {
  24. // Name should contain the official protocol name,
  25. // often a three-letter word.
  26. Name string
  27. // Version should contain the version number of the protocol.
  28. Version uint
  29. // Length should contain the number of message codes used
  30. // by the protocol.
  31. Length uint64
  32. // Run is called in a new goroutine when the protocol has been
  33. // negotiated with a peer. It should read and write messages from
  34. // rw. The Payload for each message must be fully consumed.
  35. //
  36. // The peer connection is closed when Start returns. It should return
  37. // any protocol-level error (such as an I/O error) that is
  38. // encountered.
  39. Run func(peer *Peer, rw MsgReadWriter) error
  40. // NodeInfo is an optional helper method to retrieve protocol specific metadata
  41. // about the host node.
  42. NodeInfo func() interface{}
  43. // PeerInfo is an optional helper method to retrieve protocol specific metadata
  44. // about a certain peer in the network. If an info retrieval function is set,
  45. // but returns nil, it is assumed that the protocol handshake is still running.
  46. PeerInfo func(id enode.ID) interface{}
  47. // Attributes contains protocol specific information for the node record.
  48. Attributes []enr.Entry
  49. }
  50. func (p Protocol) cap() Cap {
  51. return Cap{p.Name, p.Version}
  52. }
  53. // Cap is the structure of a peer capability.
  54. type Cap struct {
  55. Name string
  56. Version uint
  57. }
  58. func (cap Cap) String() string {
  59. return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
  60. }
  61. type capsByNameAndVersion []Cap
  62. func (cs capsByNameAndVersion) Len() int { return len(cs) }
  63. func (cs capsByNameAndVersion) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
  64. func (cs capsByNameAndVersion) Less(i, j int) bool {
  65. return cs[i].Name < cs[j].Name || (cs[i].Name == cs[j].Name && cs[i].Version < cs[j].Version)
  66. }