From 720b5b3cebc7333c09ebe3ccfb1ae4184612c674 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Tue, 4 Oct 2016 15:11:49 +0200 Subject: Fixes a bug in the network which send the wrong message length. --- libcmix-network/client.cpp | 39 +++++++++++++++++++++------------------ libcmix-network/client.hpp | 1 + 2 files changed, 22 insertions(+), 18 deletions(-) (limited to 'libcmix-network') diff --git a/libcmix-network/client.cpp b/libcmix-network/client.cpp index 0c3c432..7c95234 100644 --- a/libcmix-network/client.cpp +++ b/libcmix-network/client.cpp @@ -20,6 +20,11 @@ Client::Client(tcp::socket &&socket) , done() {} +Client::~Client() +{ + close(); +} + void Client::async_connect(std::string next_host, std::string next_port, std::function on_connect) { ::async_connect(socket, next_host, next_port, on_connect); @@ -36,8 +41,10 @@ std::array Client::prepare_length_prefix(uint32_t length) void Client::send(std::string message) { + auto length_buffer = prepare_length_prefix(message.size()); + boost::array package = { - boost::asio::buffer(prepare_length_prefix(message.size())), + boost::asio::buffer(length_buffer.data(), length_buffer.size()), boost::asio::buffer(message) }; @@ -61,6 +68,19 @@ std::vector Client::received_bytes_to_vector(size_t read_bytes) return std::vector(std::istream_iterator(is), {}); } +void Client::handle_receive_message(MessageHandler message_handler, const error_code &ec, size_t read_bytes) +{ + if(!ec) { + std::vector data = received_bytes_to_vector(read_bytes); + message_handler(data); + } else { + BOOST_LOG_TRIVIAL(error) << ec; + if(done) { + done(); + } + } +} + void Client::handle_receive_size(Client::MessageHandler message_handler, const error_code& ec, size_t read_bytes) { using namespace boost::asio::placeholders; @@ -83,23 +103,6 @@ void Client::handle_receive_size(Client::MessageHandler message_handler, const e } } -void Client::handle_receive_message(MessageHandler message_handler, const error_code &ec, size_t read_bytes) -{ - if(!ec) { - buffer.commit(read_bytes); - std::istream is(&buffer); - is.unsetf(std::ios::skipws); - - std::vector data(std::istream_iterator(is), {}); - message_handler(data); - } else { - BOOST_LOG_TRIVIAL(error) << ec; - if(done) { - done(); - } - } -} - void Client::receive(MessageHandler message_handler) { using namespace boost::asio::placeholders; diff --git a/libcmix-network/client.hpp b/libcmix-network/client.hpp index e1c2e14..51dfa6f 100644 --- a/libcmix-network/client.hpp +++ b/libcmix-network/client.hpp @@ -49,6 +49,7 @@ public: * \param socket An rvalue reference to a socket it will now own and receive from. */ Client(boost::asio::ip::tcp::socket&& socket); + ~Client(); /*! * \brief async_connect Asynchronously connects to next_host:port and calls on_connect -- cgit v1.2.3-70-g09d2