2016-11-29 17:24:33 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <functional>
|
|
|
|
#include <boost/asio.hpp>
|
|
|
|
#include "chat_messages.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class client_network_manager
|
|
|
|
{
|
|
|
|
using namespace boost;
|
|
|
|
using boost::asio::ip::tcp;
|
|
|
|
|
|
|
|
private:
|
2016-11-29 17:42:38 +00:00
|
|
|
asio::io_service _ios;
|
2016-11-29 17:24:33 +00:00
|
|
|
tcp::socket _socket;
|
|
|
|
asio::streambuf _sb;
|
|
|
|
std::istream _is{&sb};
|
|
|
|
std::ostream _os{&sb};
|
|
|
|
|
|
|
|
void handshake(boost::system::error_code ec, size_t)
|
|
|
|
{
|
|
|
|
if (ec)
|
|
|
|
throw std::runtime_error("Networking error: " + ec.message());
|
|
|
|
|
|
|
|
// TODO: do handshake & read-loop
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2016-11-29 17:42:38 +00:00
|
|
|
client_network_manager(tcp::resolver::query query)
|
2016-11-29 17:24:33 +00:00
|
|
|
{
|
|
|
|
asio::async_connect(_socket,tcp::resolver().resolve(query),
|
|
|
|
std::bind(&client_network_manager::handshake, this));
|
|
|
|
}
|
2016-11-29 17:42:38 +00:00
|
|
|
|
|
|
|
void send(chat_message message)
|
|
|
|
{
|
|
|
|
_ios.post([this, message]
|
|
|
|
{ asio::async_write(_socket, asio::buffer(message.get()), []{}); });
|
|
|
|
}
|
2016-11-29 17:24:33 +00:00
|
|
|
};
|