Mercurial > malikania
changeset 95:103c1e4ba2c2
Client: make distinct separation between error/disconnection
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 08 Jun 2017 09:01:22 +0200 |
parents | e354bca9bef9 |
children | 55300686bd78 |
files | libclient/malikania/client/client.cpp libclient/malikania/client/client.hpp libclient/malikania/client/connection.cpp |
diffstat | 3 files changed, 23 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/libclient/malikania/client/client.cpp Thu Jun 08 08:46:07 2017 +0200 +++ b/libclient/malikania/client/client.cpp Thu Jun 08 09:01:22 2017 +0200 @@ -34,9 +34,16 @@ client::~client() noexcept = default; +void client::handle_disconnect() +{ + // TODO: add error + std::cout << "client::handle_disconnect\n"; +} + void client::handle_error(boost::system::error_code) { // TODO: add error + std::cout << "client::handle_error\n"; } void client::handle_connect(boost::system::error_code)
--- a/libclient/malikania/client/client.hpp Thu Jun 08 08:46:07 2017 +0200 +++ b/libclient/malikania/client/client.hpp Thu Jun 08 09:01:22 2017 +0200 @@ -24,6 +24,8 @@ * \brief Main client game class. */ +#include <memory> + #include <boost/asio.hpp> #include <json.hpp> @@ -89,6 +91,12 @@ void connect(std::string host, std::uint16_t port); /** + * Handle disconnection, usually called when a read/send operation returned + * 0 bytes. + */ + virtual void handle_disconnect(); + + /** * Handle unexpected network error. * * \param code the error code
--- a/libclient/malikania/client/connection.cpp Thu Jun 08 08:46:07 2017 +0200 +++ b/libclient/malikania/client/connection.cpp Thu Jun 08 09:01:22 2017 +0200 @@ -69,10 +69,12 @@ { auto buffer = boost::asio::buffer(output_.front().data(), output_.front().length()); - boost::asio::async_write(socket_, buffer, [this, &client] (auto code, auto) { + boost::asio::async_write(socket_, buffer, [this, &client] (auto code, auto xfer) { output_.pop_front(); - if (code) { + if (xfer == 0) { + client.handle_disconnect(); + } else if (code) { client.handle_error(code); } else if (!output_.empty()) { this->flush(client); @@ -105,7 +107,9 @@ #if !defined(NDEBUG) is_reading = false; #endif - if (code || xfer == 0) { + if (xfer == 0) { + client.handle_disconnect(); + } else if (code) { client.handle_error(code); } else { std::string command{ @@ -118,7 +122,7 @@ try { client.handle_read(std::move(code), nlohmann::json::parse(command)); } catch (const std::exception& ex) { - // TODO: log error + std::cerr << "connection::do_read: " << ex.what() << std::endl; } } });