@@ -11,6 +11,7 @@ class CHTTPServerModule : public IModule
1111public:
1212 virtual void LuaInit (bool bServerInit) OVERRIDE;
1313 virtual void LuaShutdown () OVERRIDE;
14+ virtual void Shutdown () OVERRIDE;
1415 virtual void Think (bool bSimulating) OVERRIDE;
1516 virtual const char * Name () { return " httpserver" ; };
1617 virtual int Compatibility () { return LINUX32 | LINUX64 | WINDOWS32 | WINDOWS64; };
4950 HTTPSERVER_OFFLINE
5051};
5152
53+ static std::unordered_set<HttpServer*> g_pHttpServers;
5254class HttpServer
5355{
5456public:
57+ HttpServer () {
58+ g_pHttpServers.insert (this );
59+ }
60+
5561 ~HttpServer ()
5662 {
5763 if (!ThreadInMainThread ())
@@ -67,6 +73,7 @@ class HttpServer
6773 Util::ReferenceFree (ref, " HttpServer::~HttpServer - Handler references" );
6874
6975 m_pHandlerReferences.clear ();
76+ g_pHttpServers.erase (this );
7077 }
7178
7279 void Start (const char * address, unsigned short port);
@@ -128,11 +135,11 @@ class HttpServer
128135
129136private:
130137 unsigned char m_iStatus = HTTPSERVER_OFFLINE;
131- unsigned short m_iPort;
138+ unsigned short m_iPort = 0 ;
132139 unsigned int m_iThreadSleep = 5 ; // How long the threads sleep / wait for a request to be handled
133140 bool m_bUpdate = false ;
134141 bool m_bInUpdate = false ;
135- std::string m_strAddress;
142+ std::string m_strAddress = " " ;
136143 std::vector<HttpRequest*> m_pRequests;
137144 std::vector<int > m_pHandlerReferences; // Contains the Lua references to the handler functions.
138145 httplib::Server m_pServer;
@@ -743,11 +750,11 @@ LUA_FUNCTION_STATIC(httpserver_Destroy)
743750
744751LUA_FUNCTION_STATIC (httpserver_GetAll)
745752{
746- LUA->PreCreateTable (g_pPushedHttpServer .size (), 0 );
753+ LUA->PreCreateTable (g_pHttpServers .size (), 0 );
747754 int idx = 0 ;
748- for (auto & [_, value] : g_pPushedHttpServer )
755+ for (auto & server : g_pHttpServers )
749756 {
750- value-> Push ( );
757+ Push_HttpServer (server );
751758 Util::RawSetI (LUA, -2 , ++idx);
752759 }
753760
@@ -758,11 +765,11 @@ LUA_FUNCTION_STATIC(httpserver_FindByName)
758765{
759766 std::string strName = LUA->CheckString (1 );
760767 bool bPushed = false ;
761- for (auto & [_, value] : g_pPushedHttpServer )
768+ for (auto & server : g_pHttpServers )
762769 {
763- if (((HttpServer*)value-> GetData ()) ->GetName () == strName)
770+ if (server ->GetName () == strName)
764771 {
765- value-> Push ( );
772+ Push_HttpServer (server );
766773 bPushed = true ;
767774 }
768775 }
@@ -862,12 +869,22 @@ void CHTTPServerModule::LuaShutdown()
862869{
863870 Util::NukeTable (" httpserver" );
864871
865- // Shouls we allow HttpServers to persist across map changes? Maybe later, but if anyone wants this request it .
872+ // HttpServers WILL persist across map changes.
866873 DeleteAll_HttpResponse ();
867874 DeleteAll_HttpRequest ();
868875 DeleteAll_HttpServer ();
869876}
870877
878+ void CHTTPServerModule::Shutdown ()
879+ {
880+ std::vector<HttpServer*> httpServers; // Copy of g_pHttpServers as when deleting we can't iterate over it.
881+ for (auto & server : g_pHttpServers)
882+ httpServers.push_back (server);
883+
884+ for (auto & server : httpServers)
885+ delete server;
886+ }
887+
871888void CHTTPServerModule::Think (bool simulating)
872889{
873890 VPROF_BUDGET (" HolyLib - CHTTPServerModule::Think" , VPROF_BUDGETGROUP_HOLYLIB);
0 commit comments