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
{ {
enum class message : char
{
HELLO = 1, NEPTUN = 2, PASSW = 3, HELLO = 1, NEPTUN = 2, PASSW = 3,
SERVER_DIRECTION = 4, MESSAGE = 5, SERVER_DIRECTION = 4, MESSAGE = 5,
PING = 6, PONG = 7, BYE = 8, PING = 6, PONG = 7, BYE = 8,
LOGIN = 9, LOGOUT = 10, TERM = 0x7f 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,12 +8,16 @@
using namespace boost; namespace chat
using boost::asio::ip::tcp;
class client_network_manager
{ {
private: using namespace chat;
using namespace boost;
using boost::asio::ip::tcp;
class client_network_manager
{
private:
std::string _login; std::string _login;
asio::io_service& _ios; asio::io_service& _ios;
@ -80,14 +84,18 @@ private:
std::placeholders::_2)); std::placeholders::_2));
} }
public: public:
client_network_manager(asio::io_service& ioservice, tcp::resolver::iterator epit, std::string login) 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) : _ios(ioservice), _socket(_ios), _isb(), _osb(), _is(&_isb), _os(&_osb), _login(login)
{ connect(epit); } { connect(epit); }
void send(chat_message message) void send(chat_message message)
{ {
_ios.post([this, message] _ios.post([this, message]
{ asio::async_write(_socket, asio::buffer(message.get()), [](boost::system::error_code, size_t){}); }); {
asio::async_write(_socket, asio::buffer(message.get()), [](boost::system::error_code, size_t)
{});
});
} }
}; };
}