Skip to content

Commit 12eb730

Browse files
committed
Remote: Async fetch & Post
1 parent e541dae commit 12eb730

File tree

3 files changed

+208
-17
lines changed

3 files changed

+208
-17
lines changed

VCMP-LUA/vcmpWrap/Modules/CPR/Remote.cpp

Lines changed: 154 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,75 @@
22

33
extern sol::state Lua;
44

5+
std::vector<RemoteRequest> Remote::s_Container = {};
6+
7+
/*** Lua ***/
58
sol::table Remote::fetchHTTP(const std::string& url, sol::table params, sol::table auth)
69
{
7-
cpr::Parameters cprParams;
8-
std::string authUser(""), authPassword("");
10+
cpr::Parameters cprParams = GetParameters(params);
11+
cpr::Authentication cprAuth = GetAuthentication(auth);
912

10-
for (auto [key, value] : params)
13+
cpr::Response r = cpr::Get(
14+
cpr::Url(url),
15+
cprParams,
16+
cprAuth
17+
);
18+
19+
sol::table resultTable = Lua.create_table();
20+
resultTable["status_code"] = r.status_code;
21+
resultTable["error"] = r.error.code;
22+
resultTable["error_message"] = r.error.message;
23+
resultTable["header"] = sol::as_table(r.header);
24+
resultTable["text"] = r.text;
25+
return resultTable;
26+
}
27+
28+
void Remote::fetchHTTPAsync(sol::function handler, const std::string& url, sol::table params, sol::table auth)
29+
{
30+
cpr::Parameters cprParams = GetParameters(params);
31+
cpr::Authentication cprAuth = GetAuthentication(auth);
32+
s_Container.emplace_back(handler, RemoteRequestType::GET, url, cprParams, cpr::Header{}, cpr::Body(""), cpr::Payload{}, cprAuth);
33+
}
34+
35+
sol::table Remote::postHTTP(const std::string& url, sol::table data)
36+
{
37+
cpr::Parameters cprParams;
38+
if (data["params"].valid())
1139
{
12-
cpr::Parameter cprParameter = cpr::Parameter(key.as<std::string>(), value.as<std::string>());
13-
cprParams.Add(cprParameter);
40+
for (auto [key, value] : data.get<sol::table>("params"))
41+
{
42+
cprParams.Add(cpr::Parameter(key.as<std::string>(), value.as<std::string>()));
43+
}
1444
}
1545

16-
if (auth.size() == 2)
46+
cpr::Header cprHeader;
47+
if (data["header"].valid())
1748
{
18-
if (auth["user"].valid())
19-
authUser = auth["user"].get<std::string>();
20-
21-
if (auth["password"].valid())
22-
authPassword = auth["password"].get<std::string>();
49+
for (auto [key, value] : data.get<sol::table>("header"))
50+
{
51+
const std::string strKey = key.as<std::string>();
52+
const std::string strVal = value.as<std::string>();
53+
cprHeader.insert({ strKey, strVal });
54+
}
2355
}
2456

25-
cpr::Authentication cprAuth(authUser, authPassword);
57+
cpr::Body cprBody(data.get_or<std::string>("body", ""));
58+
59+
cpr::Payload cprPayload({});
60+
if (data["payload"].valid())
61+
{
62+
for (auto [key, value] : data.get<sol::table>("payload"))
63+
{
64+
cprPayload.Add(cpr::Pair(key.as<std::string>(), value.as<std::string>()));
65+
}
66+
}
2667

27-
cpr::Response r = cpr::Get(
68+
cpr::Response r = cpr::Post(
2869
cpr::Url(url),
70+
cprHeader,
71+
cprBody,
2972
cprParams,
30-
cprAuth
73+
cprPayload
3174
);
3275

3376
sol::table resultTable = Lua.create_table();
@@ -39,9 +82,104 @@ sol::table Remote::fetchHTTP(const std::string& url, sol::table params, sol::tab
3982
return resultTable;
4083
}
4184

85+
void Remote::postHTTPAsync(sol::function handler, const std::string& url, sol::table data)
86+
{
87+
cpr::Parameters cprParams;
88+
if (data["params"].valid())
89+
{
90+
for (auto [key, value] : data.get<sol::table>("params"))
91+
{
92+
cprParams.Add(cpr::Parameter(key.as<std::string>(), value.as<std::string>()));
93+
}
94+
}
95+
96+
cpr::Header cprHeader;
97+
if (data["header"].valid())
98+
{
99+
for (auto [key, value] : data.get<sol::table>("header"))
100+
{
101+
const std::string strKey = key.as<std::string>();
102+
const std::string strVal = value.as<std::string>();
103+
cprHeader.insert({ strKey, strVal });
104+
}
105+
}
106+
107+
cpr::Body cprBody(data.get_or<std::string>("body", ""));
108+
109+
cpr::Payload cprPayload({});
110+
if (data["payload"].valid())
111+
{
112+
for (auto [key, value] : data.get<sol::table>("payload"))
113+
{
114+
cprPayload.Add(cpr::Pair(key.as<std::string>(), value.as<std::string>()));
115+
}
116+
}
117+
118+
s_Container.emplace_back(handler, RemoteRequestType::POST, url, cprParams, cprHeader, cprBody, cprPayload, cpr::Authentication("", ""));
119+
}
120+
121+
/*** Internal ***/
42122
void Remote::Init(sol::state* Lua)
43123
{
124+
s_Container.reserve(32);
125+
44126
sol::usertype<Remote> userdata = Lua->new_usertype<Remote>("Remote");
45127

46-
userdata["fetch"] = &Remote::fetchHTTP;
47-
}
128+
userdata["fetch"] = sol::overload(&Remote::fetchHTTP, &Remote::fetchHTTPAsync);
129+
userdata["post"] = sol::overload(&Remote::postHTTP, &Remote::postHTTPAsync);
130+
}
131+
132+
void Remote::Process(float elapsedTime)
133+
{
134+
if (s_Container.size() == 0) return;
135+
using namespace std::chrono_literals;
136+
137+
for (auto& it = s_Container.begin(); it != s_Container.end(); it++)
138+
{
139+
if (it->future.wait_for(0s) == std::future_status::ready)
140+
{
141+
auto r = it->future.get();
142+
sol::table resultTable = Lua.create_table();
143+
resultTable["status_code"] = r.status_code;
144+
resultTable["error"] = r.error.code;
145+
resultTable["error_message"] = r.error.message;
146+
resultTable["header"] = sol::as_table(r.header);
147+
resultTable["text"] = r.text;
148+
it->handler(resultTable);
149+
150+
it = s_Container.erase(it);
151+
if (it == s_Container.end())
152+
break;
153+
}
154+
}
155+
}
156+
157+
cpr::Parameters Remote::GetParameters(sol::table& params)
158+
{
159+
cpr::Parameters cprParams;
160+
161+
for (auto [key, value] : params)
162+
{
163+
cpr::Parameter cprParameter = cpr::Parameter(key.as<std::string>(), value.as<std::string>());
164+
cprParams.Add(cprParameter);
165+
}
166+
167+
return cprParams;
168+
}
169+
170+
cpr::Authentication Remote::GetAuthentication(sol::table& auth)
171+
{
172+
std::string authUser(""), authPassword("");
173+
174+
if (auth.size() == 2)
175+
{
176+
if (auth["user"].valid())
177+
authUser = auth["user"].get<std::string>();
178+
179+
if (auth["password"].valid())
180+
authPassword = auth["password"].get<std::string>();
181+
}
182+
183+
cpr::Authentication cprAuth(authUser, authPassword);
184+
return cprAuth;
185+
}

VCMP-LUA/vcmpWrap/Modules/CPR/Remote.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,60 @@
33

44
#include <cpr/cpr.h>
55

6+
enum RemoteRequestType
7+
{
8+
GET,
9+
POST
10+
};
11+
12+
struct RemoteRequest
13+
{
14+
RemoteRequest(sol::function luaHandler,
15+
RemoteRequestType requestType,
16+
const std::string& url,
17+
cpr::Parameters cprParams,
18+
cpr::Header cprHeader,
19+
cpr::Body cprBody,
20+
cpr::Payload cprPayload,
21+
cpr::Authentication cprAuth
22+
)
23+
: handler(luaHandler)
24+
{
25+
if (requestType == RemoteRequestType::GET)
26+
future = cpr::GetAsync(cpr::Url(url),
27+
cprParams,
28+
cprAuth
29+
);
30+
else if (requestType == RemoteRequestType::POST)
31+
future = cpr::PostAsync(cpr::Url(url),
32+
cprParams,
33+
cprHeader,
34+
cprBody,
35+
cprPayload,
36+
cprAuth
37+
);
38+
}
39+
40+
std::future<cpr::Response> future;
41+
sol::function handler;
42+
};
43+
644
class Remote
745
{
846
public:
947
static void Init(sol::state* Lua);
48+
static void Process(float elapsedTime);
49+
50+
static std::vector<RemoteRequest> s_Container;
1051

52+
/*** Internal ***/
53+
static cpr::Parameters GetParameters(sol::table& params);
54+
static cpr::Authentication GetAuthentication(sol::table& auth);
55+
56+
/*** Lua ***/
1157
static sol::table fetchHTTP(const std::string& url, sol::table params, sol::table auth);
58+
static void fetchHTTPAsync(sol::function handler, const std::string& url, sol::table params, sol::table auth);
59+
60+
static sol::table postHTTP(const std::string& url, sol::table data);
61+
static void postHTTPAsync(sol::function handler, const std::string& url, sol::table data);
1262
};

VCMP-LUA/vcmpWrap/vcmpCallbacks.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "Classes/Vehicle.h"
77
#include "Classes/Bind.h"
88

9+
#include "Modules/CPR/Remote.h"
10+
911
extern PluginFuncs* g_Funcs;
1012
extern PluginCallbacks* g_Calls;
1113
extern sol::state Lua;
@@ -15,7 +17,7 @@ void RegisterVCMPCallbacks() {
1517
g_Calls->OnServerInitialise = [] () -> uint8_t {
1618
spdlog::debug("onServerinitialise");
1719

18-
auto handlers = EventManager::GetHandlers("onServerInit");
20+
auto handlers = EventManager::GetHandlers("onServerInit");
1921
if (handlers.size() == 0) return 1;
2022
uint8_t ret = 1;
2123
try {
@@ -64,6 +66,7 @@ void RegisterVCMPCallbacks() {
6466
g_Calls->OnServerFrame = [](float elapsedTime) -> void {
6567
//spdlog::debug("OnServerFrame");
6668

69+
Remote::Process(elapsedTime);
6770
TimerManager::OnFrame(elapsedTime);
6871
auto handlers = EventManager::GetHandlers("onServerFrame");
6972
if (handlers.size() == 0) return;

0 commit comments

Comments
 (0)