aboutsummaryrefslogtreecommitdiff
path: root/libcmix-network/protobufclient.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'libcmix-network/protobufclient.hpp')
-rw-r--r--libcmix-network/protobufclient.hpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/libcmix-network/protobufclient.hpp b/libcmix-network/protobufclient.hpp
index 247b71f..df84152 100644
--- a/libcmix-network/protobufclient.hpp
+++ b/libcmix-network/protobufclient.hpp
@@ -4,8 +4,13 @@
#include "logging.hpp"
+#include <boost/asio/ip/tcp.hpp>
+#include <boost/asio/ssl.hpp>
+
#include <type_traits>
+#include <iostream>
+
/*!
* \file
*/
@@ -15,6 +20,7 @@
* in to a desired wireformat to serialize and send or receive via the Client.
* \tparam T The Protobuf functor to use when transforming messages.
*/
+
template <typename T>
class ProtobufClient : private Client
{
@@ -31,6 +37,7 @@ public:
template<typename V, typename F>
void async_send(V value, F on_sent) {
typename T::proto_type m = T()(value);
+
Client::async_send(m.SerializeAsString(), on_sent);
}
@@ -65,3 +72,54 @@ public:
using Client::on_done;
using Client::is_open;
};
+
+template <typename T>
+class SSLProtobufClient : private SSLClient
+{
+public:
+ using SSLClient::SSLClient;
+ using SSLClient::operator=;
+ using SSLClient::async_connect;
+
+ /*!
+ * \brief an async_send wrapper that transforms a specific message to the container message an sends it
+ * \param value The specific message to send.
+ * \param on_sent The function to call after succesfully sending the message.
+ */
+ template<typename V, typename F>
+ void async_send(V value, F on_sent) {
+ typename T::proto_type m = T()(value);
+ SSLClient::async_send(m.SerializeAsString(), on_sent);
+ }
+
+ /*!
+ * \brief an async_send wrapper, like the above but without the on_sent callback.
+ * \param value The specific message to send.
+ */
+ template<typename V>
+ void async_send(V value) {
+ async_send(value, []{});
+ }
+
+ /*!
+ * \brief An async_receive wrapper that transforms the wireformat to the protobuf
+ * container message type.
+ * \param message_handler The callback to call after receiving a message.
+ */
+ template <typename F>
+ void async_receive(F message_handler) {
+ auto f = [message_handler](std::vector<uint8_t> const& buffer) {
+ typename T::proto_type message;
+ if(!message.ParseFromArray(buffer.data(), buffer.size())) {
+ BOOST_LOG_TRIVIAL(error) << "Received something which was not a CMixMessage";
+ throw std::runtime_error("Network communication was disrupted in a unrecoverable way.");
+ }
+ message_handler(message);
+ };
+ SSLClient::async_receive(f);
+ }
+
+ using SSLClient::close;
+ using SSLClient::on_done;
+ using SSLClient::is_open;
+};