-
Notifications
You must be signed in to change notification settings - Fork 1
/
SharedMemory.cpp
168 lines (143 loc) · 4.2 KB
/
SharedMemory.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "stdafx.h"
#include "SharedMemory.h"
const char* szMutexTail = "%_Mutex";
const char* szEmptyTail = "%_EmptySmph";
const char* szFullTail = "%_FullSmph";
namespace UMLink
{
CSharedMemory::CSharedMemory()
: m_mutex(NULL), m_hFileMap(NULL), m_pMapView(NULL)
, m_emptySmph(NULL), m_fullSmph(NULL), m_nSize(0), m_bIsValid(false)
{
}
CSharedMemory::~CSharedMemory()
{
CAutoMutex lock(m_mutex.get());
UnmapViewOfFile(m_pMapView);
CloseHandle(m_hFileMap);
}
CSharedMemory::SharedMemoryError CSharedMemory::InitLock(const char* name)
{
SharedMemoryError ret = SME_NOERROR;
do
{
std::string strMutexName(name);
strMutexName.append(szMutexTail);
m_mutex = std::make_shared<CMutex>(strMutexName.c_str());
if (!m_mutex->IsValid())
{
ret = SME_CREATEMUTEXFAILED;
break;
}
std::string strEmptySmphName(name);
strEmptySmphName.append(szEmptyTail);
m_emptySmph = std::make_shared<CSemaphore>(strEmptySmphName.c_str(), 1, 1);
if (!m_emptySmph->IsValid())
{
ret = SME_CREATEEMPTYSMPHFAILED;
break;
}
std::string strFullSmphName(name);
strFullSmphName.append(szFullTail);
m_fullSmph = std::make_shared<CSemaphore>(strFullSmphName.c_str(), 0, 1);
if (!m_fullSmph->IsValid())
{
ret = SME_CREATEFULLSMPHFAILED;
break;
}
} while (false);
return ret;
}
CSharedMemory::SharedMemoryError CSharedMemory::InitSharedMemroy(const char* name, unsigned int size)
{
SharedMemoryError ret = SME_NOERROR;
m_nSize = size;
do
{
if ((ret = InitLock(name)) != SME_NOERROR)
{
LD("ShareMemory::InitLock Error::" << ret);
break;
}
CAutoMutex lock(m_mutex.get());
bool bExist = false;
m_hFileMap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, name);
DWORD errorCode = GetLastError();
if (m_hFileMap == NULL)
{
assert(false);
LD("ShareMemory::CreateFileMapping Error:" << errorCode);
ret = SME_CREATEFILEMAPINGFAILED;
break;
}
else
{
if (errorCode == 183)
{
bExist = true;
}
}
m_pMapView = MapViewOfFile(m_hFileMap, FILE_MAP_READ | FILE_MAP_WRITE, 0 ,0 ,0);
DWORD errorCode2 = GetLastError();
if (m_pMapView == NULL)
{
assert(false);
LD("ShareMemory::MapViewOfFile Error:" << errorCode2);
ret = SME_MAPVIEWOFFILEFAILED;
break;
}
bExist ? void() : memset(m_pMapView, 0, size);
m_bIsValid = true;
} while (false);
return ret;
}
bool CSharedMemory::ReadData(MsgType& type, void** data)
{
LD("SharedMemory::ReadData::Enter");
bool bRet = false;
if (m_pMapView != NULL)
{
LD("SharedMemory::ReadData::MapViewValid");
bRet = true;
m_fullSmph->Lock();
LD("SharedMemory::ReadData::GetFullSemaphore");
//目前仅一个资源不用加锁 可以考虑指定容器allocator从mapView中申请内存实现多资源
//CAutoMutex _lock(m_mutex.get());
memcpy((unsigned char*)&type, m_pMapView, UMWEB_SHAREDMEMORY_MSGTYPESIZE);
if (type == UMWEB_SHAREDMEMORY_QUITMSG)
{
LD("SharedMemory::ReadData::GetQuitMessage");
*data = NULL;
m_emptySmph->Unlock();
return false;
}
unsigned int size = m_nSize - UMWEB_SHAREDMEMORY_MSGTYPESIZE;
*data = malloc(size);
memset(*data, 0, size);
memcpy(*data, (unsigned char*)m_pMapView + UMWEB_SHAREDMEMORY_MSGTYPESIZE, size);
//clear
memset(m_pMapView, 0, m_nSize);
m_emptySmph->Unlock();
LD("SharedMemory::ReadData::ReleaseEmptySemaphore");
}
LD("SharedMemory::ReadData::Return=" << bRet);
return bRet;
}
void CSharedMemory::WriteData(MsgType type, const void* data, int size)
{
LD("SharedMemory::WriteData::Enter::Type=" << type);
if (m_pMapView != NULL)
{
m_emptySmph->Lock();
LD("SharedMemory::WriteData::GetEmptySemaphore");
//目前仅一个资源不用加锁 可以考虑指定容器allocator从mapView中申请内存实现多资源
//CAutoMutex _lock(m_mutex.get());
memcpy(m_pMapView, (unsigned char*)&type, UMWEB_SHAREDMEMORY_MSGTYPESIZE);
if(size > 0)
memcpy((unsigned char*)m_pMapView + UMWEB_SHAREDMEMORY_MSGTYPESIZE, (unsigned char*)data, size);
//*(static_cast<T*>((void*)((unsigned char*)m_pMapView + UMWEB_SHAREDMEMORY_MSGTYPESIZE))) = data;
m_fullSmph->Unlock();
LD("SharedMemory::WriteData::ReleaseFullSemaphore");
}
}
}