aboutsummaryrefslogtreecommitdiff
path: root/network-handler/client.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'network-handler/client.cpp')
-rw-r--r--network-handler/client.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/network-handler/client.cpp b/network-handler/client.cpp
new file mode 100644
index 0000000..816753c
--- /dev/null
+++ b/network-handler/client.cpp
@@ -0,0 +1,39 @@
+#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(const error_code &ec, size_t read_bytes)
+{
+ if(!ec) {
+ std::string str(buffer.begin(), buffer.end());
+ std::cout << "Received: " << str << std::endl;
+ receive();
+ } else {
+ on_done();
+ }
+}
+
+void Client::receive() {
+ using namespace boost::asio::placeholders;
+
+ memset(buffer.data(), 0, buffer.size());
+ socket.async_receive(
+ boost::asio::buffer(buffer),
+ boost::bind(&Client::handle_receive, this, error(), bytes_transferred())
+ );
+}
+
+void Client::set_on_done(Client::OnDoneFT f) {
+ on_done = f;
+}