aboutsummaryrefslogtreecommitdiff
path: root/libcmix-network/client.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libcmix-network/client.cpp')
-rw-r--r--libcmix-network/client.cpp39
1 files changed, 21 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;