Refine KVM websock proxy

This commit simplifies input buffer delivering logic by removing
a doWrite() call from readDone(). Input events can be delivered
through websocket's onmessage handler only and it's enough.

Also, it fixes a suspicious weak point of commit/consume pair on
outputBuffer.

Change-Id: I5b777993a9d6f05375da0422b168c25dacb1b9f9
Signed-off-by: Jae Hyun Yoo <jae.hyun.yoo@linux.intel.com>
diff --git a/include/kvm_websocket.hpp b/include/kvm_websocket.hpp
index aa2eaec..8034ba7 100644
--- a/include/kvm_websocket.hpp
+++ b/include/kvm_websocket.hpp
@@ -22,6 +22,34 @@
 
 static bool doingWrite = false;
 
+inline void doWrite();
+
+inline void WriteDone(const boost::system::error_code& ec,
+                      std::size_t bytesWritten)
+{
+    BMCWEB_LOG_DEBUG << "Wrote " << bytesWritten << "bytes";
+    doingWrite = false;
+    inputBuffer.consume(bytesWritten);
+
+    if (session == nullptr)
+    {
+        return;
+    }
+    if (ec == boost::asio::error::eof)
+    {
+        session->close("KVM socket port closed");
+        return;
+    }
+    if (ec)
+    {
+        session->close("Error in reading to host port");
+        BMCWEB_LOG_ERROR << "Error in KVM socket write " << ec;
+        return;
+    }
+
+    doWrite();
+}
+
 inline void doWrite()
 {
     if (doingWrite)
@@ -36,37 +64,13 @@
     }
 
     doingWrite = true;
-    hostSocket->async_write_some(
-        inputBuffer.data(),
-        [](boost::beast::error_code ec, std::size_t bytes_written) {
-            BMCWEB_LOG_DEBUG << "Wrote " << bytes_written << "bytes";
-            doingWrite = false;
-            inputBuffer.consume(bytes_written);
-
-            if (session == nullptr)
-            {
-                return;
-            }
-            if (ec == boost::asio::error::eof)
-            {
-                session->close("KVM socket port closed");
-                return;
-            }
-            if (ec)
-            {
-                session->close("Error in reading to host port");
-                BMCWEB_LOG_ERROR << "Error in KVM socket write " << ec;
-                return;
-            }
-            doWrite();
-        });
+    hostSocket->async_write_some(inputBuffer.data(), WriteDone);
 }
 
 inline void doRead();
 
 inline void readDone(const boost::system::error_code& ec, std::size_t bytesRead)
 {
-    outputBuffer.commit(bytesRead);
     BMCWEB_LOG_DEBUG << "read done.  Read " << bytesRead << " bytes";
     if (ec)
     {
@@ -82,6 +86,7 @@
         return;
     }
 
+    outputBuffer.commit(bytesRead);
     boost::beast::string_view payload(
         static_cast<const char*>(outputBuffer.data().data()), bytesRead);
     BMCWEB_LOG_DEBUG << "Sending payload size " << payload.size();
@@ -112,7 +117,6 @@
         return;
     }
 
-    doWrite();
     doRead();
 }