RLPXFrameWriter.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 RLPXperimental.h
  15. * @author Alex Leverington <nessence@gmail.com>
  16. * @date 2015
  17. */
  18. #pragma once
  19. #include <libdevcore/Guards.h>
  20. #include "RLPXFrameCoder.h"
  21. #include "RLPXPacket.h"
  22. namespace ba = boost::asio;
  23. namespace bi = boost::asio::ip;
  24. namespace dev
  25. {
  26. namespace p2p
  27. {
  28. /**
  29. * @brief Multiplex packets into encrypted RLPX frames.
  30. * @todo throw when enqueued packet is invalid
  31. * @todo use RLPXFrameInfo
  32. */
  33. class RLPXFrameWriter
  34. {
  35. /**
  36. * @brief Queue and state for Writer
  37. * Properties are used independently;
  38. * only valid packets should be added to q
  39. * @todo implement as class
  40. */
  41. struct WriterState
  42. {
  43. std::deque<RLPXPacket> q;
  44. mutable Mutex x;
  45. RLPXPacket* writing = nullptr;
  46. size_t remaining = 0;
  47. bool multiFrame = false;
  48. uint16_t sequence = -1;
  49. };
  50. public:
  51. enum PacketPriority { PriorityLow = 0, PriorityHigh };
  52. static const uint16_t EmptyFrameLength;
  53. static const uint16_t MinFrameDequeLength;
  54. RLPXFrameWriter(uint16_t _protocolType): m_protocolId(_protocolType) {}
  55. RLPXFrameWriter(RLPXFrameWriter const& _s): m_protocolId(_s.m_protocolId) {}
  56. /// Returns total number of queued packets. Thread-safe.
  57. size_t size() const { size_t l = 0; size_t h = 0; DEV_GUARDED(m_q.first.x) h = m_q.first.q.size(); DEV_GUARDED(m_q.second.x) l = m_q.second.q.size(); return l + h; }
  58. /// Moves @_payload output to queue, to be muxed into frames by mux() when network buffer is ready for writing. Thread-safe.
  59. void enque(uint8_t _packetType, RLPStream& _payload, PacketPriority _priority = PriorityLow);
  60. /// Returns number of packets framed and outputs frames to o_bytes. Not thread-safe.
  61. size_t mux(RLPXFrameCoder& _coder, unsigned _size, std::deque<bytes>& o_toWrite);
  62. /// Moves @_p to queue, to be muxed into frames by mux() when network buffer is ready for writing. Thread-safe.
  63. void enque(RLPXPacket&& _p, PacketPriority _priority = PriorityLow);
  64. uint16_t protocolId() const { return m_protocolId; }
  65. private:
  66. uint16_t const m_protocolId;
  67. std::pair<WriterState, WriterState> m_q; // High, Low frame queues
  68. uint16_t m_sequenceId = 0; // Sequence ID
  69. };
  70. }
  71. }