From e4b32753a7ce7ca436bf751f390ee01f02b9efd5 Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Fri, 9 Feb 2024 18:56:29 -0800 Subject: Break out lambdas into real methods Tested: Need help Change-Id: I28cc1626212ec746b5345490ec285706eb386e65 Signed-off-by: Ed Tanous --- include/vm_websocket.hpp | 300 +++++++++++++++++++++++------------------------ 1 file changed, 144 insertions(+), 156 deletions(-) diff --git a/include/vm_websocket.hpp b/include/vm_websocket.hpp index a516edd6f4..d61bbcac12 100644 --- a/include/vm_websocket.hpp +++ b/include/vm_websocket.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -226,50 +227,60 @@ struct NbdProxyServer : std::enable_shared_from_this return endpointId; } - void run() + static void afterMount(const std::weak_ptr& weak, + const boost::system::error_code& ec, + bool /*isBinary*/) { - acceptor.async_accept( - [weak(weak_from_this())](const boost::system::error_code& ec, - stream_protocol::socket socket) { - if (ec) - { - BMCWEB_LOG_ERROR("UNIX socket: async_accept error = {}", - ec.message()); - return; - } + std::shared_ptr self = weak.lock(); + if (self == nullptr) + { + return; + } + if (ec) + { + BMCWEB_LOG_ERROR("DBus error: cannot call mount method = {}", + ec.message()); - BMCWEB_LOG_DEBUG("Connection opened"); - std::shared_ptr self = weak.lock(); - if (self == nullptr) - { - return; - } + self->connection.close("Failed to mount media"); + return; + } + } - self->connection.resumeRead(); - self->peerSocket = std::move(socket); - // Start reading from socket - self->doRead(); - }); + static void afterAccept(const std::weak_ptr& weak, + const boost::system::error_code& ec, + stream_protocol::socket socket) + { + if (ec) + { + BMCWEB_LOG_ERROR("UNIX socket: async_accept error = {}", + ec.message()); + return; + } - auto mountHandler = [weak(weak_from_this())]( - const boost::system::error_code& ec, bool) { - std::shared_ptr self = weak.lock(); - if (self == nullptr) - { - return; - } - if (ec) - { - BMCWEB_LOG_ERROR("DBus error: cannot call mount method = {}", - ec.message()); + BMCWEB_LOG_DEBUG("Connection opened"); + std::shared_ptr self = weak.lock(); + if (self == nullptr) + { + return; + } - self->connection.close("Failed to mount media"); - return; - } - }; + self->connection.resumeRead(); + self->peerSocket = std::move(socket); + // Start reading from socket + self->doRead(); + } + + void run() + { + acceptor.async_accept( + std::bind_front(&NbdProxyServer::afterAccept, weak_from_this())); crow::connections::systemBus->async_method_call( - std::move(mountHandler), "xyz.openbmc_project.VirtualMedia", path, + [weak{weak_from_this()}](const boost::system::error_code& ec, + bool isBinary) { + afterMount(weak, ec, isBinary); + }, + "xyz.openbmc_project.VirtualMedia", path, "xyz.openbmc_project.VirtualMedia.Proxy", "Mount"); } @@ -283,39 +294,75 @@ struct NbdProxyServer : std::enable_shared_from_this } private: + static void afterSendEx(const std::weak_ptr& weak) + { + std::shared_ptr self2 = weak.lock(); + if (self2 != nullptr) + { + self2->ux2wsBuf.consume(self2->ux2wsBuf.size()); + self2->doRead(); + } + } + + inline void afterRead(const std::weak_ptr& weak, + const boost::system::error_code& ec, size_t bytesRead) + { + if (ec) + { + BMCWEB_LOG_ERROR("UNIX socket: async_read_some error = {}", + ec.message()); + return; + } + std::shared_ptr self = weak.lock(); + if (self == nullptr) + { + return; + } + + // Send to websocket + self->ux2wsBuf.commit(bytesRead); + self->connection.sendEx( + crow::websocket::MessageType::Binary, + boost::beast::buffers_to_string(self->ux2wsBuf.data()), + std::bind_front(&NbdProxyServer::afterSendEx, weak_from_this())); + } + void doRead() { // Trigger async read - peerSocket.async_read_some( - ux2wsBuf.prepare(nbdBufferSize), - [weak(weak_from_this())](const boost::system::error_code& ec, - size_t bytesRead) { - if (ec) - { - BMCWEB_LOG_ERROR("UNIX socket: async_read_some error = {}", - ec.message()); - return; - } - std::shared_ptr self = weak.lock(); - if (self == nullptr) - { - return; - } + peerSocket.async_read_some(ux2wsBuf.prepare(nbdBufferSize), + std::bind_front(&NbdProxyServer::afterRead, + this, weak_from_this())); + } - // Send to websocket - self->ux2wsBuf.commit(bytesRead); - self->connection.sendEx( - crow::websocket::MessageType::Binary, - boost::beast::buffers_to_string(self->ux2wsBuf.data()), - [weak(self->weak_from_this())]() { - std::shared_ptr self2 = weak.lock(); - if (self2 != nullptr) - { - self2->ux2wsBuf.consume(self2->ux2wsBuf.size()); - self2->doRead(); - } - }); - }); + static void afterWrite(const std::weak_ptr& weak, + std::function&& onDone, + const boost::system::error_code& ec, + size_t bytesWritten) + { + std::shared_ptr self = weak.lock(); + if (self == nullptr) + { + return; + } + + self->ws2uxBuf.consume(bytesWritten); + self->uxWriteInProgress = false; + + if (ec) + { + BMCWEB_LOG_ERROR("UNIX: async_write error = {}", ec.message()); + self->connection.close("Internal error"); + return; + } + + // Retrigger doWrite if there is something in buffer + if (self->ws2uxBuf.size() > 0) + { + self->doWrite(std::move(onDone)); + return; + } + onDone(); } void doWrite(std::function&& onDone) @@ -333,35 +380,10 @@ struct NbdProxyServer : std::enable_shared_from_this } uxWriteInProgress = true; - peerSocket.async_write_some( - ws2uxBuf.data(), - [weak(weak_from_this()), - onDone(std::move(onDone))](const boost::system::error_code& ec, - size_t bytesWritten) mutable { - std::shared_ptr self = weak.lock(); - if (self == nullptr) - { - return; - } - - self->ws2uxBuf.consume(bytesWritten); - self->uxWriteInProgress = false; - - if (ec) - { - BMCWEB_LOG_ERROR("UNIX: async_write error = {}", ec.message()); - self->connection.close("Internal error"); - return; - } - - // Retrigger doWrite if there is something in buffer - if (self->ws2uxBuf.size() > 0) - { - self->doWrite(std::move(onDone)); - return; - } - onDone(); - }); + peerSocket.async_write_some(ws2uxBuf.data(), + std::bind_front(&NbdProxyServer::afterWrite, + weak_from_this(), + std::move(onDone))); } // Keeps UNIX socket endpoint file path @@ -392,64 +414,26 @@ using SessionMap = boost::container::flat_map(&value); - - if (endpointValue == nullptr) - { - BMCWEB_LOG_ERROR("EndpointId property value is null"); - } - } - if (name == "Socket") - { - socketValue = std::get_if(&value); - if (socketValue == nullptr) - { - BMCWEB_LOG_ERROR("Socket property value is null"); - } - } - } - } - - if ((endpointValue != nullptr) && (socketValue != nullptr) && - *endpointValue == conn.url().path()) - { - endpointObjectPath = &objectPath.str; - break; - } - } - - if (objects.empty() || endpointObjectPath == nullptr) + if (!success) { - BMCWEB_LOG_ERROR("Cannot find requested EndpointId"); - conn.close("Failed to match EndpointId"); + conn.close("Internal Error"); return; } @@ -465,24 +449,28 @@ inline void // If the socket file exists (i.e. after bmcweb crash), // we cannot reuse it. - std::remove((*socketValue).c_str()); - - sessions[&conn] = std::make_shared( - conn, *socketValue, *endpointValue, *endpointObjectPath); + std::remove(socket.c_str()); + sessions[&conn] = std::make_shared(conn, socket, endpointId, + path); sessions[&conn]->run(); -}; +} inline void onOpen(crow::websocket::Connection& conn) { BMCWEB_LOG_DEBUG("nbd-proxy.onopen({})", logPtr(&conn)); - sdbusplus::message::object_path path("/xyz/openbmc_project/VirtualMedia"); - dbus::utility::getManagedObjects( - "xyz.openbmc_project.VirtualMedia", path, - [&conn](const boost::system::error_code& ec, - const dbus::utility::ManagedObjectType& objects) { - afterGetManagedObjects(conn, ec, objects); + sdbusplus::message::object_path path( + "/xyz/openbmc_project/VirtualMedia/nbd"); + + path /= std::to_string(0); + + sdbusplus::asio::getAllProperties( + *crow::connections::systemBus, "xyz.openbmc_project.VirtualMedia", path, + "xyz.openbmc_project.VirtualMedia", + [&conn, path](const boost::system::error_code& ec, + const dbus::utility::DBusPropertiesMap& propertiesList) { + afterGetSocket(conn, path, ec, propertiesList); }); // We need to wait for dbus and the websockets to hook up before data is -- cgit v1.2.3