Skip to content

Commit

Permalink
fix: processing requests would fail due to no matching amx instance w…
Browse files Browse the repository at this point in the history
…hen other amx instances where used
  • Loading branch information
ADRFranklin committed Nov 25, 2022
1 parent d859cc0 commit 7f81a8a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
52 changes: 32 additions & 20 deletions src/natives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,26 @@ int Natives::RequestJSON(AMX *amx, cell *params)
return Impl::RequestJSON(amx, id, path, method, callback, obj, headers);
}

void Natives::processTick(AMX *amx)
void Natives::processTick(std::set<AMX *> amx_List)
{
std::vector<Impl::ResponseData> responses = Impl::gatherResponses();
for (auto response : responses)
{
if (response.amx != amx)
AMX *currentAmx = nullptr;
for (AMX *amx : amx_List)
{
continue;
if (amx != response.amx)
{
continue;
}

currentAmx = amx;
break;
}

if (currentAmx == nullptr)
{
return;
}

int error;
Expand All @@ -73,7 +85,7 @@ void Natives::processTick(AMX *amx)
cell amx_ret;
cell *phys_addr;

error = amx_FindPublic(amx, response.callback.c_str(), &amx_idx);
error = amx_FindPublic(currentAmx, response.callback.c_str(), &amx_idx);
if (error != AMX_ERR_NONE)
{
logprintf("ERROR: failed to locate public function '%s' in amx, error: %d", response.callback.c_str(), error);
Expand All @@ -90,21 +102,21 @@ void Natives::processTick(AMX *amx)
case Impl::E_CONTENT_TYPE::empty:
{
// (Request:id, errorCode, errorMessage[], len)
amx_Push(amx, response.rawBody.length());
amx_PushString(amx, &amx_addr, &phys_addr, response.rawBody.c_str(), 0, 0);
amx_Push(amx, response.status);
amx_Push(amx, response.id);
amx_Push(currentAmx, response.rawBody.length());
amx_PushString(currentAmx, &amx_addr, &phys_addr, response.rawBody.c_str(), 0, 0);
amx_Push(currentAmx, response.status);
amx_Push(currentAmx, response.id);

amx_Exec(amx, &amx_ret, amx_idx);
amx_Release(amx, amx_addr);
amx_Exec(currentAmx, &amx_ret, amx_idx);
amx_Release(currentAmx, amx_addr);

break;
}

case Impl::E_CONTENT_TYPE::string:
{
amx_Push(amx, response.rawBody.length());
amx_PushString(amx, &amx_addr, &phys_addr, response.rawBody.c_str(), 0, 0);
amx_Push(currentAmx, response.rawBody.length());
amx_PushString(currentAmx, &amx_addr, &phys_addr, response.rawBody.c_str(), 0, 0);

// signature is either
// (Request:id, E_HTTP_STATUS:status, data[], dataLen)
Expand All @@ -113,13 +125,13 @@ void Natives::processTick(AMX *amx)
// depending on whether the response is from a websocket
if (!response.isWebSocket)
{
amx_Push(amx, response.status);
amx_Push(currentAmx, response.status);
}

amx_Push(amx, response.id);
amx_Push(currentAmx, response.id);

amx_Exec(amx, &amx_ret, amx_idx);
amx_Release(amx, amx_addr);
amx_Exec(currentAmx, &amx_ret, amx_idx);
amx_Release(currentAmx, amx_addr);

break;
}
Expand All @@ -138,19 +150,19 @@ void Natives::processTick(AMX *amx)
logprintf("ERROR: failed to parse response as JSON: '%s'", response.rawBody.c_str());
}

amx_Push(amx, id);
amx_Push(currentAmx, id);
// signature is either
// (Request:id, E_HTTP_STATUS:status, Node:node)
// or:
// (WebSocket:id, Node:node)
// depending on whether the response is from a websocket
if (!response.isWebSocket)
{
amx_Push(amx, response.status);
amx_Push(currentAmx, response.status);
}
amx_Push(amx, response.id);
amx_Push(currentAmx, response.id);

amx_Exec(amx, &amx_ret, amx_idx);
amx_Exec(currentAmx, &amx_ret, amx_idx);

JSON::Erase(id);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/natives.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Natives
int JsonWebSocketClient(AMX *amx, cell *params);
int JsonWebSocketSend(AMX *amx, cell *params);

void processTick(AMX *amx);
void processTick(std::set<AMX *> amx_List);

namespace JSON
{
Expand Down
5 changes: 1 addition & 4 deletions src/requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX *amx)

PLUGIN_EXPORT void PLUGIN_CALL ProcessTick()
{
for (AMX *i : amx_List)
{
Natives::processTick(i);
}
Natives::processTick(amx_List);
}

PLUGIN_EXPORT int PLUGIN_CALL Unload()
Expand Down

0 comments on commit 7f81a8a

Please sign in to comment.