diff --git a/lib/remote/jsonrpc.cpp b/lib/remote/jsonrpc.cpp index 63bc5ff85..d17b5780f 100644 --- a/lib/remote/jsonrpc.cpp +++ b/lib/remote/jsonrpc.cpp @@ -15,6 +15,11 @@ using namespace icinga; #ifdef I2_DEBUG +/** + * Determine whether the developer wants to see raw JSON messages. + * + * @return Internal.DebugJsonRpc boolean + */ static bool GetDebugJsonRpcCached() { static int debugJsonRpc = -1; @@ -40,25 +45,6 @@ static bool GetDebugJsonRpcCached() } #endif /* I2_DEBUG */ -/** - * Sends a message to the connected peer and returns the bytes sent. - * - * @param message The message. - * - * @return The amount of bytes sent. - */ -size_t JsonRpc::SendMessage(const Stream::Ptr& stream, const Dictionary::Ptr& message) -{ - String json = JsonEncode(message); - -#ifdef I2_DEBUG - if (GetDebugJsonRpcCached()) - std::cerr << ConsoleColorTag(Console_ForegroundBlue) << ">> " << json << ConsoleColorTag(Console_Normal) << "\n"; -#endif /* I2_DEBUG */ - - return NetString::WriteStringToStream(stream, json); -} - /** * Sends a message to the connected peer and returns the bytes sent. * @@ -90,13 +76,15 @@ size_t JsonRpc::SendMessage(const std::shared_ptr& stream, const return JsonRpc::SendRawMessage(stream, JsonEncode(message), yc); } -/** - * Sends a message to the connected peer and returns the bytes sent. - * - * @param message The message. - * - * @return The amount of bytes sent. - */ + /** + * Sends a raw message to the connected peer. + * + * @param stream ASIO TLS Stream + * @param json message + * @param yc Yield context required for ASIO + * + * @return bytes sent + */ size_t JsonRpc::SendRawMessage(const std::shared_ptr& stream, const String& json, boost::asio::yield_context yc) { #ifdef I2_DEBUG @@ -107,23 +95,14 @@ size_t JsonRpc::SendRawMessage(const std::shared_ptr& stream, con return NetString::WriteStringToStream(stream, json, yc); } -StreamReadStatus JsonRpc::ReadMessage(const Stream::Ptr& stream, String *message, StreamReadContext& src, bool may_wait, ssize_t maxMessageLength) -{ - String jsonString; - StreamReadStatus srs = NetString::ReadStringFromStream(stream, &jsonString, src, may_wait, maxMessageLength); - - if (srs != StatusNewItem) - return srs; - - *message = jsonString; - -#ifdef I2_DEBUG - if (GetDebugJsonRpcCached()) - std::cerr << ConsoleColorTag(Console_ForegroundBlue) << "<< " << jsonString << ConsoleColorTag(Console_Normal) << "\n"; -#endif /* I2_DEBUG */ - - return StatusNewItem; -} +/** + * Reads a message from the connected peer. + * + * @param stream ASIO TLS Stream + * @param maxMessageLength maximum size of bytes read. + * + * @return A JSON string + */ String JsonRpc::ReadMessage(const std::shared_ptr& stream, ssize_t maxMessageLength) { @@ -137,6 +116,15 @@ String JsonRpc::ReadMessage(const std::shared_ptr& stream, ssize_ return std::move(jsonString); } +/** + * Reads a message from the connected peer. + * + * @param stream ASIO TLS Stream + * @param yc Yield Context for ASIO + * @param maxMessageLength maximum size of bytes read. + * + * @return A JSON string + */ String JsonRpc::ReadMessage(const std::shared_ptr& stream, boost::asio::yield_context yc, ssize_t maxMessageLength) { String jsonString = NetString::ReadStringFromStream(stream, yc, maxMessageLength); @@ -149,6 +137,13 @@ String JsonRpc::ReadMessage(const std::shared_ptr& stream, boost: return std::move(jsonString); } +/** + * Decode message, enforce a Dictionary + * + * @param message JSON string + * + * @return Dictionary ptr + */ Dictionary::Ptr JsonRpc::DecodeMessage(const String& message) { Value value = JsonDecode(message); diff --git a/lib/remote/jsonrpc.hpp b/lib/remote/jsonrpc.hpp index 98187fe6c..cc4cc7b6d 100644 --- a/lib/remote/jsonrpc.hpp +++ b/lib/remote/jsonrpc.hpp @@ -21,13 +21,13 @@ namespace icinga class JsonRpc { public: - static size_t SendMessage(const Stream::Ptr& stream, const Dictionary::Ptr& message); static size_t SendMessage(const std::shared_ptr& stream, const Dictionary::Ptr& message); static size_t SendMessage(const std::shared_ptr& stream, const Dictionary::Ptr& message, boost::asio::yield_context yc); static size_t SendRawMessage(const std::shared_ptr& stream, const String& json, boost::asio::yield_context yc); - static StreamReadStatus ReadMessage(const Stream::Ptr& stream, String *message, StreamReadContext& src, bool may_wait = false, ssize_t maxMessageLength = -1); + static String ReadMessage(const std::shared_ptr& stream, ssize_t maxMessageLength = -1); static String ReadMessage(const std::shared_ptr& stream, boost::asio::yield_context yc, ssize_t maxMessageLength = -1); + static Dictionary::Ptr DecodeMessage(const String& message); private: