v_msg.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef _MSG_H_
  2. #define _MSG_H_
  3. #include <fcntl.h> /* For O_* constants */
  4. #include <sys/stat.h> /* For mode constants */
  5. #include <map>
  6. #include <vector>
  7. #include <atomic>
  8. #include <winsock2.h>
  9. #include <condition_variable>
  10. using namespace std;
  11. #define MAX_MSG_QUE_SIZE 1000
  12. #define MAX_MSG_BUF_LEN 4080
  13. typedef int TInt;
  14. typedef unsigned int TUInt;
  15. typedef long TLong;
  16. typedef unsigned long TULong;
  17. class TSpinMutex {
  18. atomic_flag *pLockWrite;// = ATOMIC_FLAG_INIT;
  19. public:
  20. TSpinMutex() { pLockWrite = new atomic_flag{ ATOMIC_FLAG_INIT }; };
  21. TSpinMutex(const TSpinMutex&) = delete;
  22. TSpinMutex& operator= (const TSpinMutex&) = delete;
  23. void lock() { while ((pLockWrite->test_and_set())); } // 获取自旋锁
  24. void unlock() { pLockWrite->clear(); } // 释放自旋锁
  25. };
  26. //typedef std::mutex TMutex_t;
  27. typedef TSpinMutex TMutex_t;
  28. class TMutex
  29. {
  30. public:
  31. TMutex(TMutex_t *pMutex)
  32. {
  33. m_pMutex = pMutex;
  34. pMutex->lock();
  35. }
  36. ~TMutex()
  37. {
  38. m_pMutex->unlock();
  39. }
  40. private:
  41. TMutex_t *m_pMutex;
  42. };
  43. struct TMsgNode
  44. {
  45. void *pMsg;
  46. TMsgNode():pMsg(NULL){}
  47. ~TMsgNode(){pMsg = NULL;}
  48. };
  49. typedef void TSem;
  50. //typedef std::condition_variable TSem;
  51. class TMsgInfo
  52. {
  53. public:
  54. TMsgNode *m_pMsgHead;
  55. TSem *m_pSem;
  56. TSpinMutex *m_pSpinMutex;
  57. TInt m_iMsgSize;
  58. TUInt m_iTailPos;
  59. TUInt m_iHeadPos;
  60. TUInt m_iMaxSize;
  61. TMsgInfo() :m_iMsgSize(0), m_iTailPos(0), m_iHeadPos(0){ init(100); }
  62. TMsgInfo(TUInt iSize) :m_iMsgSize(0), m_iTailPos(0), m_iHeadPos(0) { init(iSize); }
  63. ~TMsgInfo() { clear(); }
  64. TInt Push_Msg(void *pMsgContent);
  65. void *Pop_Msg(TULong ulMilliseconds);
  66. TBool IsEmpty(){ return (m_iTailPos == m_iHeadPos ? VOS_TRUE : VOS_FALSE); }
  67. private:
  68. void init(int iSize);
  69. void clear();
  70. };
  71. struct TMsgBody
  72. {
  73. TMsgBody *pPrev;
  74. TMsgBody *pNext;
  75. char acBody[MAX_MSG_BUF_LEN];
  76. };
  77. struct TMsgHead
  78. {
  79. TMsgBody *pPrev;
  80. TMsgBody *pNext;
  81. TInt iFreeSize;
  82. TInt iReserve;
  83. };
  84. class TMsg
  85. {
  86. public:
  87. static TMsg *GetInstance();
  88. TInt SendMsg(void *pMsgContent, TInt iMsgLen); // 发送消息到消息队列
  89. void *RecvMsg(TULong ulMilliseconds); // 从消息队列中获取消息,接收线程调用
  90. void *ObtainMsg(); // 获取消息buffer
  91. void FreeMsg(void *pvMsg); // 释放获取的消息buffer
  92. private:
  93. TInt InitMsgInfo();
  94. inline void InsertFree(TMsgBody *pMsgNode);
  95. void *PopFreeMsg();
  96. inline TBool IsEmpty();
  97. private:
  98. TMsg();
  99. static TMsg *pInstance;
  100. TMsgInfo *m_pMsgQue;
  101. TMsgBody *m_pFreeMsgHead;
  102. TMsgHead *m_pFreeHead; // 与 m_pFreeMsgHead 地址相同
  103. TSpinMutex *m_pSpinMutex; // 保护 m_pFreeMsgHead
  104. };
  105. #endif