moved everything to namespace chat to avoid namespace pollution

This commit is contained in:
Kjistóf 2016-12-03 13:13:06 +01:00
parent 07284e5dc7
commit 75f77766c1
2 changed files with 99 additions and 86 deletions

View File

@ -5,31 +5,36 @@
enum class message : char
namespace chat
{
HELLO = 1, NEPTUN = 2, PASSW = 3,
SERVER_DIRECTION = 4, MESSAGE = 5,
PING = 6, PONG = 7, BYE = 8,
LOGIN = 9, LOGOUT = 10, TERM = 0x7f
};
enum class message : char
{
HELLO = 1, NEPTUN = 2, PASSW = 3,
SERVER_DIRECTION = 4, MESSAGE = 5,
PING = 6, PONG = 7, BYE = 8,
LOGIN = 9, LOGOUT = 10, TERM = 0x7f
};
class chat_message
{
message _header;
std::string _content;
class chat_message
{
message _header;
std::string _content;
public:
chat_message(message header, std::string content = "")
:_header(header), _content(content) {}
public:
chat_message(message header, std::string content = "")
: _header(header), _content(content)
{}
chat_message(std::underlying_type<message>::type header_integral, std::string content)
:_header(static_cast<message>(header_integral)), _content(content) {}
chat_message(std::underlying_type<message>::type header_integral, std::string content)
: _header(static_cast<message>(header_integral)), _content(content)
{}
std::string get() const
{ return static_cast<char>(_header) + _content + static_cast<char>(message::TERM); }
};
std::string get() const
{ return static_cast<char>(_header) + _content + static_cast<char>(message::TERM); }
};
std::ostream& operator<<(std::ostream& os, const chat_message& msg)
{ return os << msg.get(); }
std::ostream& operator<<(std::ostream& os, const chat_message& msg)
{ return os << msg.get(); }
}

View File

@ -8,86 +8,94 @@
using namespace boost;
using boost::asio::ip::tcp;
class client_network_manager
namespace chat
{
private:
std::string _login;
asio::io_service& _ios;
tcp::socket _socket;
asio::streambuf _isb;
asio::streambuf _osb;
std::istream _is;
std::ostream _os;
using namespace chat;
using namespace boost;
using boost::asio::ip::tcp;
static void throw_if_error(boost::system::error_code& ec)
class client_network_manager
{
if (ec)
throw std::runtime_error("Networking error: " + ec.message());
}
private:
std::string _login;
std::string receive_message_sync()
{
std::string data;
asio::read_until(_socket, _isb, static_cast<char>(message::TERM));
std::getline(_is, data, static_cast<char>(message::TERM));
asio::io_service& _ios;
tcp::socket _socket;
asio::streambuf _isb;
asio::streambuf _osb;
std::istream _is;
std::ostream _os;
return data;
}
void handshake(boost::system::error_code ec, tcp::resolver::iterator)
{
throw_if_error(ec);
static void throw_if_error(boost::system::error_code& ec)
{
if (ec)
throw std::runtime_error("Networking error: " + ec.message());
}
std::cout << receive_message_sync() << '\n';
std::string receive_message_sync()
{
std::string data;
asio::read_until(_socket, _isb, static_cast<char>(message::TERM));
std::getline(_is, data, static_cast<char>(message::TERM));
_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);
return data;
}
std::cout << receive_message_sync() << '\n';
std::cout << receive_message_sync() << '\n';
std::cout << receive_message_sync() << '\n';
void handshake(boost::system::error_code ec, tcp::resolver::iterator)
{
throw_if_error(ec);
receive();
}
std::cout << receive_message_sync() << '\n';
void receive()
{
asio::async_read(_socket, _isb,
[this](boost::system::error_code ec, size_t)
{
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);
asio::write(_socket, _osb);
std::string data;
std::getline(_is, data, static_cast<char>(message::TERM));
std::cout << data << std::endl;
std::cout << receive_message_sync() << '\n';
std::cout << receive_message_sync() << '\n';
std::cout << receive_message_sync() << '\n';
receive();
});
}
receive();
}
void connect(boost::asio::ip::tcp::resolver::iterator epit)
{
asio::async_connect(_socket, epit,
std::bind(&client_network_manager::handshake, this, std::placeholders::_1,
std::placeholders::_2));
}
void receive()
{
asio::async_read(_socket, _isb,
[this](boost::system::error_code ec, size_t)
{
throw_if_error(ec);
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); }
std::string data;
std::getline(_is, data, static_cast<char>(message::TERM));
std::cout << data << std::endl;
void send(chat_message message)
{
_ios.post([this, message]
{ asio::async_write(_socket, asio::buffer(message.get()), [](boost::system::error_code, size_t){}); });
}
};
receive();
});
}
void connect(boost::asio::ip::tcp::resolver::iterator epit)
{
asio::async_connect(_socket, epit,
std::bind(&client_network_manager::handshake, this, std::placeholders::_1,
std::placeholders::_2));
}
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)
{});
});
}
};
}