made client_network_manager mostly working. it can handle a handshake

This commit is contained in:
Kjistóf 2016-12-03 13:02:37 +01:00
parent f8c57c8e24
commit 07284e5dc7

View File

@ -1,40 +1,93 @@
#include <iostream> #include <iostream>
#include <string>
#include <functional> #include <functional>
#include <thread>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <algorithm>
#include "chat_messages.hpp" #include "chat_messages.hpp"
class client_network_manager
{
using namespace boost; using namespace boost;
using boost::asio::ip::tcp; using boost::asio::ip::tcp;
class client_network_manager
{
private: private:
asio::io_service _ios; std::string _login;
tcp::socket _socket;
asio::streambuf _sb;
std::istream _is{&sb};
std::ostream _os{&sb};
void handshake(boost::system::error_code ec, size_t) 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)
{ {
if (ec) if (ec)
throw std::runtime_error("Networking error: " + ec.message()); throw std::runtime_error("Networking error: " + ec.message());
}
// TODO: do handshake & read-loop 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));
return data;
}
void handshake(boost::system::error_code ec, tcp::resolver::iterator)
{
throw_if_error(ec);
std::cout << receive_message_sync() << '\n';
_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::cout << receive_message_sync() << '\n';
std::cout << receive_message_sync() << '\n';
std::cout << receive_message_sync() << '\n';
receive();
}
void receive()
{
asio::async_read(_socket, _isb,
[this](boost::system::error_code ec, size_t)
{
throw_if_error(ec);
std::string data;
std::getline(_is, data, static_cast<char>(message::TERM));
std::cout << data << std::endl;
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: public:
client_network_manager(tcp::resolver::query query) 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)
asio::async_connect(_socket,tcp::resolver().resolve(query), { connect(epit); }
std::bind(&client_network_manager::handshake, this));
}
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()), []{}); }); { asio::async_write(_socket, asio::buffer(message.get()), [](boost::system::error_code, size_t){}); });
} }
}; };