RLPXSocketIO.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 RLPXSocketIO.h
  15. * @author Alex Leverington <nessence@gmail.com>
  16. * @date 2015
  17. */
  18. #pragma once
  19. #include "RLPXFrameWriter.h"
  20. namespace ba = boost::asio;
  21. namespace bi = boost::asio::ip;
  22. namespace dev
  23. {
  24. namespace p2p
  25. {
  26. class RLPXSocketIO: public std::enable_shared_from_this<RLPXSocketIO>
  27. {
  28. public:
  29. static uint32_t const MinFrameSize;
  30. static uint32_t const MaxPacketSize;
  31. static uint16_t const DefaultInitialCapacity;
  32. RLPXSocketIO(unsigned _protCount, RLPXFrameCoder& _coder, bi::tcp::socket& _socket, bool _flowControl = true, size_t _initialCapacity = DefaultInitialCapacity);
  33. void send(unsigned _protocolType, unsigned _type, RLPStream& _payload);
  34. void doWrite();
  35. bool congested() const { return !!m_congestion; }
  36. private:
  37. static std::vector<RLPXFrameWriter> writers(unsigned _capacity);
  38. void deferWrite();
  39. void write(size_t _dequed);
  40. bool const m_flowControl; ///< True if flow control is enabled.
  41. RLPXFrameCoder& m_coder; ///< Encoder/decoder of frame payloads.
  42. bi::tcp::socket& m_socket;
  43. std::deque<bytes> m_toSend; ///< Reusable byte buffer for pending socket writes.
  44. std::vector<RLPXFrameWriter> m_writers; ///< Write queues for each protocol. TODO: map to bytes (of capability)
  45. std::unique_ptr<ba::deadline_timer> m_congestion; ///< Scheduled when writes are deferred due to congestion.
  46. Mutex x_queued;
  47. unsigned m_queued = 0; ///< Track total queued packets to ensure single write loop
  48. uint32_t m_egressCapacity;
  49. };
  50. }
  51. }