Fabcoin Core  0.16.2
P2P Digital Currency
RLPXFrameWriter.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU 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  cpp-ethereum 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 General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16  */
23 #pragma once
24 
25 #include <deque>
26 #include <libdevcore/Guards.h>
27 #include "RLPXFrameCoder.h"
28 #include "RLPXPacket.h"
29 namespace ba = boost::asio;
30 namespace bi = boost::asio::ip;
31 
32 namespace dev
33 {
34 namespace p2p
35 {
36 
43 {
50  struct WriterState
51  {
52  std::deque<RLPXPacket> q;
53  mutable Mutex x;
54 
55  RLPXPacket* writing = nullptr;
56  size_t remaining = 0;
57  bool multiFrame = false;
58  uint16_t sequence = -1;
59  };
60 
61 public:
63  static const uint16_t EmptyFrameLength;
64  static const uint16_t MinFrameDequeLength;
65 
66  RLPXFrameWriter(uint16_t _protocolType): m_protocolId(_protocolType) {}
68 
70  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; }
71 
73  void enque(uint8_t _packetType, RLPStream& _payload, PacketPriority _priority = PriorityLow);
74 
76  size_t mux(RLPXFrameCoder& _coder, unsigned _size, std::deque<bytes>& o_toWrite);
77 
79  void enque(RLPXPacket&& _p, PacketPriority _priority = PriorityLow);
80 
81  uint16_t protocolId() const { return m_protocolId; }
82 
83 private:
84  uint16_t const m_protocolId;
85  std::pair<WriterState, WriterState> m_q; // High, Low frame queues
86  uint16_t m_sequenceId = 0; // Sequence ID
87 };
88 
89 }
90 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
RLPXFrameWriter(RLPXFrameWriter const &_s)
#define h(i)
Definition: sha.cpp:736
#define DEV_GUARDED(MUTEX)
Simple block guard.
Definition: Guards.h:144
size_t mux(RLPXFrameCoder &_coder, unsigned _size, std::deque< bytes > &o_toWrite)
Returns number of packets framed and outputs frames to o_bytes. Not thread-safe.
void enque(uint8_t _packetType, RLPStream &_payload, PacketPriority _priority=PriorityLow)
Moves output to queue, to be muxed into frames by mux() when network buffer is ready for writing...
Queue and state for Writer Properties are used independently; only valid packets should be added to q...
RLPXFrameWriter(uint16_t _protocolType)
size_t size() const
Returns total number of queued packets. Thread-safe.
uint16_t protocolId() const
RLPX Packet.
Definition: RLPXPacket.h:39
static const uint16_t MinFrameDequeLength
static const uint16_t EmptyFrameLength
std::mutex Mutex
Definition: Guards.h:37
Class for writing to an RLP bytestream.
Definition: RLP.h:383
Encoder/decoder transport for RLPx connection established by RLPXHandshake.
std::pair< WriterState, WriterState > m_q
Multiplex packets into encrypted RLPX frames.