add async http requests

This commit is contained in:
Huoji's
2023-10-14 03:43:39 +08:00
parent 5c7a315565
commit 7c84e1e1df
4 changed files with 150 additions and 26 deletions

View File

@@ -13,7 +13,14 @@ CURLcode HttpGet(const std::string& strUrl, std::string& strResponse,
return CURLE_FAILED_INIT;
}
struct curl_slist* headers = NULL;
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);
@@ -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;
}
@@ -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(),

View File

@@ -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;
@@ -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);
@@ -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")

View File

@@ -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) {
@@ -206,7 +208,9 @@ 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 {
@@ -232,4 +236,23 @@ auto luaCall_onPlayerTeamChange(int userid, int team, int oldteam, bool disconne
});
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

View File

@@ -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;
@@ -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