2016-12-03 17:18:08 +00:00
|
|
|
#pragma once
|
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 17:48:16 +00:00
|
|
|
#include "check_policy.hpp"
|
2016-11-29 17:24:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
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 15:26:13 +00:00
|
|
|
struct receive_policy_stdout
|
|
|
|
{
|
|
|
|
static void message_do_what(chat_message msg)
|
2016-12-03 17:48:16 +00:00
|
|
|
{ std::cout << msg << '\n'; }
|
2016-12-03 15:26:13 +00:00
|
|
|
|
|
|
|
// TODO: handshake_policy
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* most ezt így üres template paraméterekkel kell példányosítani,
|
2016-12-03 17:48:16 +00:00
|
|
|
* már amennyiben nem akarunk valami custom receive policyt megadni,
|
2016-12-03 15:26:13 +00:00
|
|
|
* pl.: client_network_manager<> cnm;
|
|
|
|
* meg lehet oldani úgy, hogy ne kelljen? usingot/typedefet nem enged saját magára */
|
|
|
|
template <class receive_policy = receive_policy_stdout>
|
2016-12-03 12:13:06 +00:00
|
|
|
class client_network_manager
|
2016-11-29 17:42:38 +00:00
|
|
|
{
|
2016-12-03 17:48:16 +00:00
|
|
|
/* compile-time check for whether receive_policy is valid or not */
|
|
|
|
static_assert(is_valid_policy<receive_policy>::value,
|
|
|
|
"Receive policy does not supply neccessary methods!");
|
|
|
|
|
2016-12-03 13:30:55 +00:00
|
|
|
/* members */
|
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
|
|
|
|
|
|
|
|
2016-12-03 13:30:55 +00:00
|
|
|
/* interface */
|
|
|
|
public:
|
|
|
|
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)
|
|
|
|
{ connect(epit); }
|
|
|
|
|
|
|
|
void send(chat_message message)
|
|
|
|
{
|
|
|
|
_ios.post([this, message]
|
|
|
|
{
|
|
|
|
asio::async_write(_socket, asio::buffer(message.get()),
|
|
|
|
[](boost::system::error_code, size_t)
|
|
|
|
{});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-03 14:30:15 +00:00
|
|
|
void close_connection()
|
|
|
|
{
|
|
|
|
_ios.post([this]
|
|
|
|
{
|
|
|
|
_os << chat_message(message::BYE);
|
|
|
|
asio::write(_socket, _osb);
|
|
|
|
// TODO: handle last server message
|
|
|
|
_socket.close();
|
|
|
|
_ios.stop();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-03 13:30:55 +00:00
|
|
|
|
|
|
|
/* internal methods */
|
|
|
|
private:
|
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());
|
|
|
|
}
|
|
|
|
|
2016-12-03 13:30:55 +00:00
|
|
|
void connect(tcp::resolver::iterator epit)
|
2016-12-03 12:13:06 +00:00
|
|
|
{
|
2016-12-03 13:30:55 +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
|
|
|
}
|
|
|
|
|
|
|
|
void handshake(boost::system::error_code ec, tcp::resolver::iterator)
|
|
|
|
{
|
|
|
|
throw_if_error(ec);
|
|
|
|
|
|
|
|
_os << chat_message(message::HELLO);
|
|
|
|
_os << chat_message(message::NEPTUN, _login);
|
|
|
|
std::reverse(_login.begin(), _login.end());
|
|
|
|
_os << chat_message(message::PASSW, _login);
|
2016-12-03 14:30:15 +00:00
|
|
|
asio::write(_socket, _osb); // handshake is handled synchronously
|
2016-12-03 12:13:06 +00:00
|
|
|
|
2016-12-03 14:30:15 +00:00
|
|
|
// TODO: do something with these couts (decide whether to display this or not)
|
|
|
|
std::cout << receive_message_sync() << '\n';
|
2016-12-03 12:13:06 +00:00
|
|
|
std::cout << receive_message_sync() << '\n';
|
|
|
|
std::cout << receive_message_sync() << '\n';
|
|
|
|
std::cout << receive_message_sync() << '\n';
|
|
|
|
|
2016-12-03 14:30:15 +00:00
|
|
|
receive(); // then flow goes to receive-loop
|
2016-12-03 12:13:06 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 13:30:55 +00:00
|
|
|
std::string receive_message_sync()
|
|
|
|
{
|
2016-12-03 14:30:15 +00:00
|
|
|
boost::system::error_code ec;
|
|
|
|
|
2016-12-03 13:30:55 +00:00
|
|
|
std::string data;
|
2016-12-03 14:30:15 +00:00
|
|
|
asio::read_until(_socket, _isb, byte(message::TERM), ec);
|
2016-12-03 13:30:55 +00:00
|
|
|
std::getline(_is, data, byte(message::TERM));
|
|
|
|
|
2016-12-03 14:30:15 +00:00
|
|
|
throw_if_error(ec);
|
2016-12-03 13:30:55 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-12-03 12:13:06 +00:00
|
|
|
void receive()
|
|
|
|
{
|
2016-12-03 14:30:15 +00:00
|
|
|
asio::async_read_until
|
|
|
|
(_socket, _isb, byte(message::TERM),
|
|
|
|
[this](boost::system::error_code ec, size_t)
|
|
|
|
{
|
|
|
|
throw_if_error(ec);
|
|
|
|
|
|
|
|
std::string data;
|
|
|
|
std::getline(_is, data, byte(message::TERM));
|
|
|
|
|
|
|
|
switch (data[0])
|
|
|
|
{
|
|
|
|
// TODO: handle stuff
|
|
|
|
case byte(message::PING):
|
|
|
|
send(chat_message(message::PONG));
|
|
|
|
break;
|
|
|
|
case byte(message::MESSAGE):
|
2016-12-03 15:26:13 +00:00
|
|
|
chat_message msg(message::MESSAGE, data);
|
|
|
|
receive_policy::message_do_what(msg);
|
2016-12-03 14:30:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
receive();
|
|
|
|
});
|
2016-12-03 12:13:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|