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

View File

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