DeliverModule.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #pragma once
  2. #include <mutex>
  3. #include <string>
  4. #include <deque>
  5. #include <vector>
  6. class tagCommandTye
  7. {
  8. public:
  9. int nCommandType;
  10. bool IsWaitforACK;
  11. int WaitingTime;
  12. bool IsResend;
  13. bool IsClearSendVector;
  14. bool IsFirstSend;
  15. bool IsWaitforCP;
  16. int WatingCPtime;
  17. int NAKTimes;
  18. int NCPTimes;
  19. bool isClearSamePriority;
  20. tagCommandTye()
  21. {
  22. nCommandType=-1;
  23. IsWaitforACK=false;
  24. WaitingTime=300;
  25. IsResend=true;
  26. IsClearSendVector=false;
  27. IsFirstSend=false;
  28. IsWaitforCP=false;
  29. WatingCPtime=1000;
  30. NAKTimes=3;
  31. NCPTimes=1;
  32. isClearSamePriority=false;
  33. };
  34. tagCommandTye(bool newIsWaitforACK,int newWaitingTime,
  35. bool newIsResend,bool newIsClearSendVector,bool newIsFirstSend,
  36. bool newIsWaitforCP,int newWatingCPtime,int newNAKTimes,int newNCPTimes,bool newisClearSamePriority)
  37. {
  38. nCommandType=-1;
  39. IsWaitforACK=newIsWaitforACK;
  40. WaitingTime=newWaitingTime;
  41. IsResend=newIsResend;
  42. IsClearSendVector=newIsClearSendVector;
  43. IsFirstSend=newIsFirstSend;
  44. IsWaitforCP=newIsWaitforCP;
  45. WatingCPtime=newWatingCPtime;
  46. NAKTimes=newNAKTimes;
  47. NCPTimes=newNCPTimes;
  48. isClearSamePriority=newisClearSamePriority;
  49. };
  50. };
  51. //通过回调函数调用上层处理函数
  52. typedef void __stdcall SendCommands(const char * pData,unsigned long DataLength,void* lparam);
  53. //通过回调函数写日志
  54. enum LOG_V2_LEVEL
  55. {
  56. LOG_V2_FATAL = 0, // Fatal Error.
  57. LOG_V2_ERROR = 1, // Error.
  58. LOG_V2_WARNING = 2, // Warning.
  59. LOG_V2_INFO = 3 // Information.
  60. };
  61. typedef void __stdcall WriteLog(const char* pData, LOG_V2_LEVEL level);
  62. class CDeliverModule
  63. {
  64. struct tagCommandStruct
  65. {
  66. std::string strCommand;
  67. //0 等回复,250ms超时重发3次
  68. //1 不等回复,也不重发
  69. int nCommmandType;
  70. };
  71. public:
  72. CDeliverModule(WriteLog* logFun = NULL);
  73. ~CDeliverModule(void);
  74. public://发送线程相关的函数
  75. bool SetLogFun(WriteLog* logFun);
  76. bool InitSendModle(SendCommands* pSendData,void* lparam = NULL);
  77. void EixtSendModle();
  78. static DWORD SendingCommandsThread(LPVOID pParam);
  79. bool StartSendingCommandsThread(CDeliverModule* pCurrentGen);
  80. void StopSendingCommandsThread();
  81. bool ProcessCommand(std::string strCommand,int nType = 0);
  82. bool ReProcessCommand(tagCommandStruct strCommand);
  83. void SendCommand(std::string strcommand, int commandtype);
  84. bool WaitforACK(DWORD nWaitingTime);
  85. bool ReceiveACK (bool IsACK=true);
  86. bool WaitforCP(DWORD nWaitingTime);
  87. bool ReceiveCP (bool IsCP=true);
  88. void SetPriority(int nCommandType,bool IsWaitforACK=true,int WaitingTime=350,bool IsResend=true,
  89. bool IsClearSendVector=false,bool IsFirstSend=false,bool IsWaitforCP=false,int WatingCPtime=3000,bool isClearSamePriority=false);
  90. void SetGenErrStatus(bool isGenErr);
  91. std::string GetCurrentCommand();
  92. int GetCurrentCommandPriority();
  93. bool GetCurrentErrorState();
  94. void SetAKResendNum(int ResendNum);
  95. void SetCPResendNum(int ResendNum);
  96. private:
  97. int m_nAKResendTotalNum;
  98. int m_nCPResendTotalNum;
  99. public:
  100. tagCommandStruct m_strcurrentcmd;
  101. HANDLE m_hSendingThreadToggleEvent;
  102. HANDLE m_SendingCommandsThread;
  103. int m_nSendingCommandsThreadId;
  104. bool m_bSendingCommandsThreadExit;
  105. SendCommands* m_pSendData;
  106. WriteLog* m_pLogFun;
  107. tagCommandStruct m_SDCCommand;
  108. std::mutex m_iCriticalSection;
  109. std::deque <tagCommandStruct> m_SdcCommondvector;
  110. std::vector <tagCommandTye> m_CommondType;
  111. HANDLE m_hACKEvent;
  112. int m_nAKRsubmit;
  113. bool m_bisNAK;
  114. HANDLE m_hCPEvent;
  115. int m_nCPRsubmit;
  116. bool m_bisNCP;
  117. void *m_ClientControl;
  118. bool m_bIsGenErr;
  119. };