HostCapability.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file HostCapability.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. *
  18. * Miscellanea required for the Host/Session classes.
  19. */
  20. #pragma once
  21. #include <memory>
  22. #include "Peer.h"
  23. #include "Common.h"
  24. #include "Session.h"
  25. namespace dev
  26. {
  27. namespace p2p
  28. {
  29. class HostCapabilityFace
  30. {
  31. friend class Host;
  32. template <class T> friend class HostCapability;
  33. friend class Capability;
  34. friend class Session;
  35. public:
  36. HostCapabilityFace() {}
  37. virtual ~HostCapabilityFace() {}
  38. Host* host() const { return m_host; }
  39. std::vector<std::pair<std::shared_ptr<Session>, std::shared_ptr<Peer>>> peerSessions() const;
  40. std::vector<std::pair<std::shared_ptr<Session>, std::shared_ptr<Peer>>> peerSessions(u256 const& _version) const;
  41. protected:
  42. virtual std::string name() const = 0;
  43. virtual u256 version() const = 0;
  44. CapDesc capDesc() const { return std::make_pair(name(), version()); }
  45. virtual unsigned messageCount() const = 0;
  46. virtual std::shared_ptr<Capability> newPeerCapability(std::shared_ptr<Session> const& _s, unsigned _idOffset, CapDesc const& _cap, uint16_t _capID) = 0;
  47. virtual void onStarting() {}
  48. virtual void onStopping() {}
  49. private:
  50. Host* m_host = nullptr;
  51. };
  52. template<class PeerCap>
  53. class HostCapability: public HostCapabilityFace
  54. {
  55. public:
  56. HostCapability() {}
  57. virtual ~HostCapability() {}
  58. static std::string staticName() { return PeerCap::name(); }
  59. static u256 staticVersion() { return PeerCap::version(); }
  60. static unsigned staticMessageCount() { return PeerCap::messageCount(); }
  61. protected:
  62. virtual std::string name() const { return PeerCap::name(); }
  63. virtual u256 version() const { return PeerCap::version(); }
  64. virtual unsigned messageCount() const { return PeerCap::messageCount(); }
  65. virtual std::shared_ptr<Capability> newPeerCapability(std::shared_ptr<Session> const& _s, unsigned _idOffset, CapDesc const& _cap, uint16_t _capID)
  66. {
  67. _s->registerFraming(_capID);
  68. auto p = std::make_shared<PeerCap>(_s, this, _idOffset, _cap, _capID);
  69. _s->registerCapability(_cap, p);
  70. return p;
  71. }
  72. };
  73. }
  74. }