diff --git a/src/communication/rpc/client.cpp b/src/communication/rpc/client.cpp index faf84483e..ea442c4be 100644 --- a/src/communication/rpc/client.cpp +++ b/src/communication/rpc/client.cpp @@ -19,7 +19,7 @@ namespace communication::rpc { Client::Client(const io::network::Endpoint &endpoint) : endpoint_(endpoint) {} -std::unique_ptr Client::Call(std::unique_ptr request) { +std::unique_ptr Client::Call(const Message &request) { std::lock_guard guard(mutex_); if (FLAGS_rpc_random_latency) { @@ -50,7 +50,9 @@ std::unique_ptr Client::Call(std::unique_ptr request) { std::stringstream request_stream(std::ios_base::out | std::ios_base::binary); { boost::archive::binary_oarchive request_archive(request_stream); - request_archive << request; + // Serialize reference as pointer (to serialize the derived class). The + // request is read in protocol.cpp. + request_archive << &request; // Archive destructor ensures everything is written. } diff --git a/src/communication/rpc/client.hpp b/src/communication/rpc/client.hpp index 31ecdb5c5..579b0dedc 100644 --- a/src/communication/rpc/client.hpp +++ b/src/communication/rpc/client.hpp @@ -36,8 +36,7 @@ class Client { utils::Demangle(typeid(Req).name()).value_or("unknown")); std::unique_ptr response = nullptr; stats::Stopwatch(request_name, [&] { - response = Call(std::unique_ptr( - std::make_unique(std::forward(args)...))); + response = Call(Req(std::forward(args)...)); }); auto *real_response = dynamic_cast(response.get()); if (!real_response && response) { @@ -55,7 +54,7 @@ class Client { void Abort(); private: - std::unique_ptr Call(std::unique_ptr request); + std::unique_ptr Call(const Message &request); io::network::Endpoint endpoint_; std::experimental::optional socket_; diff --git a/src/communication/rpc/protocol.cpp b/src/communication/rpc/protocol.cpp index d586f79d4..9b20d83ba 100644 --- a/src/communication/rpc/protocol.cpp +++ b/src/communication/rpc/protocol.cpp @@ -24,15 +24,18 @@ void Session::Execute() { buffer_.Resize(request_size); if (buffer_.size() < request_size) return; - std::unique_ptr request; - { + // Read the request message. + std::unique_ptr request([this, request_len]() { + Message *req_ptr = nullptr; std::stringstream stream(std::ios_base::in | std::ios_base::binary); stream.str(std::string( reinterpret_cast(buffer_.data() + sizeof(MessageSize)), request_len)); boost::archive::binary_iarchive archive(stream); - archive >> request; - } + // Sent from client.cpp + archive >> req_ptr; + return req_ptr; + }()); buffer_.Shift(sizeof(MessageSize) + request_len); auto callbacks_accessor = server_.callbacks_.access();