From 96218c4007c3a4608b77dccdba0a21e31dfab9d5 Mon Sep 17 00:00:00 2001 From: Adrian Graber Date: Sat, 22 Aug 2020 22:09:45 +0200 Subject: [PATCH] Add AMX to message struct for reliable callback invoking Previous implementation processed the entire message queue on the first loaded AMX which could be an unrelated script. Now it takes the AMX pointer and stores it on the message struct and uses that to invoke the callback. --- src/impl.cpp | 9 ++++++--- src/impl.hpp | 5 +++-- src/main.cpp | 4 +--- src/natives.cpp | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/impl.cpp b/src/impl.cpp index 921b3e2..2600c2c 100644 --- a/src/impl.cpp +++ b/src/impl.cpp @@ -395,7 +395,7 @@ int Impl::HIncrByFloat(int client_id, std::string key, std::string field, float return 0; } -int Impl::Subscribe(std::string host, int port, std::string auth, std::string channel, std::string callback, int& id) +int Impl::Subscribe(AMX* amx, std::string host, int port, std::string auth, std::string channel, std::string callback, int& id) { cpp_redis::subscriber* sub = new cpp_redis::subscriber(); sub->connect(host, port); @@ -404,8 +404,9 @@ int Impl::Subscribe(std::string host, int port, std::string auth, std::string ch sub->auth(auth); } - sub->subscribe(channel, [callback](const std::string& chan, const std::string& msg) { + sub->subscribe(channel, [amx, callback](const std::string& chan, const std::string& msg) { message m; + m.amx = amx; m.channel = chan; m.msg = msg; m.callback = callback; @@ -467,7 +468,7 @@ int Impl::Publish(int client_id, std::string channel, std::string data) return 0; } -void Impl::amx_tick(AMX* amx) +void Impl::amx_tick() { if (message_stack_mutex.try_lock()) { message m; @@ -480,6 +481,8 @@ void Impl::amx_tick(AMX* amx) while (!message_stack.empty()) { m = message_stack.top(); + AMX* amx = m.amx; + error = amx_FindPublic(amx, m.callback.c_str(), &amx_idx); if (error == AMX_ERR_NONE) { diff --git a/src/impl.hpp b/src/impl.hpp index a16a26f..491f85d 100644 --- a/src/impl.hpp +++ b/src/impl.hpp @@ -66,6 +66,7 @@ struct message { std::string channel; std::string msg; std::string callback; + AMX* amx; }; int Connect(std::string hostname, int port, std::string auth, int& id); @@ -87,13 +88,13 @@ int HIncrBy(int client_id, std::string key, std::string field, int incr); int HIncrByFloat(int client_id, std::string key, std::string field, float incr); int HDel(int client_id, std::string key, std::string field); -int Subscribe(std::string host, int port, std::string auth, std::string channel, std::string callback, int& id); +int Subscribe(AMX* amx, std::string host, int port, std::string auth, std::string channel, std::string callback, int& id); int Unsubscribe(int client_id); int Publish(int client_id, std::string channel, std::string message); int clientFromID(int client_id, cpp_redis::client*& client); int clientDataFromID(int client_id, clientData& client); -void amx_tick(AMX* amx); +void amx_tick(); std::vector split(const std::string s); extern int context_count; diff --git a/src/main.cpp b/src/main.cpp index c3c2d09..9a681fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -95,9 +95,7 @@ PLUGIN_EXPORT void PLUGIN_CALL Unload() PLUGIN_EXPORT void PLUGIN_CALL ProcessTick() { - for (AMX* i : amx_list) { - Impl::amx_tick(i); - } + Impl::amx_tick(); } PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX* amx) diff --git a/src/natives.cpp b/src/natives.cpp index 4bc5e9d..6493ac0 100644 --- a/src/natives.cpp +++ b/src/natives.cpp @@ -357,7 +357,7 @@ cell Natives::Subscribe(AMX* amx, cell* params) cell* addr; amx_GetAddr(amx, params[6], &addr); try { - return Impl::Subscribe(host, port, auth, channel, callback, *addr); + return Impl::Subscribe(amx, host, port, auth, channel, callback, *addr); } catch (cpp_redis::redis_error e) { logprintf("ERROR: %s", e.what());