//------------------------------------------------------------------------------ /* This file is part of rippled: https://github.com/ripple/rippled Copyright (c) 2012, 2013 Ripple Labs Inc. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #ifndef RIPPLE_OVERLAY_PROTOCOLMESSAGE_H_INCLUDED #define RIPPLE_OVERLAY_PROTOCOLMESSAGE_H_INCLUDED #include #include #include #include #include #include #include #include #include #include #include namespace ripple { /** Returns the name of a protocol message given its type. */ template std::string protocolMessageName (int type) { switch (type) { case protocol::mtHELLO: return "hello"; case protocol::mtMANIFESTS: return "manifests"; case protocol::mtPING: return "ping"; case protocol::mtPROOFOFWORK: return "proof_of_work"; case protocol::mtCLUSTER: return "cluster"; case protocol::mtGET_SHARD_INFO: return "get_shard_info"; case protocol::mtSHARD_INFO: return "shard_info"; case protocol::mtGET_PEER_SHARD_INFO: return "get_peer_shard_info"; case protocol::mtPEER_SHARD_INFO: return "peer_shard_info"; case protocol::mtGET_PEERS: return "get_peers"; case protocol::mtPEERS: return "peers"; case protocol::mtENDPOINTS: return "endpoints"; case protocol::mtTRANSACTION: return "tx"; case protocol::mtGET_LEDGER: return "get_ledger"; case protocol::mtLEDGER_DATA: return "ledger_data"; case protocol::mtPROPOSE_LEDGER: return "propose"; case protocol::mtSTATUS_CHANGE: return "status"; case protocol::mtHAVE_SET: return "have_set"; case protocol::mtVALIDATION: return "validation"; case protocol::mtGET_OBJECTS: return "get_objects"; default: break; }; return "unknown"; } namespace detail { template std::enable_if_t::value, boost::system::error_code> invoke (int type, Buffers const& buffers, Handler& handler) { ZeroCopyInputStream stream(buffers); stream.Skip(Message::kHeaderBytes); auto const m (std::make_shared()); if (! m->ParseFromZeroCopyStream(&stream)) return boost::system::errc::make_error_code( boost::system::errc::invalid_argument); auto ec = handler.onMessageBegin (type, m, Message::kHeaderBytes + Message::size (buffers)); if (! ec) { handler.onMessage (m); handler.onMessageEnd (type, m); } return ec; } } /** Calls the handler for up to one protocol message in the passed buffers. If there is insufficient data to produce a complete protocol message, zero is returned for the number of bytes consumed. @return The number of bytes consumed, or the error code if any. */ template std::pair invokeProtocolMessage (Buffers const& buffers, Handler& handler) { std::pair result = { 0, {} }; boost::system::error_code& ec = result.second; auto const bs = boost::asio::buffer_size(buffers); // If we don't even have enough bytes for the header, there's no point // in doing any work. if (bs < Message::kHeaderBytes) return result; if (bs > Message::kMaxMessageSize) { result.second = make_error_code(boost::system::errc::message_size); return result; } auto const size = Message::kHeaderBytes + Message::size(buffers); if (bs < size) return result; auto const type = Message::type(buffers); switch (type) { case protocol::mtHELLO: ec = detail::invoke (type, buffers, handler); break; case protocol::mtMANIFESTS: ec = detail::invoke (type, buffers, handler); break; case protocol::mtPING: ec = detail::invoke (type, buffers, handler); break; case protocol::mtCLUSTER: ec = detail::invoke (type, buffers, handler); break; case protocol::mtGET_SHARD_INFO: ec = detail::invoke (type, buffers, handler); break; case protocol::mtSHARD_INFO: ec = detail::invoke(type, buffers, handler); break; case protocol::mtGET_PEER_SHARD_INFO: ec = detail::invoke (type, buffers, handler); break; case protocol::mtPEER_SHARD_INFO: ec = detail::invoke(type, buffers, handler); break; case protocol::mtGET_PEERS: ec = detail::invoke (type, buffers, handler); break; case protocol::mtPEERS: ec = detail::invoke (type, buffers, handler); break; case protocol::mtENDPOINTS: ec = detail::invoke (type, buffers, handler); break; case protocol::mtTRANSACTION: ec = detail::invoke (type, buffers, handler); break; case protocol::mtGET_LEDGER: ec = detail::invoke (type, buffers, handler); break; case protocol::mtLEDGER_DATA: ec = detail::invoke (type, buffers, handler); break; case protocol::mtPROPOSE_LEDGER: ec = detail::invoke (type, buffers, handler); break; case protocol::mtSTATUS_CHANGE: ec = detail::invoke (type, buffers, handler); break; case protocol::mtHAVE_SET: ec = detail::invoke (type, buffers, handler); break; case protocol::mtVALIDATION: ec = detail::invoke (type, buffers, handler); break; case protocol::mtGET_OBJECTS: ec = detail::invoke (type, buffers, handler); break; default: ec = handler.onMessageUnknown (type); break; } if (! ec) result.first = size; return result; } /** Write a protocol message to a streambuf. */ template void write (Streambuf& streambuf, ::google::protobuf::Message const& m, int type, std::size_t blockBytes) { auto const size = m.ByteSize(); std::array v; v[0] = static_cast((size >> 24) & 0xFF); v[1] = static_cast((size >> 16) & 0xFF); v[2] = static_cast((size >> 8) & 0xFF); v[3] = static_cast( size & 0xFF); v[4] = static_cast((type >> 8) & 0xFF); v[5] = static_cast( type & 0xFF); streambuf.commit(boost::asio::buffer_copy( streambuf.prepare(Message::kHeaderBytes), boost::asio::buffer(v))); ZeroCopyOutputStream stream ( streambuf, blockBytes); m.SerializeToZeroCopyStream(&stream); } } // ripple #endif