aboutsummaryrefslogtreecommitdiff
path: root/libcmix-network/client.cpp
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-08-31 11:57:15 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2016-08-31 11:57:15 +0200
commit2f1c3293d2c5776c4ecd9b2f1dce66492b15dbdd (patch)
tree1c582e4c5715a93c493582b1e01e51bc960d56c0 /libcmix-network/client.cpp
parent1525c5defe3db08c765477003be73c68bb2c3cb7 (diff)
downloadcmix-2f1c3293d2c5776c4ecd9b2f1dce66492b15dbdd.tar.gz
cmix-2f1c3293d2c5776c4ecd9b2f1dce66492b15dbdd.tar.bz2
cmix-2f1c3293d2c5776c4ecd9b2f1dce66492b15dbdd.zip
Split up the client and server parts in a separate network library
Diffstat (limited to 'libcmix-network/client.cpp')
-rw-r--r--libcmix-network/client.cpp41
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;
+}