From 75f77766c1d2800d6a32551428c499526d4afeb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sat, 3 Dec 2016 13:13:06 +0100 Subject: [PATCH] moved everything to namespace chat to avoid namespace pollution --- chat_messages.hpp | 45 +++++++------- chat_networking.hpp | 140 +++++++++++++++++++++++--------------------- 2 files changed, 99 insertions(+), 86 deletions(-) diff --git a/chat_messages.hpp b/chat_messages.hpp index f014cf2..12f98d3 100644 --- a/chat_messages.hpp +++ b/chat_messages.hpp @@ -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::type header_integral, std::string content) - :_header(static_cast(header_integral)), _content(content) {} + chat_message(std::underlying_type::type header_integral, std::string content) + : _header(static_cast(header_integral)), _content(content) + {} - std::string get() const - { return static_cast(_header) + _content + static_cast(message::TERM); } -}; + std::string get() const + { return static_cast(_header) + _content + static_cast(message::TERM); } + }; -std::ostream& operator<<(std::ostream& os, const chat_message& msg) -{ return os << msg.get(); } \ No newline at end of file + std::ostream& operator<<(std::ostream& os, const chat_message& msg) + { return os << msg.get(); } +} \ No newline at end of file diff --git a/chat_networking.hpp b/chat_networking.hpp index aaad119..3ccaf43 100644 --- a/chat_networking.hpp +++ b/chat_networking.hpp @@ -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(message::TERM)); - std::getline(_is, data, static_cast(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(message::TERM)); + std::getline(_is, data, static_cast(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(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(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){}); }); - } -}; \ No newline at end of file + 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) + {}); + }); + } + }; +} \ No newline at end of file