ShareMemory_Circle.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // 下列 ifdef 块是创建使从 DLL 导出更简单的
  2. // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 SHAREMEMORY_CIRCLE_EXPORTS
  3. // 符号编译的。在使用此 DLL 的
  4. // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
  5. // SHAREMEMORY_CIRCLE_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
  6. // 符号视为是被导出的。
  7. #pragma once
  8. #ifndef SHAREMEMORY_CIRCLE_EXPORTS
  9. #ifdef _WIN64
  10. #ifdef _DEBUG
  11. #pragma comment(lib, "ShareMemory_CircleX64D.lib")
  12. #else
  13. #pragma comment(lib, "ShareMemory_CircleX64.lib")
  14. #endif
  15. #else
  16. #ifdef _DEBUG
  17. #pragma comment(lib, "ShareMemory_CircleD.lib")
  18. #else
  19. #pragma comment(lib, "ShareMemory_Circle.lib")
  20. #endif
  21. #endif
  22. #endif
  23. #ifdef SHAREMEMORY_CIRCLE_EXPORTS
  24. #define SHAREMEMORY_CIRCLE_API __declspec(dllexport)
  25. #define SHAREMEMORY_CIRCLE_C_API extern "C" __declspec(dllexport)
  26. #else
  27. #define SHAREMEMORY_CIRCLE_API __declspec(dllimport)
  28. #define SHAREMEMORY_CIRCLE_C_API extern "C" __declspec(dllimport)
  29. #endif
  30. #include <string>
  31. //#include"DiosThread.h"
  32. #include "ShareMemory_Client.h"
  33. using namespace std;
  34. #define SMCWaitTimeout 2000
  35. #define ShareMemoryNameLen 16
  36. //typedef struct _ShareMemoryCircleBlock
  37. //{
  38. // SHRAREMEMBLOCKID g_ID;
  39. // size_t BlockSize;
  40. //}SMCBLOCK;
  41. class SHAREMEMORY_CIRCLE_API CShareMemory_Circle{
  42. SMClientID m_SMinfo;
  43. char m_SMname[ShareMemoryNameLen];
  44. BYTE m_ShareMemoryId;
  45. DWORD m_BlockSize;
  46. int m_nMaxLimit;
  47. PVOID m_pLog;
  48. //void CreateLogger();
  49. BYTE m_ResetCount;
  50. unsigned int * m_pBlockSize;
  51. unsigned int * m_pMainCount;
  52. unsigned int * m_pMainFirstIdx;
  53. unsigned int * m_pMainLastIdx;
  54. unsigned int * m_pMainCurrentIdx;
  55. bool * m_pMainListEmpty;
  56. char * m_pClearUseTmpBuff;
  57. bool SetSMCctrl(int First, int Last, int Current);
  58. bool GetSMCctrl();
  59. bool UpdateSMCctrlCrrent(bool IncOrDec);
  60. bool UpdateSMCctrlPush(bool ForceFlag);
  61. bool UpdateSMCctrlPop(bool ForceFlag);
  62. bool SMCBidIsValid(ShareMemoryBlockID &SMCBid);
  63. bool APILock();
  64. bool APIUnLock();
  65. bool BlockLock(DWORD position);
  66. bool BlockUnLock(DWORD position);
  67. bool ClearFullBlock();
  68. bool CircleIsFull();
  69. public:
  70. CShareMemory_Circle();
  71. ~CShareMemory_Circle();
  72. //static BYTE m_type;
  73. int Create(const char * path, DWORD DataSize, DWORD DataCount, int Authority);
  74. bool Destroy();
  75. int Open(const char * path, int Authority);
  76. int Open(ShareMemoryBlockID &SMCBid, int Authority);
  77. bool Close();
  78. ShareMemoryBlockID Push(LPVOID buff, DWORD size, bool ForceFlag);//Write
  79. ShareMemoryBlockID Push(ShareMemoryBlockID SMBid, bool ForceFlag);//Write
  80. ShareMemoryBlockID PushRaw(string imageHead, LPVOID RawData, DWORD size, bool ForceFlag);//Write Raw and imageHead
  81. bool Pop(LPVOID & buff, DWORD & size);//Read
  82. ShareMemoryBlockID Pop();//Read
  83. bool PopLast(LPVOID & buff, DWORD & size);//Read
  84. ShareMemoryBlockID PopLast();//Read
  85. bool GetCurrent(LPVOID & buff, DWORD & size);
  86. ShareMemoryBlockID GetCurrent();
  87. bool GetCurrent(LPVOID & buff, DWORD & size, bool IncOrDec);
  88. ShareMemoryBlockID GetCurrent(bool IncOrDec);
  89. bool SetCurrent(int CurrentFlag, ShareMemoryBlockID & CurrentSMBid);
  90. DWORD ResetCircle(DWORD newBlockSize);
  91. bool Clear();
  92. bool Seek(UINT64 position);
  93. DWORD GetSize();
  94. DWORD GetDataCount();//有效数据个数
  95. DWORD GetLeftCount();//未读有效数据个数
  96. DWORD GetLimitCount();//能保存的最大数据个数
  97. bool Read(ShareMemoryBlockID &SMCBid, LPVOID buff, DWORD size);
  98. bool Write(ShareMemoryBlockID &SMCBid, LPVOID buff, DWORD size);
  99. bool Lock();
  100. bool UnLock();
  101. static bool SM_Circle_BlockIDVerify(ShareMemoryBlockID &SMCBid);
  102. bool GetBlockDataAddr(ShareMemoryBlockID &SMCBid, LPVOID & buff, DWORD & size);//获取到的buff是去除SMCELEMENTHEAD的整块block的首地址,size为去除SMCELEMENTHEAD的整块block大小
  103. };