Index: source/rlinterface/RLInterface.h =================================================================== --- source/rlinterface/RLInterface.h +++ source/rlinterface/RLInterface.h @@ -149,7 +149,7 @@ std::string GetGameState() const; private: - GameMessage m_GameMessage; + GameMessage m_GameMessage{GameMessageType::None}; ScenarioConfig m_ScenarioConfig; std::string m_ReturnValue; bool m_NeedsGameState = false; @@ -158,6 +158,13 @@ std::mutex m_MsgLock; std::condition_variable m_MsgApplied; std::string m_Code; + + struct ContextDeleter + { + void operator()(mg_context* context) noexcept; + }; + + const std::unique_ptr m_Context; }; } Index: source/rlinterface/RLInterface.cpp =================================================================== --- source/rlinterface/RLInterface.cpp +++ source/rlinterface/RLInterface.cpp @@ -40,17 +40,24 @@ namespace RL { -Interface::Interface(const char* server_address) : m_GameMessage({GameMessageType::None}) +Interface::Interface(const char* server_address) : + m_Context{[&] + { + LOGMESSAGERENDER("Starting RL interface HTTP server"); + const char *options[] = { + "listening_ports", server_address, + "num_threads", "1", + nullptr + }; + return mg_start(MgCallback, this, options); + }()} +{ + ENSURE(m_Context); +} + +void Interface::ContextDeleter::operator()(mg_context* context) noexcept { - LOGMESSAGERENDER("Starting RL interface HTTP server"); - - const char *options[] = { - "listening_ports", server_address, - "num_threads", "1", - nullptr - }; - mg_context* mgContext = mg_start(MgCallback, this, options); - ENSURE(mgContext); + mg_stop(context); } // Interactions with the game engine (g_Game) must be done in the main