aboutsummaryrefslogtreecommitdiff
path: root/libcmix-network/protobufclient.hpp
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-12-01 14:34:18 +0100
committerDennis Brentjes <d.brentjes@gmail.com>2016-12-01 14:43:00 +0100
commitbc39d05453340257fff986edfdd728f2a89d13ad (patch)
tree34fe208d1ccba0ae891dc23851352d3af6f7ee0f /libcmix-network/protobufclient.hpp
parent95283582a0392633974d1f3f67d5510a16eb104c (diff)
downloadcmix-bc39d05453340257fff986edfdd728f2a89d13ad.tar.gz
cmix-bc39d05453340257fff986edfdd728f2a89d13ad.tar.bz2
cmix-bc39d05453340257fff986edfdd728f2a89d13ad.zip
Removed a large chunk of code duplication in the network code.
Diffstat (limited to 'libcmix-network/protobufclient.hpp')
-rw-r--r--libcmix-network/protobufclient.hpp77
1 files changed, 17 insertions, 60 deletions
diff --git a/libcmix-network/protobufclient.hpp b/libcmix-network/protobufclient.hpp
index df84152..15bcaec 100644
--- a/libcmix-network/protobufclient.hpp
+++ b/libcmix-network/protobufclient.hpp
@@ -20,14 +20,13 @@
* 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
+template <typename C, typename T>
+class BaseProtobufClient : private C
{
public:
- using Client::Client;
- using Client::operator=;
- using Client::async_connect;
+ using C::C;
+ using C::operator=;
+ using C::async_connect;
/*!
* \brief an async_send wrapper that transforms a specific message to the container message an sends it
@@ -38,7 +37,7 @@ public:
void async_send(V value, F on_sent) {
typename T::proto_type m = T()(value);
- Client::async_send(m.SerializeAsString(), on_sent);
+ C::async_send(m.SerializeAsString(), on_sent);
}
/*!
@@ -65,61 +64,19 @@ public:
}
message_handler(message);
};
- Client::async_receive(f);
+ C::async_receive(f);
}
- using Client::close;
- using Client::on_done;
- using Client::is_open;
+ using C::close;
+ using C::on_done;
+ using C::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;
-};
+using ProtobufClient = BaseProtobufClient<Client, T>;
+
+/*!
+ * \brief The same as the ProtobufClient but for ssl_stream sockets.
+ */
+template <typename T>
+using SSLProtobufClient = BaseProtobufClient<SSLClient, T>;