From bc39d05453340257fff986edfdd728f2a89d13ad Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Thu, 1 Dec 2016 14:34:18 +0100 Subject: Removed a large chunk of code duplication in the network code. --- libcmix-network/client.hpp | 31 ++++++--------- libcmix-network/protobufclient.hpp | 77 +++++++++----------------------------- 2 files changed, 28 insertions(+), 80 deletions(-) (limited to 'libcmix-network') diff --git a/libcmix-network/client.hpp b/libcmix-network/client.hpp index b43f32b..7b2b16f 100644 --- a/libcmix-network/client.hpp +++ b/libcmix-network/client.hpp @@ -23,11 +23,13 @@ */ /*! - * \brief The Client class + * \brief Forward decl for the friend decleration in BaseClient; */ - struct SSLClient; +/*! + * \brief The BaseClient class takes care of all ssl agnostic operations. + */ template class BaseClient { friend SSLClient; @@ -209,25 +211,14 @@ public: } }; -struct Client : private BaseClient { - - using BaseClient::BaseClient; - using BaseClient::operator=; - - using BaseClient::async_receive; - - using BaseClient::async_send; - - using BaseClient::async_connect; - - using BaseClient::close; - - using BaseClient::on_done; - - using BaseClient::is_open; - -}; +/*! + * \brief Conveniance wrapper around BaseClient for non SSL sockets. + */ +using Client = BaseClient; +/*! + * \brief The SSLClient struct A wrapper around BaseClient that takes care of some of the SSL specific things. + */ struct SSLClient : private BaseClient> { using BaseClient::BaseClient; 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 -class ProtobufClient : private Client +template +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 -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 - 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 - 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 - void async_receive(F message_handler) { - auto f = [message_handler](std::vector 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; + +/*! + * \brief The same as the ProtobufClient but for ssl_stream sockets. + */ +template +using SSLProtobufClient = BaseProtobufClient; -- cgit v1.2.3-70-g09d2