Mercurial > irccd
changeset 544:8d9662b2beee
Irccd: unify handlers, take error code as first argument
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 21 Nov 2017 15:12:05 +0100 |
parents | 7051034bf2ee |
children | 1ef194b6b168 |
files | libirccd/irccd/transport_client.cpp libirccd/irccd/transport_client.hpp libirccd/irccd/transport_server.cpp libirccd/irccd/transport_server.hpp libirccd/irccd/transport_service.cpp libirccdctl/irccd/ctl/network_connection.hpp |
diffstat | 6 files changed, 34 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/libirccd/irccd/transport_client.cpp Tue Nov 21 13:40:39 2017 +0100 +++ b/libirccd/irccd/transport_client.cpp Tue Nov 21 15:12:05 2017 +0100 @@ -59,7 +59,7 @@ do_recv(input_, [this, self, handler] (auto code, auto xfer) { if (code || xfer == 0) { - handler("", code); + handler(code, nullptr); close(); return; } @@ -72,16 +72,18 @@ // Remove early in case of errors. input_.consume(xfer); - try { - auto json = nlohmann::json::parse(message); + nlohmann::json command; - if (!json.is_object()) - handler(nullptr, network_errc::invalid_message); - else - handler(json, code); + try { + command = nlohmann::json::parse(message); } catch (...) { - handler(nullptr, network_errc::invalid_message); + handler(network_errc::invalid_message, nullptr); } + + if (!command.is_object()) + handler(network_errc::invalid_message, nullptr); + else + handler(network_errc::no_error, std::move(command)); }); }
--- a/libirccd/irccd/transport_client.hpp Tue Nov 21 13:40:39 2017 +0100 +++ b/libirccd/irccd/transport_client.hpp Tue Nov 21 15:12:05 2017 +0100 @@ -51,12 +51,12 @@ /** * Callback on receive operation. */ - using recv_t = std::function<void (const nlohmann::json&, const boost::system::error_code&)>; + using recv_t = std::function<void (boost::system::error_code, nlohmann::json)>; /** * Callback on send operation. */ - using send_t = std::function<void (const boost::system::error_code&)>; + using send_t = std::function<void (boost::system::error_code)>; /** * Client state. @@ -73,14 +73,14 @@ * * The implementation should read until \r\n\r\n is found. */ - using do_recv_handler_t = std::function<void (const boost::system::error_code&, std::size_t)>; + using do_recv_handler_t = std::function<void (boost::system::error_code, std::size_t)>; /** * Handler for do_send. * * The implementation must send the whole message. */ - using do_send_handler_t = std::function<void (const boost::system::error_code&, std::size_t)>; + using do_send_handler_t = std::function<void (boost::system::error_code, std::size_t)>; /** * Input buffer.
--- a/libirccd/irccd/transport_server.cpp Tue Nov 21 13:40:39 2017 +0100 +++ b/libirccd/irccd/transport_server.cpp Tue Nov 21 15:12:05 2017 +0100 @@ -30,9 +30,11 @@ assert(client); assert(handler); - client->recv([this, client, handler] (auto message, auto code) { - if (code) - handler(client, code); + client->recv([this, client, handler] (auto code, auto message) { + if (code) { + handler(std::move(code), std::move(client)); + return; + } clients_.insert(client); @@ -50,7 +52,7 @@ code = network_errc::no_error; } - handler(client, code); + handler(std::move(code), std::move(client)); }); } @@ -74,12 +76,12 @@ client->send(greetings, [this, client, handler] (auto code) { if (code) - handler(client, code); + handler(std::move(code), std::move(client)); else if (!password_.empty()) do_auth(std::move(client), std::move(handler)); else { clients_.insert(client); - handler(client, code); + handler(std::move(code), std::move(client)); } }); } @@ -88,9 +90,9 @@ { assert(handler); - do_accept([this, handler] (auto client, auto code) { + do_accept([this, handler] (auto code, auto client) { if (code) - handler(nullptr, code); + handler(std::move(code), nullptr); else do_greetings(std::move(client), std::move(handler)); });
--- a/libirccd/irccd/transport_server.hpp Tue Nov 21 13:40:39 2017 +0100 +++ b/libirccd/irccd/transport_server.hpp Tue Nov 21 15:12:05 2017 +0100 @@ -52,7 +52,7 @@ /** * Callback when a new client should be accepted. */ - using accept_t = std::function<void (std::shared_ptr<transport_client>, boost::system::error_code)>; + using accept_t = std::function<void (boost::system::error_code, std::shared_ptr<transport_client>)>; private: client_set_t clients_; @@ -192,9 +192,9 @@ acceptor_.async_accept(client->socket(), [client, handler] (auto code) { if (!code) - handler(std::move(client), std::move(code)); + handler(std::move(code), std::move(client)); else - handler(nullptr, code); + handler(std::move(code), nullptr); }); }
--- a/libirccd/irccd/transport_service.cpp Tue Nov 21 13:40:39 2017 +0100 +++ b/libirccd/irccd/transport_service.cpp Tue Nov 21 15:12:05 2017 +0100 @@ -53,15 +53,15 @@ void transport_service::do_accept(transport_server& ts) { - ts.accept([this, &ts] (auto client, auto code) { + ts.accept([this, &ts] (auto code, auto client) { if (code) log::warning() << "transport: " << code.message() << std::endl; else { - client->recv([this, client] (auto json, auto code) { + client->recv([this, client] (auto code, auto json) { if (code) log::warning() << "transport: " << code.message() << std::endl; else - handle_command(client, json); + handle_command(std::move(client), json); }); }
--- a/libirccdctl/irccd/ctl/network_connection.hpp Tue Nov 21 13:40:39 2017 +0100 +++ b/libirccdctl/irccd/ctl/network_connection.hpp Tue Nov 21 15:12:05 2017 +0100 @@ -109,7 +109,10 @@ handler(network_errc::invalid_message, nullptr); } - handler(network_errc::no_error, std::move(json)); + if (!json.is_object()) + handler(network_errc::invalid_message, nullptr); + else + handler(network_errc::no_error, std::move(json)); }); }