20#ifndef RIPPLE_OVERLAY_PROTOCOLMESSAGE_H_INCLUDED
21#define RIPPLE_OVERLAY_PROTOCOLMESSAGE_H_INCLUDED
23#include <xrpld/overlay/Compression.h>
24#include <xrpld/overlay/Message.h>
25#include <xrpld/overlay/detail/ZeroCopyStream.h>
26#include <xrpl/beast/utility/instrumentation.h>
27#include <xrpl/protocol/messages.h>
29#include <boost/asio/buffer.hpp>
30#include <boost/asio/buffers_iterator.hpp>
39inline protocol::MessageType
42 return protocol::mtGET_LEDGER;
45inline protocol::MessageType
48 return protocol::mtREPLAY_DELTA_REQ;
51inline protocol::MessageType
54 return protocol::mtPROOF_PATH_REQ;
58template <
class =
void>
64 case protocol::mtMANIFESTS:
66 case protocol::mtPING:
68 case protocol::mtCLUSTER:
70 case protocol::mtENDPOINTS:
72 case protocol::mtTRANSACTION:
74 case protocol::mtGET_LEDGER:
76 case protocol::mtLEDGER_DATA:
78 case protocol::mtPROPOSE_LEDGER:
80 case protocol::mtSTATUS_CHANGE:
82 case protocol::mtHAVE_SET:
84 case protocol::mtVALIDATORLIST:
85 return "validator_list";
86 case protocol::mtVALIDATORLISTCOLLECTION:
87 return "validator_list_collection";
88 case protocol::mtVALIDATION:
90 case protocol::mtGET_OBJECTS:
92 case protocol::mtHAVE_TRANSACTIONS:
93 return "have_transactions";
94 case protocol::mtTRANSACTIONS:
95 return "transactions";
96 case protocol::mtSQUELCH:
98 case protocol::mtPROOF_PATH_REQ:
99 return "proof_path_request";
100 case protocol::mtPROOF_PATH_RESPONSE:
101 return "proof_path_response";
102 case protocol::mtREPLAY_DELTA_REQ:
103 return "replay_delta_request";
104 case protocol::mtREPLAY_DELTA_RESPONSE:
105 return "replay_delta_response";
141template <
typename BufferSequence>
145 return boost::asio::buffers_iterator<BufferSequence, std::uint8_t>::begin(
149template <
typename BufferSequence>
153 return boost::asio::buffers_iterator<BufferSequence, std::uint8_t>::end(
166template <
class BufferSequence>
169 boost::system::error_code& ec,
170 BufferSequence
const& bufs,
179 "ripple::detail::parseMessageHeader : non-empty buffer");
211 for (
int i = 0; i != 4; ++i)
219 for (
int i = 0; i != 2; ++i)
222 for (
int i = 0; i != 4; ++i)
231 if ((*iter & 0xFC) == 0)
243 for (
int i = 0; i != 4; ++i)
249 for (
int i = 0; i != 2; ++i)
267 auto const m = std::make_shared<T>();
284 if (payloadSize == 0 || !m->ParseFromArray(payload.
data(), payloadSize))
287 else if (!m->ParseFromZeroCopyStream(&stream))
302 auto const m = parseMessageContent<T>(header, buffers);
307 handler.onMessageBegin(
313 handler.onMessage(m);
333template <
class Buffers,
class Handler>
336 Buffers
const& buffers,
342 auto const size = boost::asio::buffer_size(buffers);
369 if (!handler.compressionEnabled() &&
378 if (header->total_wire_size > size)
380 hint = header->total_wire_size - size;
386 switch (header->message_type)
388 case protocol::mtMANIFESTS:
389 success = detail::invoke<protocol::TMManifests>(
390 *header, buffers, handler);
392 case protocol::mtPING:
394 detail::invoke<protocol::TMPing>(*header, buffers, handler);
396 case protocol::mtCLUSTER:
398 detail::invoke<protocol::TMCluster>(*header, buffers, handler);
400 case protocol::mtENDPOINTS:
401 success = detail::invoke<protocol::TMEndpoints>(
402 *header, buffers, handler);
404 case protocol::mtTRANSACTION:
405 success = detail::invoke<protocol::TMTransaction>(
406 *header, buffers, handler);
408 case protocol::mtGET_LEDGER:
409 success = detail::invoke<protocol::TMGetLedger>(
410 *header, buffers, handler);
412 case protocol::mtLEDGER_DATA:
413 success = detail::invoke<protocol::TMLedgerData>(
414 *header, buffers, handler);
416 case protocol::mtPROPOSE_LEDGER:
417 success = detail::invoke<protocol::TMProposeSet>(
418 *header, buffers, handler);
420 case protocol::mtSTATUS_CHANGE:
421 success = detail::invoke<protocol::TMStatusChange>(
422 *header, buffers, handler);
424 case protocol::mtHAVE_SET:
425 success = detail::invoke<protocol::TMHaveTransactionSet>(
426 *header, buffers, handler);
428 case protocol::mtVALIDATION:
429 success = detail::invoke<protocol::TMValidation>(
430 *header, buffers, handler);
432 case protocol::mtVALIDATORLIST:
433 success = detail::invoke<protocol::TMValidatorList>(
434 *header, buffers, handler);
436 case protocol::mtVALIDATORLISTCOLLECTION:
437 success = detail::invoke<protocol::TMValidatorListCollection>(
438 *header, buffers, handler);
440 case protocol::mtGET_OBJECTS:
441 success = detail::invoke<protocol::TMGetObjectByHash>(
442 *header, buffers, handler);
444 case protocol::mtHAVE_TRANSACTIONS:
445 success = detail::invoke<protocol::TMHaveTransactions>(
446 *header, buffers, handler);
448 case protocol::mtTRANSACTIONS:
449 success = detail::invoke<protocol::TMTransactions>(
450 *header, buffers, handler);
452 case protocol::mtSQUELCH:
454 detail::invoke<protocol::TMSquelch>(*header, buffers, handler);
456 case protocol::mtPROOF_PATH_REQ:
457 success = detail::invoke<protocol::TMProofPathRequest>(
458 *header, buffers, handler);
460 case protocol::mtPROOF_PATH_RESPONSE:
461 success = detail::invoke<protocol::TMProofPathResponse>(
462 *header, buffers, handler);
464 case protocol::mtREPLAY_DELTA_REQ:
465 success = detail::invoke<protocol::TMReplayDeltaRequest>(
466 *header, buffers, handler);
468 case protocol::mtREPLAY_DELTA_RESPONSE:
469 success = detail::invoke<protocol::TMReplayDeltaResponse>(
470 *header, buffers, handler);
473 handler.onMessageUnknown(header->message_type);
478 result.
first = header->total_wire_size;
std::size_t decompress(InputStream &in, std::size_t inSize, std::uint8_t *decompressed, std::size_t decompressedSize, Algorithm algorithm=Algorithm::LZ4)
Decompress input stream.
std::shared_ptr< T > parseMessageContent(MessageHeader const &header, Buffers const &buffers)
bool invoke(MessageHeader const &header, Buffers const &buffers, Handler &handler)
auto buffersBegin(BufferSequence const &bufs)
std::optional< MessageHeader > parseMessageHeader(boost::system::error_code &ec, BufferSequence const &bufs, std::size_t size)
Parse a message header.
auto buffersEnd(BufferSequence const &bufs)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string protocolMessageName(int type)
Returns the name of a protocol message given its type.
protocol::MessageType protocolMessageType(protocol::TMGetLedger const &)
std::pair< std::size_t, boost::system::error_code > invokeProtocolMessage(Buffers const &buffers, Handler &handler, std::size_t &hint)
Calls the handler for up to one protocol message in the passed buffers.
std::error_code make_error_code(ripple::TokenCodecErrc e)
constexpr std::size_t maximiumMessageSize