Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
add async http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
huoji120 committed Oct 13, 2023
1 parent 5c7a315 commit 7c84e1e
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 26 deletions.
14 changes: 13 additions & 1 deletion csgo2/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ CURLcode HttpGet(const std::string& strUrl, std::string& strResponse,
return CURLE_FAILED_INIT;
}
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, (char*)header.c_str());

if (header.empty() == false) {
headers = curl_slist_append(headers, (char*)header.c_str());
}
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers,
"Content-Type: application/json"); // text/html
headers = curl_slist_append(headers, "charsets: utf-8");
curl_easy_setopt(pCURL, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str());
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
Expand All @@ -24,7 +31,9 @@ CURLcode HttpGet(const std::string& strUrl, std::string& strResponse,
curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, receive_data);
curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse);
res = curl_easy_perform(pCURL);
curl_slist_free_all(headers);
curl_easy_cleanup(pCURL);

return res;
}

Expand All @@ -36,6 +45,9 @@ CURLcode HttpPost(const std::string& strUrl, std::string header,
if (pCURL == NULL) {
return CURLE_FAILED_INIT;
}
if (header.empty() == false) {
headers = curl_slist_append(headers, (char*)header.c_str());
}
CURLcode ret;
ret = curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str());
// std::string data = curl_easy_escape(pCURL, szJson.c_str(),
Expand Down
88 changes: 86 additions & 2 deletions csgo2/script_apis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ auto luaApi_HttpGet(lua_State* luaVm) -> int {
const auto timeOut = lua_tointeger(luaVm, 3);
auto strHeader = std::string(header);
std::string response;
auto result = Server::HttpGet(url, strHeader, response, timeOut);
auto result = Server::HttpGet(url, response, strHeader, timeOut);
lua_pushinteger(luaVm, result);
lua_pushstring(luaVm, response.c_str());
return 2;
Expand All @@ -790,6 +790,87 @@ auto luaApi_HttpPost(lua_State* luaVm) -> int {
lua_pushstring(luaVm, response.c_str());
return 2;
}
auto luaApi_HttpAsyncPost(lua_State* luaVm) -> int {
// param: url:string header:string postdata:string timeOut:int
// metadata:string
const auto url = lua_tostring(luaVm, 1);
const auto header = lua_tostring(luaVm, 2);
const auto postdata = lua_tostring(luaVm, 3);
const auto theTimeOut = lua_tointeger(luaVm, 4);
const auto metadata = lua_tostring(luaVm, 5);
struct _ctx {
std::shared_ptr<std::string> url;
std::shared_ptr<std::string> header;
std::shared_ptr<std::string> postdata;
std::shared_ptr<std::string> metadata;
long timeOut;
};
_ctx* ctx = new _ctx{
.url = std::make_shared<std::string>(url),
.header = std::make_shared<std::string>(header),
.postdata = std::make_shared<std::string>(postdata),
.metadata = std::make_shared<std::string>(metadata),
.timeOut = (int)theTimeOut,
};
if (ctx) {
CreateThread(
nullptr, 0,
[](void* ctx) -> DWORD {
const auto theCtx = reinterpret_cast<_ctx*>(ctx);
auto strHeader = std::string(*theCtx->header.get());
std::string response;
auto result = Server::HttpPost(*theCtx->url.get(), strHeader,
*theCtx->postdata.get(),
response, theCtx->timeOut);
ScriptCallBacks::luaCall_onHttpRequest(
*theCtx->url.get(), *theCtx->metadata.get(),
response.c_str(), result);
delete theCtx;
return 0;
},
ctx, 0, nullptr);
}
lua_pop(luaVm, 5);
return 0;
}
auto luaApi_HttpAsyncGet(lua_State* luaVm) -> int {
// param: url:string header:string timeOut:int
// metadata:string
const auto url = lua_tostring(luaVm, 1);
const auto header = lua_tostring(luaVm, 2);
const auto theTimeOut = lua_tointeger(luaVm, 3);
const auto metadata = lua_tostring(luaVm, 4);
struct _ctx {
std::shared_ptr<std::string> url;
std::shared_ptr<std::string> header;
std::shared_ptr<std::string> metadata;
long timeOut;
};
_ctx* ctx = new _ctx{
.url = std::make_shared<std::string>(url),
.header = std::make_shared<std::string>(header),
.metadata = std::make_shared<std::string>(metadata),
.timeOut = (long)theTimeOut,
};
if (ctx) {
CreateThread(
nullptr, 0,
[](void* ctx) -> DWORD {
const auto theCtx = reinterpret_cast<_ctx*>(ctx);
std::string response;
auto httpCode =
Server::HttpGet(*theCtx->url.get(), response, *theCtx->header.get(),theCtx->timeOut);
ScriptCallBacks::luaCall_onHttpRequest(
*theCtx->url.get(), *theCtx->metadata.get(),
response.c_str(), httpCode);
delete theCtx;
return 0;
},
const_cast<_ctx*>(ctx), 0, nullptr);
}
lua_pop(luaVm, 4);
return 0;
}

auto initFunciton(lua_State* luaVm) -> void {
lua_register(luaVm, "ListenToGameEvent", luaApi_ListenToGameEvent);
Expand Down Expand Up @@ -838,8 +919,11 @@ auto initFunciton(lua_State* luaVm) -> void {
lua_register(luaVm, "luaApi_KickPlayer", luaApi_KickPlayer);
lua_register(luaVm, "luaApi_HttpGet", luaApi_HttpGet);
lua_register(luaVm, "luaApi_HttpPost", luaApi_HttpPost);
lua_register(luaVm, "luaApi_HttpAsyncGet", luaApi_HttpAsyncGet);
lua_register(luaVm, "luaApi_HttpAsyncPost", luaApi_HttpAsyncPost);

lua_register(luaVm, "luaApi_GetPlayerSteamId", luaApi_GetPlayerSteamId);
//lua_register(luaVm, "luaApi_TeleportPlayer", luaApi_TeleportPlayer);
// lua_register(luaVm, "luaApi_TeleportPlayer", luaApi_TeleportPlayer);

luabridge::getGlobalNamespace(luaVm)
.beginClass<_luaApi_WeaponInfo>("WeaponInfo")
Expand Down
65 changes: 44 additions & 21 deletions csgo2/script_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ std::unordered_map<uint32_t, _CallbackNames> callbackNameWithEnumMap{
{hash_32_fnv1a_const("round_end"), _CallbackNames::kOnRoundEnd},
{hash_32_fnv1a_const("player_hurt"), _CallbackNames::kOnPlayerHurt},
{hash_32_fnv1a_const("player_team"), _CallbackNames::kOnPlayerTeamChange},
{hash_32_fnv1a_const("http_request"), _CallbackNames::kOnHttpRequest},

};
auto CallBackNameToEnum(const char* name) -> _CallbackNames {
if (name == nullptr) {
Expand Down Expand Up @@ -206,30 +208,51 @@ auto luaCall_onPlayerHurt(int userid, int attacker, int health, int armor,
}
});
}
auto luaCall_onPlayerTeamChange(int userid, int team, int oldteam, bool disconnect, bool slient, bool isBot) -> bool {
auto luaCall_onPlayerTeamChange(int userid, int team, int oldteam,
bool disconnect, bool slient, bool isBot)
-> bool {
bool result = false;
ExcuteCallbackInAllLuaVm(_CallbackNames::kOnPlayerTeamChange,
[&](lua_State* luaVm, int refIndex) -> void {
lua_rawgeti(luaVm, LUA_REGISTRYINDEX,
refIndex);
if (lua_isfunction(luaVm, -1)) {
lua_pushinteger(luaVm, userid);
lua_pushinteger(luaVm, team);
lua_pushinteger(luaVm, oldteam);
lua_pushboolean(luaVm, disconnect);
lua_pushboolean(luaVm, slient);
lua_pushboolean(luaVm, isBot);
[&](lua_State* luaVm, int refIndex) -> void {
lua_rawgeti(luaVm, LUA_REGISTRYINDEX,
refIndex);
if (lua_isfunction(luaVm, -1)) {
lua_pushinteger(luaVm, userid);
lua_pushinteger(luaVm, team);
lua_pushinteger(luaVm, oldteam);
lua_pushboolean(luaVm, disconnect);
lua_pushboolean(luaVm, slient);
lua_pushboolean(luaVm, isBot);

if (lua_pcall(luaVm, 6, 1, 0) != LUA_OK) {
LOG("Error calling Lua callback: %s\n",
lua_tostring(luaVm, -1));
lua_pop(luaVm, 1);
}
if (lua_isboolean(luaVm, -1)) {
result = lua_toboolean(luaVm, -1);
}
}
});
if (lua_pcall(luaVm, 6, 1, 0) != LUA_OK) {
LOG("Error calling Lua callback: %s\n",
lua_tostring(luaVm, -1));
lua_pop(luaVm, 1);
}
if (lua_isboolean(luaVm, -1)) {
result = lua_toboolean(luaVm, -1);
}
}
});
return result;
}
auto luaCall_onHttpRequest(std::string url, std::string metaData,
std::string respon, int statusCode) -> void {
ExcuteCallbackInAllLuaVm(_CallbackNames::kOnHttpRequest,
[&](lua_State* luaVm, int refIndex) -> void {
lua_rawgeti(luaVm, LUA_REGISTRYINDEX,
refIndex);
if (lua_isfunction(luaVm, -1)) {
lua_pushstring(luaVm, url.c_str());
lua_pushstring(luaVm, metaData.c_str());
lua_pushstring(luaVm, respon.c_str());
lua_pushinteger(luaVm, statusCode);
if (lua_pcall(luaVm, 4, 0, 0) != LUA_OK) {
LOG("Error calling Lua callback: %s\n",
lua_tostring(luaVm, -1));
lua_pop(luaVm, 1);
}
}
});
}
} // namespace ScriptCallBacks
9 changes: 7 additions & 2 deletions csgo2/script_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ enum class _CallbackNames {
kOnRoundStart,
kOnRoundEnd,
kOnPlayerHurt,
kOnPlayerTeamChange
kOnPlayerTeamChange,
kOnHttpRequest
};
extern std::unordered_map<lua_State*, std::unordered_map<_CallbackNames, int>>
callbackList;
Expand All @@ -34,5 +35,9 @@ auto luaCall_onRoundEnd(int winnerTeam, int reason, const char* message)
auto luaCall_onPlayerHurt(int userid, int attacker, int health, int armor,
const char* weapon, int dmg_health, int dmg_armor,
int hitgroup) -> void;
auto luaCall_onPlayerTeamChange(int userid, int team, int oldteam, bool disconnect, bool slient, bool isBot) -> bool;
auto luaCall_onPlayerTeamChange(int userid, int team, int oldteam,
bool disconnect, bool slient, bool isBot)
-> bool;
auto luaCall_onHttpRequest(std::string url, std::string metaData,
std::string respon, int statusCode) -> void;
} // namespace ScriptCallBacks

0 comments on commit 7c84e1e

Please sign in to comment.