protocol.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/discover"
  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 groutine 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 discover.NodeID) interface{}
  46. }
  47. func (p Protocol) cap() Cap {
  48. return Cap{p.Name, p.Version}
  49. }
  50. // Cap is the structure of a peer capability.
  51. type Cap struct {
  52. Name string
  53. Version uint
  54. }
  55. func (cap Cap) RlpData() interface{} {
  56. return []interface{}{cap.Name, cap.Version}
  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. }