diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/kvm_websocket.hpp | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/include/kvm_websocket.hpp b/include/kvm_websocket.hpp index e3131bb3c2..d04c19fd93 100644 --- a/include/kvm_websocket.hpp +++ b/include/kvm_websocket.hpp @@ -14,7 +14,7 @@ namespace obmc_kvm static constexpr const uint maxSessions = 4; -class KvmSession +class KvmSession : public std::enable_shared_from_this<KvmSession> { public: explicit KvmSession(crow::websocket::Connection& connIn) : @@ -71,7 +71,13 @@ class KvmSession bytes); hostSocket.async_read_some( outputBuffer.prepare(outputBuffer.capacity() - outputBuffer.size()), - [this](const boost::system::error_code& ec, std::size_t bytesRead) { + [this, weak(weak_from_this())](const boost::system::error_code& ec, + std::size_t bytesRead) { + auto self = weak.lock(); + if (self == nullptr) + { + return; + } BMCWEB_LOG_DEBUG("conn:{}, read done. Read {} bytes", logPtr(&conn), bytesRead); if (ec) @@ -115,9 +121,15 @@ class KvmSession } doingWrite = true; - hostSocket.async_write_some(inputBuffer.data(), - [this](const boost::system::error_code& ec, + hostSocket.async_write_some( + inputBuffer.data(), + [this, weak(weak_from_this())](const boost::system::error_code& ec, std::size_t bytesWritten) { + auto self = weak.lock(); + if (self == nullptr) + { + return; + } BMCWEB_LOG_DEBUG("conn:{}, Wrote {}bytes", logPtr(&conn), bytesWritten); doingWrite = false; @@ -140,7 +152,7 @@ class KvmSession } doWrite(); - }); + }); } crow::websocket::Connection& conn; @@ -151,7 +163,7 @@ class KvmSession }; using SessionMap = boost::container::flat_map<crow::websocket::Connection*, - std::unique_ptr<KvmSession>>; + std::shared_ptr<KvmSession>>; // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) static SessionMap sessions; @@ -171,7 +183,7 @@ inline void requestRoutes(App& app) return; } - sessions[&conn] = std::make_unique<KvmSession>(conn); + sessions[&conn] = std::make_shared<KvmSession>(conn); }) .onclose([](crow::websocket::Connection& conn, const std::string&) { sessions.erase(&conn); |