Repository navigation
Expand file tree
/
Copy pathInterprocessComm.cpp
More file actions
123 lines (104 loc) · 3.66 KB
/
Copy pathInterprocessComm.cpp
File metadata and controls
123 lines (104 loc) · 3.66 KB
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
#include "../include/InterprocessComm.h"
#include <iostream>
#include <utility>
InterprocessComm::InterprocessComm(const std::wstring& name, size_t size)
: name(name), size(size), hMapFile(NULL), pSharedMemory(NULL), attached(false) {
if (CreateSharedMemory()) {
// Increment the instance count only when the shared memory is successfully created
SharedMemoryData* sharedData = GetSharedMemoryPointer();
if (sharedData) {
InterlockedIncrement(&sharedData->instanceCount);
attached = true;
}
}
}
InterprocessComm::InterprocessComm(InterprocessComm&& other) noexcept
: name(std::move(other.name)),
size(other.size),
hMapFile(other.hMapFile),
pSharedMemory(other.pSharedMemory),
attached(other.attached) {
other.size = 0;
other.hMapFile = NULL;
other.pSharedMemory = NULL;
other.attached = false;
}
InterprocessComm& InterprocessComm::operator=(InterprocessComm&& other) noexcept {
if (this != &other) {
SharedMemoryData* sharedData = GetSharedMemoryPointer();
if (attached && sharedData) {
InterlockedDecrement(&sharedData->instanceCount);
}
ReleaseSharedMemory();
name = std::move(other.name);
size = other.size;
hMapFile = other.hMapFile;
pSharedMemory = other.pSharedMemory;
attached = other.attached;
other.size = 0;
other.hMapFile = NULL;
other.pSharedMemory = NULL;
other.attached = false;
}
return *this;
}
InterprocessComm::~InterprocessComm() {
// Decrement the instance count only when the instance is destroyed
SharedMemoryData* sharedData = GetSharedMemoryPointer();
if (attached && sharedData) {
LONG newCount = InterlockedDecrement(&sharedData->instanceCount);
// Debug output
std::wcout << L"Decremented instance count to: " << newCount << std::endl;
}
attached = false;
ReleaseSharedMemory();
}
bool InterprocessComm::CreateSharedMemory() {
if (pSharedMemory != NULL) {
return true;
}
hMapFile = CreateFileMappingW(
INVALID_HANDLE_VALUE, // Use paging file
NULL, // Default security
PAGE_READWRITE, // Read/write access
0, // Maximum object size (high-order DWORD)
size, // Maximum object size (low-order DWORD)
name.c_str()); // Name of the mapping object
if (hMapFile == NULL) {
return false;
}
// Check if the memory mapping object already exists or not
bool alreadyExists = (GetLastError() == ERROR_ALREADY_EXISTS);
pSharedMemory = MapViewOfFile(
hMapFile, // Handle to map object
FILE_MAP_ALL_ACCESS, // Read/write permission
0,
0,
size);
if (pSharedMemory == NULL) {
CloseHandle(hMapFile);
hMapFile = NULL;
return false;
}
// If this is a new shared memory (not previously existing), initialize it
if (!alreadyExists) {
ZeroMemory(pSharedMemory, size); // Initialize the shared memory to zero
}
return true;
}
void InterprocessComm::ReleaseSharedMemory() {
if (pSharedMemory != NULL) {
UnmapViewOfFile(pSharedMemory);
pSharedMemory = NULL;
}
if (hMapFile != NULL) {
CloseHandle(hMapFile);
hMapFile = NULL;
}
}
bool InterprocessComm::IsValid() const {
return pSharedMemory != NULL;
}
SharedMemoryData* InterprocessComm::GetSharedMemoryPointer() const {
return reinterpret_cast<SharedMemoryData*>(pSharedMemory); // Return the pointer to the shared memory as SharedMemoryData
}