Capability.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 Capability.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include "Common.h"
  20. #include "HostCapability.h"
  21. namespace dev
  22. {
  23. namespace p2p
  24. {
  25. class ReputationManager;
  26. class Capability: public std::enable_shared_from_this<Capability>
  27. {
  28. friend class Session;
  29. public:
  30. Capability(std::shared_ptr<Session> _s, HostCapabilityFace* _h, unsigned _idOffset, uint16_t _protocolID);
  31. virtual ~Capability() {}
  32. // Implement these in the derived class.
  33. /* static std::string name() { return ""; }
  34. static u256 version() { return 0; }
  35. static unsigned messageCount() { return 0; }
  36. */
  37. std::shared_ptr<Session> session() const { return m_session.lock(); }
  38. HostCapabilityFace* hostCapability() const { return m_hostCap; }
  39. Host* host() const { return m_hostCap->host(); }
  40. ReputationManager& repMan() const;
  41. protected:
  42. virtual bool interpret(unsigned _id, RLP const&) = 0;
  43. void disable(std::string const& _problem);
  44. RLPStream& prep(RLPStream& _s, unsigned _id, unsigned _args = 0);
  45. void sealAndSend(RLPStream& _s);
  46. void addRating(int _r);
  47. uint16_t const c_protocolID;
  48. private:
  49. std::weak_ptr<Session> m_session;
  50. HostCapabilityFace* m_hostCap;
  51. bool m_enabled = true;
  52. unsigned m_idOffset;
  53. };
  54. }
  55. }