#include "pch.h" #include "tunnel.h" #include "protocol.h" #include "globalcfg.h" #include "httplib.h" #include "usrerr.h" #define HTTP_JSON_CONTENT TEXT("application/json") #define GET_USR_CFG_PATH TEXT("/tunnel/getuserconfig") static httplib::Client *g_httpCtx = nullptr; template int CreateProtocolRequest(T *pReqParams, TCHAR **pOutJson) { std::string json; if (!g_httpCtx && lstrlen(GetGlobalCfgInfo()->platformServerUrl) > 0) { g_httpCtx = new httplib::Client(GetGlobalCfgInfo()->platformServerUrl); if (g_httpCtx) { g_httpCtx->set_connection_timeout(0, 300000); // 300 milliseconds g_httpCtx->set_read_timeout(5, 0); // 5 seconds g_httpCtx->set_write_timeout(5, 0); // 5 seconds } } if (aigc::JsonHelper::ObjectToJson(*pReqParams, json)) { *pOutJson = _strdup(json.c_str()); return ERR_SUCCESS; } return -ERR_JSON_CREATE; } template int DecodeProtocolResponse(T *pResponse, const TCHAR *pJson) { if (aigc::JsonHelper::JsonToObject(*pResponse, pJson)) { return ERR_SUCCESS; } return -ERR_JSON_DECODE; } template int ProtolPostMessage(ProtocolRequest *pReq, ProtocolResponse *pRsp) { int ret; TCHAR *pJson = nullptr; if (lstrlen(GetGlobalCfgInfo()->platformServerUrl) == 0) { SPDLOG_ERROR(TEXT("Platform Server URL uninitialize.")); return -ERR_SYSTEM_UNINITIALIZE; } if (pReq == nullptr) { SPDLOG_ERROR("Input pToken params error"); SPDLOG_ERROR("Input ProtocolRequest *pReq params error"); return -ERR_INPUT_PARAMS; } if (pRsp == nullptr) { SPDLOG_ERROR("Input ProtocolResponse *pRsp params error"); return -ERR_INPUT_PARAMS; } ret = CreateProtocolRequest(pReq, &pJson); if (ret != ERR_SUCCESS) { if (pJson) { free(pJson); } return ret; } const auto res = g_httpCtx->Post(GET_USR_CFG_PATH, pJson, HTTP_JSON_CONTENT); free(pJson); if (res.error() != httplib::Error::Success) { SPDLOG_ERROR(TEXT("[{0}]:Post Data {1} error: {2}"), GET_USR_CFG_PATH, pJson, httplib::to_string(res.error())); return -ERR_HTTP_POST_DATA; } if (res->status != 200) { SPDLOG_ERROR(TEXT("[{0}]:Post Data {1} server return HTTP error: {2}"), GET_USR_CFG_PATH, pJson, res->status); return -ERR_HTTP_SERVER_RSP; } if (DecodeProtocolResponse(pRsp, res->body.c_str()) != ERR_SUCCESS) { SPDLOG_ERROR(TEXT("Decode JSON {0} to UserClientConfigParams error"), res->body); return -ERR_JSON_DECODE; } return ERR_SUCCESS; } int ProtoGetUserConfigure(const TCHAR *pUser, const TCHAR *pToken) { int ret; TCHAR *pJson = nullptr; ProtocolRequest req; ProtocolResponse rsp; ret = ProtolPostMessage(&req, &rsp); return ret; }