Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <crow/app.h> |
| 3 | #include <crow/websocket.h> |
| 4 | #include <sys/socket.h> |
| 5 | |
| 6 | #include <boost/container/flat_map.hpp> |
| 7 | #include <boost/container/flat_set.hpp> |
| 8 | #include <webserver_common.hpp> |
| 9 | |
| 10 | namespace crow |
| 11 | { |
| 12 | namespace obmc_kvm |
| 13 | { |
| 14 | |
| 15 | static std::unique_ptr<boost::asio::ip::tcp::socket> hostSocket; |
| 16 | |
| 17 | // TODO(ed) validate that these buffer sizes are sane |
| 18 | static boost::beast::flat_static_buffer<1024U * 50U> outputBuffer; |
| 19 | static boost::beast::flat_static_buffer<1024U> inputBuffer; |
| 20 | |
| 21 | static crow::websocket::Connection* session = nullptr; |
| 22 | |
| 23 | static bool doingWrite = false; |
| 24 | |
Jae Hyun Yoo | c68604b | 2019-02-26 15:46:07 -0800 | [diff] [blame^] | 25 | inline void doWrite(); |
| 26 | |
| 27 | inline void WriteDone(const boost::system::error_code& ec, |
| 28 | std::size_t bytesWritten) |
| 29 | { |
| 30 | BMCWEB_LOG_DEBUG << "Wrote " << bytesWritten << "bytes"; |
| 31 | doingWrite = false; |
| 32 | inputBuffer.consume(bytesWritten); |
| 33 | |
| 34 | if (session == nullptr) |
| 35 | { |
| 36 | return; |
| 37 | } |
| 38 | if (ec == boost::asio::error::eof) |
| 39 | { |
| 40 | session->close("KVM socket port closed"); |
| 41 | return; |
| 42 | } |
| 43 | if (ec) |
| 44 | { |
| 45 | session->close("Error in reading to host port"); |
| 46 | BMCWEB_LOG_ERROR << "Error in KVM socket write " << ec; |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | doWrite(); |
| 51 | } |
| 52 | |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 53 | inline void doWrite() |
| 54 | { |
| 55 | if (doingWrite) |
| 56 | { |
| 57 | BMCWEB_LOG_DEBUG << "Already writing. Bailing out"; |
| 58 | return; |
| 59 | } |
| 60 | if (inputBuffer.size() == 0) |
| 61 | { |
| 62 | BMCWEB_LOG_DEBUG << "inputBuffer empty. Bailing out"; |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | doingWrite = true; |
Jae Hyun Yoo | c68604b | 2019-02-26 15:46:07 -0800 | [diff] [blame^] | 67 | hostSocket->async_write_some(inputBuffer.data(), WriteDone); |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | inline void doRead(); |
| 71 | |
| 72 | inline void readDone(const boost::system::error_code& ec, std::size_t bytesRead) |
| 73 | { |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 74 | BMCWEB_LOG_DEBUG << "read done. Read " << bytesRead << " bytes"; |
| 75 | if (ec) |
| 76 | { |
| 77 | BMCWEB_LOG_ERROR << "Couldn't read from KVM socket port: " << ec; |
| 78 | if (session != nullptr) |
| 79 | { |
| 80 | session->close("Error in connecting to KVM port"); |
| 81 | } |
| 82 | return; |
| 83 | } |
| 84 | if (session == nullptr) |
| 85 | { |
| 86 | return; |
| 87 | } |
| 88 | |
Jae Hyun Yoo | c68604b | 2019-02-26 15:46:07 -0800 | [diff] [blame^] | 89 | outputBuffer.commit(bytesRead); |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 90 | boost::beast::string_view payload( |
| 91 | static_cast<const char*>(outputBuffer.data().data()), bytesRead); |
| 92 | BMCWEB_LOG_DEBUG << "Sending payload size " << payload.size(); |
| 93 | session->sendBinary(payload); |
| 94 | outputBuffer.consume(bytesRead); |
| 95 | |
| 96 | doRead(); |
| 97 | } |
| 98 | |
| 99 | inline void doRead() |
| 100 | { |
| 101 | std::size_t bytes = outputBuffer.capacity() - outputBuffer.size(); |
| 102 | BMCWEB_LOG_DEBUG << "Reading " << bytes << " from kvm socket"; |
| 103 | hostSocket->async_read_some( |
| 104 | outputBuffer.prepare(outputBuffer.capacity() - outputBuffer.size()), |
| 105 | readDone); |
| 106 | } |
| 107 | |
| 108 | inline void connectHandler(const boost::system::error_code& ec) |
| 109 | { |
| 110 | if (ec) |
| 111 | { |
| 112 | BMCWEB_LOG_ERROR << "Couldn't connect to KVM socket port: " << ec; |
| 113 | if (session != nullptr) |
| 114 | { |
| 115 | session->close("Error in connecting to KVM port"); |
| 116 | } |
| 117 | return; |
| 118 | } |
| 119 | |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 120 | doRead(); |
| 121 | } |
| 122 | |
| 123 | inline void requestRoutes(CrowApp& app) |
| 124 | { |
| 125 | BMCWEB_ROUTE(app, "/kvm/0") |
| 126 | .websocket() |
| 127 | .onopen([](crow::websocket::Connection& conn) { |
| 128 | BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened"; |
| 129 | |
| 130 | if (session != nullptr) |
| 131 | { |
| 132 | conn.close("User already connected"); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | session = &conn; |
| 137 | if (hostSocket == nullptr) |
| 138 | { |
| 139 | boost::asio::ip::tcp::endpoint endpoint( |
| 140 | boost::asio::ip::address::from_string("127.0.0.1"), 5900); |
| 141 | |
| 142 | hostSocket = std::make_unique<boost::asio::ip::tcp::socket>( |
| 143 | conn.get_io_context()); |
| 144 | hostSocket->async_connect(endpoint, connectHandler); |
| 145 | } |
| 146 | }) |
| 147 | .onclose( |
| 148 | [](crow::websocket::Connection& conn, const std::string& reason) { |
| 149 | session = nullptr; |
| 150 | hostSocket = nullptr; |
| 151 | inputBuffer.reset(); |
| 152 | outputBuffer.reset(); |
| 153 | }) |
| 154 | .onmessage([](crow::websocket::Connection& conn, |
| 155 | const std::string& data, bool is_binary) { |
| 156 | if (data.length() > inputBuffer.capacity()) |
| 157 | { |
| 158 | BMCWEB_LOG_ERROR << "Buffer overrun when writing " |
| 159 | << data.length() << " bytes"; |
| 160 | conn.close("Buffer overrun"); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | BMCWEB_LOG_DEBUG << "Read " << data.size() |
| 165 | << " bytes from websocket"; |
| 166 | boost::asio::buffer_copy(inputBuffer.prepare(data.size()), |
| 167 | boost::asio::buffer(data)); |
| 168 | BMCWEB_LOG_DEBUG << "commiting " << data.size() |
| 169 | << " bytes from websocket"; |
| 170 | inputBuffer.commit(data.size()); |
| 171 | |
| 172 | BMCWEB_LOG_DEBUG << "inputbuffer size " << inputBuffer.size(); |
| 173 | doWrite(); |
| 174 | }); |
| 175 | } |
| 176 | } // namespace obmc_kvm |
| 177 | } // namespace crow |