diff --git a/src/communication/rpc/client.cpp b/src/communication/rpc/client.cpp index c4bb677ee..f77410216 100644 --- a/src/communication/rpc/client.cpp +++ b/src/communication/rpc/client.cpp @@ -62,9 +62,12 @@ std::unique_ptr Client::Call(std::unique_ptr request) { } // Serialize and send request. - std::stringstream request_stream; - boost::archive::binary_oarchive request_archive(request_stream); - request_archive << request; + std::stringstream request_stream(std::ios_base::out | std::ios_base::binary); + { + boost::archive::binary_oarchive request_archive(request_stream); + request_archive << request; + // Archive destructor ensures everything is written. + } const std::string &request_buffer = request_stream.str(); MessageSize request_data_size = request_buffer.size(); @@ -104,14 +107,17 @@ std::unique_ptr Client::Call(std::unique_ptr request) { sizeof(uint32_t) + sizeof(MessageSize) + response_data_size; if (received_bytes_ < response_size) continue; - std::stringstream response_stream; - response_stream.str( - std::string(reinterpret_cast(buffer_.data() + sizeof(uint32_t) + - sizeof(MessageSize)), - response_data_size)); - boost::archive::binary_iarchive response_archive(response_stream); std::unique_ptr response; - response_archive >> response; + { + std::stringstream response_stream(std::ios_base::in | + std::ios_base::binary); + response_stream.str(std::string( + reinterpret_cast(buffer_.data() + sizeof(uint32_t) + + sizeof(MessageSize)), + response_data_size)); + boost::archive::binary_iarchive response_archive(response_stream); + response_archive >> response; + } std::copy(buffer_.begin() + response_size, buffer_.begin() + received_bytes_, buffer_.begin()); diff --git a/src/communication/rpc/protocol.cpp b/src/communication/rpc/protocol.cpp index 5f012188e..783c7151c 100644 --- a/src/communication/rpc/protocol.cpp +++ b/src/communication/rpc/protocol.cpp @@ -38,14 +38,16 @@ void Session::Execute() { return; // TODO (mferencevic): check for exceptions - std::stringstream stream; - stream.str( - std::string(reinterpret_cast(buffer_.data() + sizeof(uint32_t) + - sizeof(MessageSize)), - message_len)); - boost::archive::binary_iarchive archive(stream); std::unique_ptr message; - archive >> message; + { + std::stringstream stream(std::ios_base::in | std::ios_base::binary); + stream.str( + std::string(reinterpret_cast(buffer_.data() + sizeof(uint32_t) + + sizeof(MessageSize)), + message_len)); + boost::archive::binary_iarchive archive(stream); + archive >> message; + } buffer_.Shift(sizeof(uint32_t) + sizeof(MessageSize) + message_len); system_.AddTask(socket_, service_name_, message_id, std::move(message)); @@ -68,9 +70,12 @@ void SendMessage(Socket &socket, uint32_t message_id, CHECK(message) << "Trying to send nullptr instead of message"; // Serialize and send message - std::stringstream stream; - boost::archive::binary_oarchive archive(stream); - archive << message; + std::stringstream stream(std::ios_base::out | std::ios_base::binary); + { + boost::archive::binary_oarchive archive(stream); + archive << message; + // Archive destructor ensures everything is written. + } const std::string &buffer = stream.str(); uint64_t message_size = sizeof(MessageSize) + buffer.size();