diff options
Diffstat (limited to 'libcmix-network/client.cpp')
| -rw-r--r-- | libcmix-network/client.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/libcmix-network/client.cpp b/libcmix-network/client.cpp new file mode 100644 index 0000000..3098185 --- /dev/null +++ b/libcmix-network/client.cpp @@ -0,0 +1,41 @@ +#include "client.hpp" + +#include <boost/asio/placeholders.hpp> +#include <boost/bind.hpp> + +#include <iostream> + +using namespace boost::asio::ip; +using namespace boost::system; + +Client::Client(tcp::socket &&socket) +: socket(std::move(socket)) +, buffer() +{} + +void Client::handle_receive(MessageHandler message_handler, const error_code &ec, size_t read_bytes) +{ + buffer.commit(read_bytes); + std::istream is(&buffer); + + if(!ec) { + std::vector<uint8_t> data(std::istream_iterator<uint8_t>(is), {}); + message_handler(data); + receive(message_handler); + } else { + done(); + } +} + +void Client::receive(MessageHandler message_handler) { + using namespace boost::asio::placeholders; + + socket.async_receive( + buffer.prepare(512), + boost::bind(&Client::handle_receive, this, message_handler, error(), bytes_transferred()) + ); +} + +void Client::on_done(Client::OnDoneFT f) { + done = f; +} |
