diff options
author | Jae Hyun Yoo <jae.hyun.yoo@linux.intel.com> | 2019-02-27 02:46:07 +0300 |
---|---|---|
committer | Ed Tanous <ed.tanous@intel.com> | 2019-03-18 19:58:08 +0300 |
commit | c68604bd993d00ad11ee394514295ff5a2ef6f12 (patch) | |
tree | 30e9b81e97b6f061a3999314cfc767728773236a /include/kvm_websocket.hpp | |
parent | 2a133282e3a841e317d1fd0215ccb4dbe6e5cb0e (diff) | |
download | bmcweb-c68604bd993d00ad11ee394514295ff5a2ef6f12.tar.xz |
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>
Diffstat (limited to 'include/kvm_websocket.hpp')
-rw-r--r-- | include/kvm_websocket.hpp | 56 |
1 files changed, 30 insertions, 26 deletions
diff --git a/include/kvm_websocket.hpp b/include/kvm_websocket.hpp index aa2eaecc32..8034ba77f4 100644 --- a/include/kvm_websocket.hpp +++ b/include/kvm_websocket.hpp @@ -22,6 +22,34 @@ static crow::websocket::Connection* session = nullptr; 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 @@ inline void doWrite() } 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 @@ inline void readDone(const boost::system::error_code& ec, std::size_t bytesRead) 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 @@ inline void connectHandler(const boost::system::error_code& ec) return; } - doWrite(); doRead(); } |