changeset 152:16f27d6543bf

Client: do not catch handler result While here, also fix an infinite handler call because the output queue is not popped after execution. Also rename read to recv.
author David Demelier <markand@malikania.fr>
date Wed, 13 Dec 2017 13:45:00 +0100
parents 7a8ccd50bdf6
children 43a26de57fe7
files libclient/malikania/client/connection.cpp libclient/malikania/client/connection.hpp
diffstat 2 files changed, 15 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libclient/malikania/client/connection.cpp	Mon Dec 04 12:54:40 2017 +0100
+++ b/libclient/malikania/client/connection.cpp	Wed Dec 13 13:45:00 2017 +0100
@@ -54,11 +54,14 @@
     auto buffer = boost::asio::buffer(std::get<1>(output_[0]));
 
     boost::asio::async_write(*socket_, buffer, [this] (auto code, auto xfer) {
-        auto& handler = std::get<2>(output_[0]);
+        auto handler = std::get<2>(output_[0]);
+
+        output_.pop_front();
 
         if (handler)
             handler(std::move(code), std::get<0>(output_[0]));
 
+        // TODO: precise error code.
         if (!code && xfer != 0)
             flush();
     });
@@ -112,7 +115,7 @@
  * connection::recv
  * ------------------------------------------------------------------
  */
-void connection::read(recv_t handler)
+void connection::recv(recv_t handler)
 {
     assert(socket_);
 
@@ -126,8 +129,10 @@
 #if !defined(NDEBUG)
         is_reading = false;
 #endif
-        if (code || xfer == 0)
+        if (code)
             handler(std::move(code), nullptr);
+        else if (xfer == 0)
+            handler(make_error_code(boost::system::errc::network_down), nullptr);
         else {
             std::string command{
                 boost::asio::buffers_begin(input_.data()),
@@ -136,11 +141,15 @@
 
             input_.consume(xfer);
 
+            nlohmann::json msg;
+
             try {
-                handler(std::move(code), nlohmann::json::parse(command));
+                msg = nlohmann::json::parse(command);
             } catch (const std::exception&) {
                 // TODO: add custom error code.
             }
+
+            handler(std::move(code), std::move(msg));
         }
     });
 }
--- a/libclient/malikania/client/connection.hpp	Mon Dec 04 12:54:40 2017 +0100
+++ b/libclient/malikania/client/connection.hpp	Wed Dec 13 13:45:00 2017 +0100
@@ -107,13 +107,13 @@
     virtual void send(nlohmann::json message, send_t handler);
 
     /**
-     * Request for a read operation.
+     * Request for a recv operation.
      *
      * \pre no reading operation must be pending
      * \pre handler != nullptr
      * \param handler the handler
      */
-    virtual void read(recv_t handler);
+    virtual void recv(recv_t handler);
 };
 
 } // !client