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-12-05 11:54:38 +00:00
|
|
|
#include <vector>
|
2016-11-29 17:24:33 +00:00
|
|
|
#include <functional>
|
|
|
|
#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-04 14:04:10 +00:00
|
|
|
/* receive_policy and send_policy must provide the static methods
|
|
|
|
* that decide what happens with the incoming / outgoing messages.
|
|
|
|
* you can read about the exact requirements in check_policy.hpp */
|
2016-12-04 13:31:26 +00:00
|
|
|
template <class receive_policy, class send_policy>
|
2016-12-03 12:13:06 +00:00
|
|
|
class client_network_manager
|
2016-11-29 17:42:38 +00:00
|
|
|
{
|
2016-12-04 13:31:26 +00:00
|
|
|
/* compile-time checks for whether policies are valid or not */
|
|
|
|
static_assert(is_valid_receive_policy<receive_policy>::value,
|
2016-12-03 17:48:16 +00:00
|
|
|
"Receive policy does not supply neccessary methods!");
|
2016-12-04 13:31:26 +00:00
|
|
|
static_assert(is_valid_send_policy<send_policy>::value,
|
|
|
|
"Send policy does not supply neccessary methods!");
|
|
|
|
|
2016-12-03 17:48:16 +00:00
|
|
|
|
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-04 14:04:10 +00:00
|
|
|
asio::streambuf _isb;//<-------. input stream
|
|
|
|
asio::streambuf _osb;//<---. | output stream
|
2016-12-03 12:22:51 +00:00
|
|
|
std::istream _is;//--------|---'
|
|
|
|
std::ostream _os;//--------'
|
|
|
|
|
|
|
|
std::string _login;
|
2016-12-05 11:54:38 +00:00
|
|
|
std::vector<std::function<void(chat_message)>> _subscriptions;
|
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)
|
|
|
|
{
|
2016-12-04 13:31:26 +00:00
|
|
|
if (!send_policy::check_msg_length(message))
|
|
|
|
return;
|
|
|
|
|
2016-12-03 13:30:55 +00:00
|
|
|
_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);
|
2016-12-04 11:51:28 +00:00
|
|
|
// TODO: decide what to do with last server message
|
2016-12-03 14:30:15 +00:00
|
|
|
_socket.close();
|
|
|
|
_ios.stop();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:54:38 +00:00
|
|
|
void subscribe(std::function<void(chat_message)> callback)
|
|
|
|
{ _subscriptions.push_back(std::move(callback)); }
|
|
|
|
|
2016-12-03 13:30:55 +00:00
|
|
|
|
|
|
|
/* internal methods */
|
|
|
|
private:
|
2016-12-04 14:04:10 +00:00
|
|
|
/* throws if something went wrong on the network */
|
|
|
|
static void throw_if_error(const boost::system::error_code& ec)
|
2016-12-03 12:13:06 +00:00
|
|
|
{
|
|
|
|
if (ec)
|
|
|
|
throw std::runtime_error("Networking error: " + ec.message());
|
|
|
|
}
|
|
|
|
|
2016-12-04 14:04:10 +00:00
|
|
|
/* asynchronously connect to the host epit points to */
|
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
|
|
|
}
|
|
|
|
|
2016-12-04 14:04:10 +00:00
|
|
|
/* handles the handshake as defined in the common protocol */
|
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);
|
2016-12-04 14:04:10 +00:00
|
|
|
std::reverse(_login.begin(), _login.end()); // passw is neptun reversed
|
2016-12-03 12:13:06 +00:00
|
|
|
_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 22:27:36 +00:00
|
|
|
std::string data;
|
|
|
|
for (int i = 0; i < 4; ++i) // hello + 3 responses for messages above
|
|
|
|
data += receive_message_sync() += '\n';
|
|
|
|
|
|
|
|
receive_policy::handshake_do_what(chat_message(message::SERVER_DIRECTION, data));
|
2016-12-03 12:13:06 +00:00
|
|
|
|
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-04 14:04:10 +00:00
|
|
|
/* helper method used by handshake() */
|
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-04 12:59:17 +00:00
|
|
|
return data.substr(1);
|
2016-12-03 13:30:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 14:04:10 +00:00
|
|
|
/* asynchronously receive a message, and decide what to do with it */
|
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));
|
|
|
|
|
2016-12-04 14:04:10 +00:00
|
|
|
char header = data[0]; // get command byte
|
|
|
|
auto content = data.substr(1, data.size()-1); // minus command & term bytes
|
2016-12-05 11:54:38 +00:00
|
|
|
chat_message msg(static_cast<message>(header), content);
|
2016-12-04 11:51:28 +00:00
|
|
|
|
2016-12-04 14:04:10 +00:00
|
|
|
switch (header) // decide what to do
|
2016-12-03 14:30:15 +00:00
|
|
|
{
|
|
|
|
case byte(message::PING):
|
|
|
|
send(chat_message(message::PONG));
|
|
|
|
break;
|
2016-12-04 11:51:28 +00:00
|
|
|
case byte(message::SERVER_DIRECTION):
|
|
|
|
receive_policy::serverdirection_do_what
|
|
|
|
(chat_message(message::SERVER_DIRECTION, content));
|
|
|
|
break;
|
2016-12-03 14:30:15 +00:00
|
|
|
case byte(message::MESSAGE):
|
2016-12-04 11:51:28 +00:00
|
|
|
receive_policy::message_do_what
|
|
|
|
(chat_message(message::MESSAGE, content));
|
|
|
|
break;
|
|
|
|
case byte(message::LOGIN):
|
|
|
|
receive_policy::login_do_what
|
|
|
|
(chat_message(message::LOGIN, content));
|
|
|
|
break;
|
|
|
|
case byte(message::LOGOUT):
|
|
|
|
receive_policy::logout_do_what
|
|
|
|
(chat_message(message::LOGOUT, content));
|
2016-12-03 14:30:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:54:38 +00:00
|
|
|
for (auto& fun : _subscriptions) // notify subscribers
|
|
|
|
fun(msg);
|
|
|
|
|
2016-12-04 14:04:10 +00:00
|
|
|
receive(); // receive the next message
|
2016-12-03 14:30:15 +00:00
|
|
|
});
|
2016-12-03 12:13:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|