diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2016-10-04 15:11:49 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2016-10-04 15:11:49 +0200 |
| commit | 720b5b3cebc7333c09ebe3ccfb1ae4184612c674 (patch) | |
| tree | d790fd3ffdcb29cb28853ae85694167c4f7b090d /libcmix-network | |
| parent | 4f2c980343b321e7fccf48190c32bfb4bca426f4 (diff) | |
| download | cmix-720b5b3cebc7333c09ebe3ccfb1ae4184612c674.tar.gz cmix-720b5b3cebc7333c09ebe3ccfb1ae4184612c674.tar.bz2 cmix-720b5b3cebc7333c09ebe3ccfb1ae4184612c674.zip | |
Fixes a bug in the network which send the wrong message length.
Diffstat (limited to 'libcmix-network')
| -rw-r--r-- | libcmix-network/client.cpp | 39 | ||||
| -rw-r--r-- | libcmix-network/client.hpp | 1 |
2 files changed, 22 insertions, 18 deletions
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<void ()> on_connect) { ::async_connect(socket, next_host, next_port, on_connect); @@ -36,8 +41,10 @@ std::array<uint8_t, 4> Client::prepare_length_prefix(uint32_t length) void Client::send(std::string message) { + auto length_buffer = prepare_length_prefix(message.size()); + boost::array<boost::asio::const_buffer, 2> 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<uint8_t> Client::received_bytes_to_vector(size_t read_bytes) return std::vector<uint8_t>(std::istream_iterator<uint8_t>(is), {}); } +void Client::handle_receive_message(MessageHandler message_handler, const error_code &ec, size_t read_bytes) +{ + if(!ec) { + std::vector<uint8_t> 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<uint8_t> data(std::istream_iterator<uint8_t>(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 |
