implemented cnm::close_connection() + general code cleaning + added TODOS

This commit is contained in:
Kjistóf 2016-12-03 15:30:15 +01:00
parent 6f34d28458
commit 5cfef17cc2

View File

@ -47,6 +47,18 @@ namespace chat
}); });
} }
void close_connection()
{
_ios.post([this]
{
_os << chat_message(message::BYE);
asio::write(_socket, _osb);
// TODO: handle last server message
_socket.close();
_ios.stop();
});
}
/* internal methods */ /* internal methods */
private: private:
@ -71,53 +83,57 @@ namespace chat
{ {
throw_if_error(ec); throw_if_error(ec);
std::cout << receive_message_sync() << '\n';
_os << chat_message(message::HELLO); _os << chat_message(message::HELLO);
_os << chat_message(message::NEPTUN, _login); _os << chat_message(message::NEPTUN, _login);
std::reverse(_login.begin(), _login.end()); std::reverse(_login.begin(), _login.end());
_os << chat_message(message::PASSW, _login); _os << chat_message(message::PASSW, _login);
asio::write(_socket, _osb); asio::write(_socket, _osb); // handshake is handled synchronously
// TODO: do something with these couts (decide whether to display this or not)
std::cout << receive_message_sync() << '\n';
std::cout << receive_message_sync() << '\n'; std::cout << receive_message_sync() << '\n';
std::cout << receive_message_sync() << '\n'; std::cout << receive_message_sync() << '\n';
std::cout << receive_message_sync() << '\n'; std::cout << receive_message_sync() << '\n';
receive(); receive(); // then flow goes to receive-loop
} }
std::string receive_message_sync() std::string receive_message_sync()
{ {
boost::system::error_code ec;
std::string data; std::string data;
asio::read_until(_socket, _isb, byte(message::TERM)); asio::read_until(_socket, _isb, byte(message::TERM), ec);
std::getline(_is, data, byte(message::TERM)); std::getline(_is, data, byte(message::TERM));
throw_if_error(ec);
return data; return data;
} }
void receive() void receive()
{ {
asio::async_read_until(_socket, _isb, byte(message::TERM), asio::async_read_until
[this](boost::system::error_code ec, size_t) (_socket, _isb, byte(message::TERM),
{ [this](boost::system::error_code ec, size_t)
throw_if_error(ec); {
throw_if_error(ec);
std::string data; std::string data;
std::getline(_is, data, byte(message::TERM)); std::getline(_is, data, byte(message::TERM));
switch (data[0]) switch (data[0])
{ {
// TODO: handle stuff // TODO: handle stuff
case byte(message::LOGIN): case byte(message::PING):
std::cout << "login\n"; send(chat_message(message::PONG));
break; break;
case byte(message::PING): case byte(message::MESSAGE):
std::cout << "ping\n"; std::cout << data << '\n';
break; break;
} }
receive(); receive();
}); });
} }
}; };
} }