Skip to content

Commit 1480f79

Browse files
committed
httpserver: properly implement persistance
1 parent b94ebb7 commit 1480f79

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
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: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <baseclient.h>
66
#include <inetchannel.h>
77
#include <netadr.h>
8+
#include "unordered_set"
89

910
class CHTTPServerModule : public IModule
1011
{
@@ -16,7 +17,7 @@ class CHTTPServerModule : public IModule
1617
virtual int Compatibility() { return LINUX32 | LINUX64 | WINDOWS32 | WINDOWS64; };
1718
};
1819

19-
CHTTPServerModule g_pHttpServerModule;
20+
static CHTTPServerModule g_pHttpServerModule;
2021
IModule* pHttpServerModule = &g_pHttpServerModule;
2122

2223
struct HttpResponse {
@@ -49,9 +50,15 @@ enum
4950
HTTPSERVER_OFFLINE
5051
};
5152

53+
class HttpServer;
54+
static std::unordered_set<HttpServer*> g_pHttpServers;
5255
class HttpServer
5356
{
5457
public:
58+
HttpServer() {
59+
g_pHttpServers.insert(this);
60+
}
61+
5562
~HttpServer()
5663
{
5764
if (!ThreadInMainThread())
@@ -67,6 +74,7 @@ class HttpServer
6774
Util::ReferenceFree(ref, "HttpServer::~HttpServer - Handler references");
6875

6976
m_pHandlerReferences.clear();
77+
g_pHttpServers.erase(this);
7078
}
7179

7280
void Start(const char* address, unsigned short port);
@@ -128,11 +136,11 @@ class HttpServer
128136

129137
private:
130138
unsigned char m_iStatus = HTTPSERVER_OFFLINE;
131-
unsigned short m_iPort;
139+
unsigned short m_iPort = 0;
132140
unsigned int m_iThreadSleep = 5; // How long the threads sleep / wait for a request to be handled
133141
bool m_bUpdate = false;
134142
bool m_bInUpdate = false;
135-
std::string m_strAddress;
143+
std::string m_strAddress = "";
136144
std::vector<HttpRequest*> m_pRequests;
137145
std::vector<int> m_pHandlerReferences; // Contains the Lua references to the handler functions.
138146
httplib::Server m_pServer;
@@ -743,11 +751,11 @@ LUA_FUNCTION_STATIC(httpserver_Destroy)
743751

744752
LUA_FUNCTION_STATIC(httpserver_GetAll)
745753
{
746-
LUA->PreCreateTable(g_pPushedHttpServer.size(), 0);
754+
LUA->PreCreateTable(g_pHttpServers.size(), 0);
747755
int idx = 0;
748-
for (auto& [_, value] : g_pPushedHttpServer)
756+
for (auto& server : g_pHttpServers)
749757
{
750-
value->Push();
758+
Push_HttpServer(server);
751759
Util::RawSetI(LUA, -2, ++idx);
752760
}
753761

@@ -758,11 +766,11 @@ LUA_FUNCTION_STATIC(httpserver_FindByName)
758766
{
759767
std::string strName = LUA->CheckString(1);
760768
bool bPushed = false;
761-
for (auto& [_, value] : g_pPushedHttpServer)
769+
for (auto& server : g_pHttpServers)
762770
{
763-
if (((HttpServer*)value->GetData())->GetName() == strName)
771+
if (server->GetName() == strName)
764772
{
765-
value->Push();
773+
Push_HttpServer(server);
766774
bPushed = true;
767775
}
768776
}
@@ -862,10 +870,17 @@ void CHTTPServerModule::LuaShutdown()
862870
{
863871
Util::NukeTable("httpserver");
864872

865-
// Shouls we allow HttpServers to persist across map changes? Maybe later, but if anyone wants this request it.
873+
// HttpServers WILL persist across map changes.
866874
DeleteAll_HttpResponse();
867875
DeleteAll_HttpRequest();
868876
DeleteAll_HttpServer();
877+
878+
std::vector<HttpServer*> httpServers; // Copy of g_pHttpServers as when deleting we can't iterate over it.
879+
for (auto server : g_pHttpServers)
880+
httpServers.push_back(server);
881+
882+
for (auto server : httpServers)
883+
delete server;
869884
}
870885

871886
void CHTTPServerModule::Think(bool simulating)

0 commit comments

Comments
 (0)