protocol.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // DialCandidates, if non-nil, is a way to tell Server about protocol-specific nodes
  48. // that should be dialed. The server continuously reads nodes from the iterator and
  49. // attempts to create connections to them.
  50. DialCandidates enode.Iterator
  51. // Attributes contains protocol specific information for the node record.
  52. Attributes []enr.Entry
  53. }
  54. func (p Protocol) cap() Cap {
  55. return Cap{p.Name, p.Version}
  56. }
  57. // Cap is the structure of a peer capability.
  58. type Cap struct {
  59. Name string
  60. Version uint
  61. }
  62. func (cap Cap) String() string {
  63. return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
  64. }
  65. type capsByNameAndVersion []Cap
  66. func (cs capsByNameAndVersion) Len() int { return len(cs) }
  67. func (cs capsByNameAndVersion) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
  68. func (cs capsByNameAndVersion) Less(i, j int) bool {
  69. return cs[i].Name < cs[j].Name || (cs[i].Name == cs[j].Name && cs[i].Version < cs[j].Version)
  70. }