Advertisement
jlalt

Threaded_Query

Jul 20th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 KB | None | 0 0
  1. #include <thread>
  2. #include <vector>
  3. #include <string>
  4. #include <time.h>
  5. #include "../SDK/plugin.h"
  6. #include "../Invoke/Invoke.h"
  7.  
  8. using namespace std;
  9.  
  10. typedef void
  11. (*logprintf_t)(char* format, ...)
  12. ;
  13.  
  14. logprintf_t
  15. logprintf
  16. ;
  17.  
  18. void
  19. **ppPluginData
  20. ;
  21.  
  22. extern void
  23. *pAMXFunctions
  24. ;
  25.  
  26. static time_t lasttime = 0;
  27.  
  28. class ThreadQuerysList
  29. {
  30. public:
  31.     AMX* address;
  32.     int database, extraid;
  33.     char *query, *callback;
  34.     ThreadQuerysList(AMX* amxaddress, int databaseid, char *query_, int extraid_, char *callback_)
  35.     {
  36.         address = amxaddress;
  37.         database = databaseid;
  38.         extraid = extraid_;
  39.         query = query_;
  40.         callback = callback_;
  41.     }
  42. };
  43.  
  44. vector<ThreadQuerysList> PendingQuerys;
  45.  
  46. PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData)
  47. {
  48.     g_Invoke = new Invoke;
  49.     pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
  50.     logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];
  51.     return 1;
  52. }
  53.  
  54. bool finishedquery = true;
  55.  
  56. void ThreadedQuery()
  57. {
  58.     g_Invoke->getAddresses();
  59.     ThreadQuerysList list = PendingQuerys[0];
  60.     int result = g_Invoke->callNative(&PAWN::db_query, list.database, list.query);
  61.     int idx;   
  62.     if ((time(NULL) - lasttime) < 1)
  63.     {
  64.         do
  65.         {
  66.             this_thread::sleep_for(chrono::milliseconds(500));
  67.         } while ((time(NULL) - lasttime) < 1);
  68.     }
  69.     if (!amx_FindPublic(list.address, list.callback, &idx))
  70.     {
  71.         cell ret;
  72.         amx_Push(list.address, result);
  73.         amx_Push(list.address, list.extraid);
  74.         amx_Exec(list.address, &ret, idx);
  75.         g_Invoke->callNative(&PAWN::db_free_result, result);
  76.     }
  77.     if ((time(NULL) - lasttime) < 1)
  78.     {
  79.         do
  80.         {
  81.             this_thread::sleep_for(chrono::milliseconds(500));
  82.         } while ((time(NULL) - lasttime) < 1);
  83.     }
  84.     PendingQuerys.erase(PendingQuerys.begin());
  85.     if (PendingQuerys.size())
  86.     {
  87.         do
  88.         {
  89.             ThreadQuerysList list2 = PendingQuerys[0];
  90.             int result2 = g_Invoke->callNative(&PAWN::db_query, list2.database, list2.query);
  91.             if ((time(NULL) - lasttime) < 1)
  92.             {
  93.                 do
  94.                 {
  95.                     this_thread::sleep_for(chrono::milliseconds(500));
  96.                 } while ((time(NULL) - lasttime) < 1);
  97.             }
  98.             int idx2 = 0;
  99.             if (!amx_FindPublic(list2.address, list2.callback, &idx2))
  100.             {
  101.                 cell ret2;
  102.                 amx_Push(list2.address, result2);
  103.                 amx_Push(list2.address, list2.extraid);
  104.                 amx_Exec(list2.address, &ret2, idx2);
  105.                 g_Invoke->callNative(&PAWN::db_free_result, result2);
  106.             }
  107.             if ((time(NULL) - lasttime) < 1)
  108.             {
  109.                 do
  110.                 {
  111.                     this_thread::sleep_for(chrono::milliseconds(500));
  112.                 } while ((time(NULL) - lasttime) < 1);
  113.             }
  114.             PendingQuerys.erase(PendingQuerys.begin());
  115.         } while (PendingQuerys.size() > 0);
  116.     }
  117.     finishedquery = true;
  118. }
  119.  
  120. #define CHECK_PARAMS(m, n) \
  121.     if (params[0] != (m * 4)) \
  122.     { \
  123.         logprintf("DBTQ: %s: Expecting %d parameter(s), but found %d", n, m, params[0] / sizeof(cell)); \
  124.         return 0; \
  125.     }
  126.  
  127. cell AMX_NATIVE_CALL db_tquery(AMX *amx, cell *params)
  128. {
  129.     CHECK_PARAMS(4, "db_tquery");
  130.     int database_ID = static_cast<int>(params[1]);
  131.     int extraid = static_cast<int>(params[3]);
  132.     int
  133.         len = NULL,
  134.         ret = NULL;
  135.  
  136.     cell *addr = NULL;
  137.     amx_GetAddr(amx, params[2], &addr);
  138.     amx_StrLen(addr, &len);
  139.     if (len)
  140.     {
  141.         len++;
  142.         char *query = new char[len];
  143.         amx_GetString(query, addr, 0, len);
  144.         amx_GetAddr(amx, params[4], &addr);
  145.         amx_StrLen(addr, &len);
  146.         len++;
  147.         char *callback = new char[len];
  148.         if (len)
  149.         {
  150.             amx_GetString(callback, addr, 0, len);
  151.         }
  152.        
  153.         if (finishedquery)
  154.         {
  155.             finishedquery = false;
  156.             PendingQuerys.push_back(ThreadQuerysList(amx, database_ID, query, extraid, callback));
  157.             thread mythread(ThreadedQuery);
  158.             mythread.detach();
  159.         }
  160.         else
  161.         {
  162.             PendingQuerys.push_back(ThreadQuerysList(amx, database_ID, query, extraid, callback));
  163.             lasttime = time(NULL);
  164.         }
  165.     }
  166.     return 1;
  167. }
  168.  
  169. PLUGIN_EXPORT void PLUGIN_CALL Unload()
  170. {
  171. }
  172.  
  173. AMX_NATIVE_INFO projectNatives[] =
  174. {
  175.     { "db_tquery", db_tquery },
  176.     { 0, 0 }
  177. };
  178.  
  179.  
  180. PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports()
  181. {
  182.     return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
  183. }
  184.  
  185. PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX *amx)
  186. {
  187.     g_Invoke->amx_list.push_back(amx);
  188.     return amx_Register(amx, projectNatives, -1);
  189. }
  190.  
  191. PLUGIN_EXPORT int PLUGIN_CALL AmxUnload(AMX *amx)
  192. {
  193.     for (std::list<AMX *>::iterator i = g_Invoke->amx_list.begin(); i != g_Invoke->amx_list.end(); ++i)
  194.     {
  195.         if (*i == amx)
  196.         {
  197.             g_Invoke->amx_list.erase(i);
  198.             break;
  199.         }
  200.     }
  201.     return AMX_ERR_NONE;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement