-
Notifications
You must be signed in to change notification settings - Fork 1
/
SharedMemory.h
69 lines (55 loc) · 1.38 KB
/
SharedMemory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef UM_COMMON_SHAREDMEMORY_H_
#define UM_COMMON_SHAREDMEMORY_H_
#include "AutoLock.h"
#define UMWEB_SHAREDMEMORY_NAME "UMLINK_SHAREMEMORY_DD6D1FC3-A8E1-4D26-9B10-9775FC9D5C11"
#define UMWEB_SHAREDMEMORY_SIZE 2048
namespace UMLink
{
class CSharedMemory
{
public:
typedef enum emSharedMemoryError
{
SME_NOERROR,
SME_CREATEMUTEXFAILED,
SME_CREATEFILEMAPINGFAILED,
SME_MAPVIEWOFFILEFAILED,
SME_CREATEFULLSMPHFAILED,
SME_CREATEEMPTYSMPHFAILED,
} SharedMemoryError;
typedef unsigned int MsgType;
#define UMWEB_SHAREDMEMORY_MSGTYPESIZE sizeof(MsgType)
#define UMWEB_SHAREDMEMORY_QUITMSG 0x0000
public:
CSharedMemory();
~CSharedMemory();
public:
SharedMemoryError InitSharedMemroy(const char* name, unsigned int size);
bool IsValid()
{
return m_bIsValid;
}
//ÐèÒª¹ÜÀíÄÚ´æ
bool ReadData(MsgType& type, void** data);
void WriteData(MsgType type, const void* data, int size);
template<typename T>
void WriteData(MsgType type, const T& data)
{
if (m_pMapView != NULL)
{
WriteData(type, &data, sizeof(T));
}
}
private:
SharedMemoryError InitLock(const char* name);
protected:
bool m_bIsValid;
HANDLE m_hFileMap;
LPVOID m_pMapView;
unsigned int m_nSize;
std::shared_ptr<CMutex> m_mutex;
std::shared_ptr<CSemaphore> m_emptySmph;
std::shared_ptr<CSemaphore> m_fullSmph;
};
}
#endif //UM_COMMON_SHAREDMEMORY_H_