]> granicus.if.org Git - icinga2/commitdiff
JsonRpc::SendMessage(): add Boost ASIO overload
authorAlexander A. Klimov <alexander.klimov@icinga.com>
Mon, 18 Feb 2019 14:23:41 +0000 (15:23 +0100)
committerAlexander A. Klimov <alexander.klimov@icinga.com>
Mon, 1 Apr 2019 09:40:14 +0000 (11:40 +0200)
lib/remote/jsonrpc.cpp
lib/remote/jsonrpc.hpp

index 1c429ea7cd0d30a1a985cde662c979e24ff91d8d..b07d0e6ed1250533539901c9d2e3f16990d918c5 100644 (file)
@@ -6,7 +6,10 @@
 #include "base/console.hpp"
 #include "base/scriptglobal.hpp"
 #include "base/convert.hpp"
+#include "base/tlsstream.hpp"
 #include <iostream>
+#include <memory>
+#include <boost/asio/spawn.hpp>
 
 using namespace icinga;
 
@@ -55,6 +58,25 @@ size_t JsonRpc::SendMessage(const Stream::Ptr& stream, const Dictionary::Ptr& me
        return NetString::WriteStringToStream(stream, json);
 }
 
+/**
+ * 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 std::shared_ptr<AsioTlsStream>& stream, const Dictionary::Ptr& message, boost::asio::yield_context yc)
+{
+       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, yc);
+}
+
 StreamReadStatus JsonRpc::ReadMessage(const Stream::Ptr& stream, String *message, StreamReadContext& src, bool may_wait, ssize_t maxMessageLength)
 {
        String jsonString;
index 44e8a9360500099a201d74d1c10062e2d92a3d68..2efe8022b0cc1f7d47f79cd5fe4608412cfe35f2 100644 (file)
@@ -5,7 +5,10 @@
 
 #include "base/stream.hpp"
 #include "base/dictionary.hpp"
+#include "base/tlsstream.hpp"
 #include "remote/i2-remote.hpp"
+#include <memory>
+#include <boost/asio/spawn.hpp>
 
 namespace icinga
 {
@@ -19,6 +22,7 @@ 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, 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 Dictionary::Ptr DecodeMessage(const String& message);