2016-11-29 17:24:33 +00:00
|
|
|
#include <iostream>
|
2016-12-03 12:02:37 +00:00
|
|
|
#include <string>
|
2016-11-29 17:24:33 +00:00
|
|
|
#include <functional>
|
2016-12-03 12:02:37 +00:00
|
|
|
#include <thread>
|
2016-11-29 17:24:33 +00:00
|
|
|
#include <boost/asio.hpp>
|
2016-12-03 12:02:37 +00:00
|
|
|
#include <algorithm>
|
2016-11-29 17:24:33 +00:00
|
|
|
#include "chat_messages.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-03 12:13:06 +00:00
|
|
|
namespace chat
|
2016-11-29 17:24:33 +00:00
|
|
|
{
|
2016-12-03 12:13:06 +00:00
|
|
|
using namespace chat;
|
|
|
|
using namespace boost;
|
|
|
|
using boost::asio::ip::tcp;
|
2016-12-03 12:02:37 +00:00
|
|
|
|
|
|
|
|
2016-12-03 12:13:06 +00:00
|
|
|
class client_network_manager
|
2016-11-29 17:42:38 +00:00
|
|
|
{
|
2016-12-03 12:13:06 +00:00
|
|
|
private:
|
|
|
|
asio::io_service& _ios;
|
|
|
|
tcp::socket _socket;
|
2016-12-03 12:22:51 +00:00
|
|
|
asio::streambuf _isb;//<-------.
|
|
|
|
asio::streambuf _osb;//<---. |
|
|
|
|
std::istream _is;//--------|---'
|
|
|
|
std::ostream _os;//--------'
|
|
|
|
|
|
|
|
std::string _login;
|
2016-12-03 12:13:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void throw_if_error(boost::system::error_code& ec)
|
|
|
|
{
|
|
|
|
if (ec)
|
|
|
|
throw std::runtime_error("Networking error: " + ec.message());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string receive_message_sync()
|
|
|
|
{
|
|
|
|
std::string data;
|
2016-12-03 12:51:33 +00:00
|
|
|
asio::read_until(_socket, _isb, byte(message::TERM));
|
|
|
|
std::getline(_is, data, byte(message::TERM));
|
2016-12-03 12:13:06 +00:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void handshake(boost::system::error_code ec, tcp::resolver::iterator)
|
|
|
|
{
|
|
|
|
throw_if_error(ec);
|
|
|
|
|
|
|
|
std::cout << receive_message_sync() << '\n';
|
|
|
|
|
|
|
|
_os << chat_message(message::HELLO);
|
|
|
|
_os << chat_message(message::NEPTUN, _login);
|
|
|
|
std::reverse(_login.begin(), _login.end());
|
|
|
|
_os << chat_message(message::PASSW, _login);
|
|
|
|
asio::write(_socket, _osb);
|
|
|
|
|
|
|
|
std::cout << receive_message_sync() << '\n';
|
|
|
|
std::cout << receive_message_sync() << '\n';
|
|
|
|
std::cout << receive_message_sync() << '\n';
|
|
|
|
|
|
|
|
receive();
|
|
|
|
}
|
|
|
|
|
|
|
|
void receive()
|
|
|
|
{
|
2016-12-03 12:51:33 +00:00
|
|
|
asio::async_read_until(_socket, _isb, byte(message::TERM),
|
|
|
|
[this](boost::system::error_code ec, size_t)
|
|
|
|
{
|
|
|
|
throw_if_error(ec);
|
2016-12-03 12:13:06 +00:00
|
|
|
|
2016-12-03 12:51:33 +00:00
|
|
|
std::string data;
|
|
|
|
std::getline(_is, data, byte(message::TERM));
|
2016-12-03 12:13:06 +00:00
|
|
|
|
2016-12-03 12:51:33 +00:00
|
|
|
switch (data[0])
|
|
|
|
{
|
|
|
|
// TODO: handle stuff
|
|
|
|
}
|
|
|
|
|
|
|
|
receive();
|
|
|
|
});
|
2016-12-03 12:13:06 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 12:22:51 +00:00
|
|
|
void connect(tcp::resolver::iterator epit)
|
2016-12-03 12:13:06 +00:00
|
|
|
{
|
2016-12-03 12:22:51 +00:00
|
|
|
asio::async_connect
|
|
|
|
(
|
|
|
|
_socket, epit,
|
|
|
|
std::bind(&client_network_manager::handshake,
|
|
|
|
this, std::placeholders::_1,
|
|
|
|
std::placeholders::_2)
|
|
|
|
);
|
2016-12-03 12:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2016-12-03 12:22:51 +00:00
|
|
|
client_network_manager(asio::io_service& ioservice,
|
|
|
|
tcp::resolver::iterator epit,
|
|
|
|
std::string login)
|
|
|
|
: _ios(ioservice), _socket(_ios), _isb(), _osb(), _is(&_isb), _os(&_osb), _login(login)
|
2016-12-03 12:13:06 +00:00
|
|
|
{ connect(epit); }
|
|
|
|
|
|
|
|
void send(chat_message message)
|
|
|
|
{
|
|
|
|
_ios.post([this, message]
|
|
|
|
{
|
2016-12-03 12:22:51 +00:00
|
|
|
asio::async_write(_socket, asio::buffer(message.get()),
|
|
|
|
[](boost::system::error_code, size_t)
|
|
|
|
{});
|
2016-12-03 12:13:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|