Skip to content

Commit

Permalink
Process requests using a zero-delay timer
Browse files Browse the repository at this point in the history
  • Loading branch information
timschumi committed Jun 22, 2022
1 parent e6192b0 commit 163c3f0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ The value is passed to libcurl as-is and is not checked for validity by CHTTP,
so make sure to review the documentation for
[libcurl's CURLOPT_INTERFACE option](https://curl.se/libcurl/c/CURLOPT_INTERFACE.html).

### Zero-delay timers

CHTTP has switched to using a zero-delay timer instead of a hook to allow for running
requests while in singleplayer or while the server is hibernating.

In case there are any issues (bug reports greatly appreciated), you can return to
using the old hooking method by setting the environment variable `CHTTP_FORCE_HOOK`.

## Addon development

This is only required for developers who want to use CHTTP in their addons.
Expand Down
8 changes: 7 additions & 1 deletion src/chttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ GMOD_MODULE_OPEN() {
// Pop the global table from the stack again
LUA->Pop();

registerHook(LUA, "Think", "__chttpThinkHook", threadingDoThink);
if (getenv("CHTTP_FORCE_HOOK")) {
Logger::msg("Processing requests using a hook...");
registerHook(LUA, "Think", "__chttpThinkHook", threadingDoThink);
} else {
Logger::msg("Processing requests using a zero-delay timer...");
registerZeroDelayTimer(LUA, "__chttpThinkTimer", threadingDoThink);
}

// Start the background thread
RequestWorker::the();
Expand Down
13 changes: 13 additions & 0 deletions src/lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ void luaTableToMap(GarrysMod::Lua::ILuaBase *LUA, int index, std::map<std::strin
}
}

void registerZeroDelayTimer(GarrysMod::Lua::ILuaBase *LUA, char const* identifier, GarrysMod::Lua::CFunc function) {
LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
LUA->GetField(-1, "timer");
LUA->GetField(-1, "Create");
LUA->PushString(identifier);
LUA->PushNumber(0);
LUA->PushNumber(0);
LUA->PushCFunction(function);
LUA->Call(4, 0);
LUA->Pop();
LUA->Pop();
}

void registerHook(GarrysMod::Lua::ILuaBase *LUA, char const* event, char const* identifier, GarrysMod::Lua::CFunc function) {
// We are working on the global table today
LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
Expand Down
2 changes: 2 additions & 0 deletions src/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ void mapToLuaTable(GarrysMod::Lua::ILuaBase *LUA, const std::map<std::string, st

void luaTableToMap(GarrysMod::Lua::ILuaBase *LUA, int index, std::map<std::string, std::string> &map);

void registerZeroDelayTimer(GarrysMod::Lua::ILuaBase *LUA, char const* identifier, GarrysMod::Lua::CFunc function);

void registerHook(GarrysMod::Lua::ILuaBase *LUA, char const* event, char const* identifier, GarrysMod::Lua::CFunc function);

0 comments on commit 163c3f0

Please sign in to comment.