aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--network-handler/client.cpp10
-rw-r--r--network-handler/client.hpp3
-rw-r--r--network-handler/networkhandler.cpp5
3 files changed, 11 insertions, 7 deletions
diff --git a/network-handler/client.cpp b/network-handler/client.cpp
index 816753c..c2825d7 100644
--- a/network-handler/client.cpp
+++ b/network-handler/client.cpp
@@ -15,9 +15,12 @@ Client::Client(tcp::socket &&socket)
void Client::handle_receive(const error_code &ec, size_t read_bytes)
{
+ buffer.commit(read_bytes);
+ std::istream is(&buffer);
+
if(!ec) {
- std::string str(buffer.begin(), buffer.end());
- std::cout << "Received: " << str << std::endl;
+ std::vector<uint8_t> data(std::istream_iterator<uint8_t>(is), {});
+ std::cout << "Received: " << std::string(data.begin(), data.end()) << std::endl;
receive();
} else {
on_done();
@@ -27,9 +30,8 @@ void Client::handle_receive(const error_code &ec, size_t read_bytes)
void Client::receive() {
using namespace boost::asio::placeholders;
- memset(buffer.data(), 0, buffer.size());
socket.async_receive(
- boost::asio::buffer(buffer),
+ buffer.prepare(512),
boost::bind(&Client::handle_receive, this, error(), bytes_transferred())
);
}
diff --git a/network-handler/client.hpp b/network-handler/client.hpp
index 878c5be..e71edb2 100644
--- a/network-handler/client.hpp
+++ b/network-handler/client.hpp
@@ -1,6 +1,7 @@
#pragma once
#include <boost/asio/ip/tcp.hpp>
+#include <boost/asio/streambuf.hpp>
#include <array>
@@ -10,8 +11,8 @@ public:
private:
boost::asio::ip::tcp::socket socket;
- std::array<uint8_t, 20> buffer;
OnDoneFT on_done;
+ boost::asio::streambuf buffer;
public:
diff --git a/network-handler/networkhandler.cpp b/network-handler/networkhandler.cpp
index 8c5997f..4b1926d 100644
--- a/network-handler/networkhandler.cpp
+++ b/network-handler/networkhandler.cpp
@@ -18,10 +18,11 @@ NetworkHandler::NetworkHandler(const ListenSettings& listen_settings)
, clients()
{
auto accept_handler = [this](tcp::socket&& socket) {
- clients.push_back(Client(std::move(socket)));
+ clients.emplace_back(std::move(socket));
+
auto it = --clients.end();
- clients.back().set_on_done(
+ clients.back().on_done(
[this, it]() {
clients.erase(it);
}