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;
}
#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.
*
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<AsioTlsStream>& stream, const String& json, boost::asio::yield_context yc)
{
#ifdef I2_DEBUG
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<AsioTlsStream>& stream, ssize_t maxMessageLength)
{
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<AsioTlsStream>& stream, boost::asio::yield_context yc, ssize_t maxMessageLength)
{
String jsonString = NetString::ReadStringFromStream(stream, yc, maxMessageLength);
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);
class JsonRpc
{
public:
- static size_t SendMessage(const Stream::Ptr& stream, const Dictionary::Ptr& message);
static size_t SendMessage(const std::shared_ptr<AsioTlsStream>& stream, const Dictionary::Ptr& message);
static size_t SendMessage(const std::shared_ptr<AsioTlsStream>& stream, const Dictionary::Ptr& message, boost::asio::yield_context yc);
static size_t SendRawMessage(const std::shared_ptr<AsioTlsStream>& 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<AsioTlsStream>& stream, ssize_t maxMessageLength = -1);
static String ReadMessage(const std::shared_ptr<AsioTlsStream>& stream, boost::asio::yield_context yc, ssize_t maxMessageLength = -1);
+
static Dictionary::Ptr DecodeMessage(const String& message);
private: