diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2016-08-29 13:29:30 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2016-08-29 13:29:30 +0200 |
| commit | a736abb4e3668771f248e7400a093cb40b5313b7 (patch) | |
| tree | ccd7b225f1138dc2743ee64aadc7bf11635c9bc9 /network-handler/client.cpp | |
| parent | 00ab2cf6add2976b3a4e8f8cc488777ad5c27808 (diff) | |
| download | cmix-a736abb4e3668771f248e7400a093cb40b5313b7.tar.gz cmix-a736abb4e3668771f248e7400a093cb40b5313b7.tar.bz2 cmix-a736abb4e3668771f248e7400a093cb40b5313b7.zip | |
Adds the Acceptor and Client class.
The acceptor combines the endpoint and acceptor to reduce clutter in
NetworkHandler class.
The client will handle the connection for the network handler for every
separate client.
The networkhander now has a std::list of Client, to keep track and
ownership of all the Clients.
Diffstat (limited to 'network-handler/client.cpp')
| -rw-r--r-- | network-handler/client.cpp | 39 |
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; +} |
