message_handler: Hold the session instance

The message_handler was using `getSession()` to get the session of the
handler.

The session instance is valid until the handler is completed, so in
reality, it is reasonable for the handler to own the session instance.

In the ipmi stress test, it's found an issue that the session is marked
as inactive and gets removed from sessions_manager before the handler is
completed, so the `getSession()` will throw in `~Handler()` and an error
log is printed:

 Async RMCP+ reply failed

The session at that time is actually valid.
Let the handler hold the session instance and do not call `getSession()`
every time it uses the session, so that it does not need to call
session_manager's `getSession()` in `~Handler()`.

There are cases that the Handler is created without calling
`updSessionData()`, inititalize the session in its constructor in such
cases.

Note that there will be following commits to address the `getSession()`
issue in others places.

Tested: With the following commits, there is no "Async RMCP+ reply
        failed" logs anymore.

Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Change-Id: Ief4cbf0237605f7a9b38d59acae42c86f046f792
diff --git a/message_handler.cpp b/message_handler.cpp
index 861d69f..f845228 100644
--- a/message_handler.cpp
+++ b/message_handler.cpp
@@ -43,7 +43,7 @@
 
 void Handler::updSessionData(std::shared_ptr<Message>& inMessage)
 {
-    auto session = session::Manager::get().getSession(inMessage->bmcSessionID);
+    session = session::Manager::get().getSession(inMessage->bmcSessionID);
 
     sessionID = inMessage->bmcSessionID;
     inMessage->rcSessionID = session->getRCSessionID();
@@ -113,7 +113,6 @@
     auto command = inMessage->getCommand();
     if (inMessage->payloadType == PayloadType::IPMI)
     {
-        auto session = session::Manager::get().getSession(sessionID);
         // Process PayloadType::IPMI only if ipmi is enabled or for sessionless
         // or for session establisbment command
         if (this->sessionID == session::sessionZero ||
@@ -167,8 +166,6 @@
 
 void Handler::send(std::shared_ptr<Message> outMessage)
 {
-    auto session = session::Manager::get().getSession(sessionID);
-
     // Flatten the packet
     auto packet = parser::flatten(outMessage, sessionHeader, session);
 
@@ -178,15 +175,11 @@
 
 void Handler::setChannelInSession() const
 {
-    auto session = session::Manager::get().getSession(sessionID);
-
     session->channelPtr = channel;
 }
 
 void Handler::sendSOLPayload(const std::vector<uint8_t>& input)
 {
-    auto session = session::Manager::get().getSession(sessionID);
-
     auto outMessage = std::make_shared<Message>();
     outMessage->payloadType = PayloadType::SOL;
     outMessage->payload = input;
@@ -201,8 +194,6 @@
 void Handler::sendUnsolicitedIPMIPayload(uint8_t netfn, uint8_t cmd,
                                          const std::vector<uint8_t>& output)
 {
-    auto session = session::Manager::get().getSession(sessionID);
-
     auto outMessage = std::make_shared<Message>();
     outMessage->payloadType = PayloadType::IPMI;
     outMessage->isPacketEncrypted = session->isCryptAlgoEnabled();
diff --git a/message_handler.hpp b/message_handler.hpp
index 88197ca..37e7240 100644
--- a/message_handler.hpp
+++ b/message_handler.hpp
@@ -3,6 +3,7 @@
 #include "message.hpp"
 #include "message_parsers.hpp"
 #include "session.hpp"
+#include "sessions_manager.hpp"
 #include "sol/console_buffer.hpp"
 
 #include <memory>
@@ -22,7 +23,12 @@
             uint32_t sessionID = message::Message::MESSAGE_INVALID_SESSION_ID) :
         sessionID(sessionID),
         channel(channel), io(io)
-    {}
+    {
+        if (sessionID != message::Message::MESSAGE_INVALID_SESSION_ID)
+        {
+            session = session::Manager::get().getSession(sessionID);
+        }
+    }
 
     /**
      * @brief Create a Handler intended for a send only (SOL)
@@ -31,7 +37,12 @@
             uint32_t sessionID = message::Message::MESSAGE_INVALID_SESSION_ID) :
         sessionID(sessionID),
         channel(channel), io(nullptr)
-    {}
+    {
+        if (sessionID != message::Message::MESSAGE_INVALID_SESSION_ID)
+        {
+            session = session::Manager::get().getSession(sessionID);
+        }
+    }
 
     ~Handler();
     Handler() = delete;
@@ -138,6 +149,9 @@
     parser::SessionHeader sessionHeader = parser::SessionHeader::IPMI20;
 
     std::shared_ptr<message::Message> inMessage;
+
+    /** @brief The IPMI session of the handler */
+    std::shared_ptr<session::Session> session;
 };
 
 } // namespace message