QueueStrManager.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "stdafx.h"
  2. #include "QueueStrManager.h"
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #include <cstdio>
  6. #include <process.h>
  7. #include "ProcessRemodule.h"
  8. #include "./system_basic_info/SystemAnalysis.h"
  9. #pragma warning(disable: 4996)
  10. QueueStrManager::QueueStrManager(void)
  11. {
  12. m_pfunc = NULL;
  13. InitializeCriticalSectionAndSpinCount(&m_csQueueList, MAXCRITICALSECTIONSPINCOUNT);
  14. }
  15. QueueStrManager::~QueueStrManager(void)
  16. {
  17. DeleteCriticalSection(&m_csQueueList);
  18. delete[] m_chr;
  19. m_chr = NULL;
  20. m_pfunc = NULL;
  21. }
  22. void QueueStrManager::AddString(const char * chr)
  23. {
  24. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_100);
  25. EnterCriticalSection(&m_csQueueList);
  26. int vlen = strlen(chr);
  27. if(m_couter + 1 > m_max_couter || m_length + vlen + 1 > m_max_length){
  28. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_105);
  29. if(m_pfunc == NULL){
  30. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_106);
  31. LeaveCriticalSection(&m_csQueueList);
  32. return;
  33. }
  34. try
  35. {
  36. char* pchr = new char[m_max_length];
  37. strcpy(pchr, m_chr);
  38. //QueueUserWorkItem(m_pfunc, (LPVOID)pchr, WT_EXECUTEDEFAULT);
  39. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_101);
  40. send_sql_message(pchr);
  41. memset(m_chr, 0, m_max_length);
  42. strcpy(m_chr, chr);
  43. m_couter = 1;
  44. m_length = strlen(chr);
  45. m_last_exec_time = ::GetCurrentTime();
  46. }
  47. catch(...){
  48. debug_print_syslog(0,"[allocate memory error] call AddString()");
  49. }
  50. }else {
  51. if(m_couter == 0){
  52. strcpy(m_chr, chr);
  53. }else {
  54. strcat(m_chr, chr);
  55. }
  56. m_couter++;
  57. m_length += strlen(chr);
  58. }
  59. LeaveCriticalSection(&m_csQueueList);
  60. }
  61. void QueueStrManager::Execute(bool bExit /*= true*/)
  62. {
  63. // lihongzhen 2017/10/13 执行超过3秒的语句
  64. if(!bExit)
  65. {
  66. if(::GetCurrentTime() - m_last_exec_time < 3000)
  67. {
  68. return;
  69. }
  70. }
  71. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_103);
  72. EnterCriticalSection(&m_csQueueList);
  73. if(m_length > 0){
  74. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_107);
  75. if(m_pfunc == NULL){
  76. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_108);
  77. LeaveCriticalSection(&m_csQueueList);
  78. return;
  79. }
  80. try
  81. {
  82. char* pchr = new char[m_length + 2]; // 预留结束符
  83. strcpy(pchr, m_chr);
  84. //QueueUserWorkItem(m_pfunc, (LPVOID)pchr, WT_EXECUTEDEFAULT);
  85. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_104);
  86. send_sql_message(pchr);
  87. m_last_exec_time = ::GetCurrentTime();
  88. memset(m_chr, 0, m_max_length);
  89. m_couter = 0;
  90. m_length = 0;
  91. }
  92. catch(...){
  93. }
  94. }
  95. LeaveCriticalSection(&m_csQueueList);
  96. }