Skip to content

Commit 479f2ae

Browse files
committed
httpserver: properly implement persistance
1 parent b94ebb7 commit 479f2ae

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

source/lua.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ void Lua::RemoveLuaData(GarrysMod::Lua::ILuaInterface* LUA)
238238
Msg("holylib - Removed thread data %p\n", data);
239239
}
240240

241-
242241
static void LuaCheck(const CCommand& args)
243242
{
244243
if (!g_Lua)

source/lua.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace Lua
2727
// This will be required when we work with multiple ILuaInterface's
2828
struct StateData
2929
{
30-
int iReference = -1; // Reference to this structure to stop GC
30+
void* pOtherData[4]; // If any other plugin wants to use this, they can.
3131
};
3232

3333
extern Lua::StateData* GetLuaData(GarrysMod::Lua::ILuaInterface* LUA);

source/modules/httpserver.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class CHTTPServerModule : public IModule
1111
public:
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; };
@@ -49,9 +50,14 @@ enum
4950
HTTPSERVER_OFFLINE
5051
};
5152

53+
static std::unordered_set<HttpServer*> g_pHttpServers;
5254
class HttpServer
5355
{
5456
public:
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

129136
private:
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

744751
LUA_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+
871888
void CHTTPServerModule::Think(bool simulating)
872889
{
873890
VPROF_BUDGET("HolyLib - CHTTPServerModule::Think", VPROF_BUDGETGROUP_HOLYLIB);

0 commit comments

Comments
 (0)