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>
27#include <xrpl/beast/utility/instrumentation.h>
28#include <xrpl/protocol/messages.h>
30#include <boost/asio/buffer.hpp>
31#include <boost/asio/buffers_iterator.hpp>
40inline protocol::MessageType
43 return protocol::mtGET_LEDGER;
46inline protocol::MessageType
49 return protocol::mtREPLAY_DELTA_REQ;
52inline protocol::MessageType
55 return protocol::mtPROOF_PATH_REQ;
59template <
class =
void>
65 case protocol::mtMANIFESTS:
67 case protocol::mtPING:
69 case protocol::mtCLUSTER:
71 case protocol::mtENDPOINTS:
73 case protocol::mtTRANSACTION:
75 case protocol::mtGET_LEDGER:
77 case protocol::mtLEDGER_DATA:
79 case protocol::mtPROPOSE_LEDGER:
81 case protocol::mtSTATUS_CHANGE:
83 case protocol::mtHAVE_SET:
85 case protocol::mtVALIDATORLIST:
86 return "validator_list";
87 case protocol::mtVALIDATORLISTCOLLECTION:
88 return "validator_list_collection";
89 case protocol::mtVALIDATION:
91 case protocol::mtGET_OBJECTS:
93 case protocol::mtHAVE_TRANSACTIONS:
94 return "have_transactions";
95 case protocol::mtTRANSACTIONS:
96 return "transactions";
97 case protocol::mtSQUELCH:
99 case protocol::mtPROOF_PATH_REQ:
100 return "proof_path_request";
101 case protocol::mtPROOF_PATH_RESPONSE:
102 return "proof_path_response";
103 case protocol::mtREPLAY_DELTA_REQ:
104 return "replay_delta_request";
105 case protocol::mtREPLAY_DELTA_RESPONSE:
106 return "replay_delta_response";
142template <
typename BufferSequence>
146 return boost::asio::buffers_iterator<BufferSequence, std::uint8_t>::begin(
150template <
typename BufferSequence>
154 return boost::asio::buffers_iterator<BufferSequence, std::uint8_t>::end(
167template <
class BufferSequence>
170 boost::system::error_code& ec,
171 BufferSequence
const& bufs,
180 "ripple::detail::parseMessageHeader : non-empty buffer");
212 for (
int i = 0; i != 4; ++i)
220 for (
int i = 0; i != 2; ++i)
223 for (
int i = 0; i != 4; ++i)
232 if ((*iter & 0xFC) == 0)
244 for (
int i = 0; i != 4; ++i)
250 for (
int i = 0; i != 2; ++i)
268 auto const m = std::make_shared<T>();
285 if (payloadSize == 0 || !m->ParseFromArray(payload.
data(), payloadSize))
288 else if (!m->ParseFromZeroCopyStream(&stream))
303 auto const m = parseMessageContent<T>(header, buffers);
308 handler.onMessageBegin(
314 handler.onMessage(m);
334template <
class Buffers,
class Handler>
337 Buffers
const& buffers,
343 auto const size = boost::asio::buffer_size(buffers);
370 if (!handler.compressionEnabled() &&
379 if (header->total_wire_size > size)
381 hint = header->total_wire_size - size;
387 switch (header->message_type)
389 case protocol::mtMANIFESTS:
390 success = detail::invoke<protocol::TMManifests>(
391 *header, buffers, handler);
393 case protocol::mtPING:
395 detail::invoke<protocol::TMPing>(*header, buffers, handler);
397 case protocol::mtCLUSTER:
399 detail::invoke<protocol::TMCluster>(*header, buffers, handler);
401 case protocol::mtENDPOINTS:
402 success = detail::invoke<protocol::TMEndpoints>(
403 *header, buffers, handler);
405 case protocol::mtTRANSACTION:
406 success = detail::invoke<protocol::TMTransaction>(
407 *header, buffers, handler);
409 case protocol::mtGET_LEDGER:
410 success = detail::invoke<protocol::TMGetLedger>(
411 *header, buffers, handler);
413 case protocol::mtLEDGER_DATA:
414 success = detail::invoke<protocol::TMLedgerData>(
415 *header, buffers, handler);
417 case protocol::mtPROPOSE_LEDGER:
418 success = detail::invoke<protocol::TMProposeSet>(
419 *header, buffers, handler);
421 case protocol::mtSTATUS_CHANGE:
422 success = detail::invoke<protocol::TMStatusChange>(
423 *header, buffers, handler);
425 case protocol::mtHAVE_SET:
426 success = detail::invoke<protocol::TMHaveTransactionSet>(
427 *header, buffers, handler);
429 case protocol::mtVALIDATION:
430 success = detail::invoke<protocol::TMValidation>(
431 *header, buffers, handler);
433 case protocol::mtVALIDATORLIST:
434 success = detail::invoke<protocol::TMValidatorList>(
435 *header, buffers, handler);
437 case protocol::mtVALIDATORLISTCOLLECTION:
438 success = detail::invoke<protocol::TMValidatorListCollection>(
439 *header, buffers, handler);
441 case protocol::mtGET_OBJECTS:
442 success = detail::invoke<protocol::TMGetObjectByHash>(
443 *header, buffers, handler);
445 case protocol::mtHAVE_TRANSACTIONS:
446 success = detail::invoke<protocol::TMHaveTransactions>(
447 *header, buffers, handler);
449 case protocol::mtTRANSACTIONS:
450 success = detail::invoke<protocol::TMTransactions>(
451 *header, buffers, handler);
453 case protocol::mtSQUELCH:
455 detail::invoke<protocol::TMSquelch>(*header, buffers, handler);
457 case protocol::mtPROOF_PATH_REQ:
458 success = detail::invoke<protocol::TMProofPathRequest>(
459 *header, buffers, handler);
461 case protocol::mtPROOF_PATH_RESPONSE:
462 success = detail::invoke<protocol::TMProofPathResponse>(
463 *header, buffers, handler);
465 case protocol::mtREPLAY_DELTA_REQ:
466 success = detail::invoke<protocol::TMReplayDeltaRequest>(
467 *header, buffers, handler);
469 case protocol::mtREPLAY_DELTA_RESPONSE:
470 success = detail::invoke<protocol::TMReplayDeltaResponse>(
471 *header, buffers, handler);
474 handler.onMessageUnknown(header->message_type);
479 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