cpp11chat/chat_networking.hpp

40 lines
933 B
C++
Raw Normal View History

#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:
asio::io_service _ios;
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:
client_network_manager(tcp::resolver::query query)
{
asio::async_connect(_socket,tcp::resolver().resolve(query),
std::bind(&client_network_manager::handshake, this));
}
void send(chat_message message)
{
_ios.post([this, message]
{ asio::async_write(_socket, asio::buffer(message.get()), []{}); });
}
};