This repository was archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathipc.cpp
415 lines (345 loc) · 12.8 KB
/
ipc.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* OSPE - Open Source Packet Editor
* Copyright(C) 2018-2019 Javier Pereda <https://github.com/elecyb>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "StdAfx.h"
#include "ipc.h"
osIPC::Server::Server(void)
{
// Set default params
m_hMapFile = 0;
m_hSignal = 0;
m_hAvail = 0;
m_pNodeHeader = NULL;
m_sAddr = NULL;
// create the server
create();
};
osIPC::Server::~Server(void)
{
// Free memory
if (m_sAddr) {
free(m_sAddr);
m_sAddr = NULL;
}
// Close the server
close();
};
void osIPC::Server::create(void)
{
// Determine the name of the memory
DWORD ProcessID = GetCurrentProcessId();
m_sAddr = (char*)malloc(IPC_MAX_NAME);
if (!m_sAddr) return;
sprintf_s(m_sAddr, IPC_MAX_NAME, "%s%u", IPC_SRV_MMF_NAME, ProcessID);
char *m_sEvtAvail = (char*)malloc(IPC_MAX_NAME);
if (!m_sEvtAvail) { printf("server_create: failed: %01d\n", __LINE__); return; }
sprintf_s(m_sEvtAvail, IPC_MAX_NAME, "%s_evt_nodeavail", m_sAddr);
char *m_sEvtFilled = (char*)malloc(IPC_MAX_NAME);
if (!m_sEvtFilled) { free(m_sEvtAvail); printf("server_create: failed: %01d\n", __LINE__); return; }
sprintf_s(m_sEvtFilled, IPC_MAX_NAME, "%s_evt_dataexists", m_sAddr);
char *m_sMemName = (char*)malloc(IPC_MAX_NAME);
if (!m_sMemName) { free(m_sEvtAvail); free(m_sEvtFilled); printf("server_create: failed: %01d\n", __LINE__); return; }
sprintf_s(m_sMemName, IPC_MAX_NAME, "%s", m_sAddr);
// Create the events
m_hSignal = CreateEventA(NULL, FALSE, FALSE, (LPCSTR)m_sEvtFilled);
if (m_hSignal == NULL || m_hSignal == INVALID_HANDLE_VALUE) { free(m_sEvtAvail); free(m_sEvtFilled); free(m_sMemName); printf("server_create: failed: %01d\n", __LINE__); return; }
m_hAvail = CreateEventA(NULL, FALSE, FALSE, (LPCSTR)m_sEvtAvail);
if (m_hAvail == NULL || m_hSignal == INVALID_HANDLE_VALUE) { free(m_sEvtAvail); free(m_sEvtFilled); free(m_sMemName); printf("server_create: failed: %01d\n", __LINE__); return; }
long _sizeHeader = 16; // Hardcodeado debido a los arrays
long _sizeNodeHeader = 24; // Hardcodeado debido a los arrays
long _sizeNode = sizeof(Node);
// Initialise Header
DWORD _sharedMemorySize = _sizeHeader
+ _sizeNodeHeader
+ IPC_BLOCK_COUNT * _sizeNode
+ IPC_BLOCK_COUNT * IPC_BLOCK_SIZE;
// Create the file mapping
m_hMapFile = CreateFileMappingA(INVALID_HANDLE_VALUE, // FileHandle = new IntPtr(-1)
NULL, // SECURITY_ATTRIBUTES
PAGE_READWRITE, // GetPageAccess(access = PAGE_READWRITE = 0x04;) | (int)options = MemoryMappedFileOptions.None
0,
_sharedMemorySize,
(LPCSTR)m_sMemName);
if (m_hMapFile == NULL || m_hMapFile == INVALID_HANDLE_VALUE) { free(m_sEvtAvail); free(m_sEvtFilled); free(m_sMemName); printf("server_create: failed: %01d\n", __LINE__); return; }
// Map to the file
Header * m_pHeader = (Header*)MapViewOfFile(m_hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
_sharedMemorySize);
if (m_pHeader == NULL) { free(m_sEvtAvail); free(m_sEvtFilled); free(m_sMemName); printf("server_create: failed: %01d\n", __LINE__); return; }
// Initialise Header
m_pHeader->SharedMemorySize = _sharedMemorySize;
m_pHeader->Shutdown = 0;
// Get NodeHeader pointer
m_pNodeHeader = &m_pHeader->nodeHeader;
// Clear the buffer
ZeroMemory(m_pNodeHeader, _sizeNodeHeader);
// Initialize the pointers InitialiseNodeHeader()
m_pNodeHeader->m_ReadEnd = 0;
m_pNodeHeader->m_ReadStart = 0;
m_pNodeHeader->m_WriteEnd = 0;
m_pNodeHeader->m_WriteStart = 0;
m_pNodeHeader->nodeCount = IPC_BLOCK_COUNT;
m_pNodeHeader->NodeBufferSize = IPC_BLOCK_SIZE;
long NodeHeaderOffset = 0; // -_-
long NodeOffset = NodeHeaderOffset + _sizeNodeHeader;
long NodeBufferOffset = NodeOffset + (_sizeNode * IPC_BLOCK_COUNT);
// Create a circular linked list
int N = 0;
int NodeCount = IPC_BLOCK_COUNT;
// First Node
m_pNodeHeader->m_Nodes[N].Next = 1;
m_pNodeHeader->m_Nodes[N].Prev = (NodeCount - 1);
m_pNodeHeader->m_Nodes[N].Offset = NodeBufferOffset;
m_pNodeHeader->m_Nodes[N].Index = N;
m_pNodeHeader->m_Nodes[N].AmountWritten = 0;
// Middle Nodes
for (N = 1; N < NodeCount - 1; N++)
{
// Add this block into the available list
m_pNodeHeader->m_Nodes[N].Next = (N + 1);
m_pNodeHeader->m_Nodes[N].Prev = (N - 1);
m_pNodeHeader->m_Nodes[N].Offset = NodeBufferOffset + (m_pNodeHeader->NodeBufferSize * N);
m_pNodeHeader->m_Nodes[N].Index = N;
m_pNodeHeader->m_Nodes[N].AmountWritten = 0;
}
// Last Node
m_pNodeHeader->m_Nodes[N].Next = 0;
m_pNodeHeader->m_Nodes[N].Prev = (NodeCount - 2);
m_pNodeHeader->m_Nodes[N].Offset = NodeBufferOffset + (m_pNodeHeader->NodeBufferSize * N);
m_pNodeHeader->m_Nodes[N].Index = N;
m_pNodeHeader->m_Nodes[N].AmountWritten = 0;
// Release memory
free(m_sEvtAvail);
free(m_sEvtFilled);
free(m_sMemName);
};
void osIPC::Server::close(void)
{
// Close the event
if (m_hSignal) {
HANDLE handle = m_hSignal;
m_hSignal = NULL;
CloseHandle(handle);
}
// Close the event
if (m_hAvail) {
HANDLE handle = m_hAvail;
m_hAvail = NULL;
CloseHandle(handle);
}
// Unmap the memory
if (m_pNodeHeader) {
NodeHeader *pBuff = m_pNodeHeader;
m_pNodeHeader = NULL;
UnmapViewOfFile(pBuff);
}
// Close the file handle
if (m_hMapFile) {
HANDLE handle = m_hMapFile;
m_hMapFile = NULL;
CloseHandle(handle);
}
};
osIPC::Node* osIPC::Server::GetNodeForReading(DWORD dwTimeout)
{
// Grab another block to read from
// Enter a continous loop (this is to make sure the operation is atomic)
for (;;)
{
// Check if there is room to expand the read start cursor
LONG blockIndex = m_pNodeHeader->m_ReadStart;
Node *pNode = m_pNodeHeader->m_Nodes + blockIndex;
if (blockIndex == m_pNodeHeader->m_WriteEnd)
{
// No room is available, wait for room to become available
if (WaitForSingleObject(m_hSignal, dwTimeout) == WAIT_OBJECT_0)
continue;
// Timeout
return NULL;
}
// Make sure the operation is atomic
if (InterlockedCompareExchange(&m_pNodeHeader->m_ReadStart, pNode->Next, blockIndex) == blockIndex)
return pNode;
// The operation was interrupted by another thread.
// The other thread has already stolen this block, try again
continue;
}
};
void osIPC::Server::ReturnNode(osIPC::Node* pNode)
{
// Set the done flag for this block
pNode->doneRead = 1;
// Move the read pointer as far forward as we can
for (;;)
{
// Try and get the right to move the poitner
DWORD blockIndex = m_pNodeHeader->m_ReadEnd;
pNode = m_pNodeHeader->m_Nodes + blockIndex;
if (InterlockedCompareExchange(&pNode->doneRead, 0, 1) != 1)
{
// If we get here then another thread has already moved the pointer
// for us or we have reached as far as we can possible move the pointer
return;
}
// Move the pointer one forward (interlock protected)
InterlockedCompareExchange(&m_pNodeHeader->m_ReadEnd, pNode->Next, blockIndex);
// Signal availability of more data but only if a thread is waiting
if (pNode->Prev == m_pNodeHeader->m_WriteStart)
SetEvent(m_hAvail);
}
};
DWORD osIPC::Server::read(void *pBuff, DWORD buffSize, DWORD dwTimeout)
{
// Grab a block
Node *pNode = GetNodeForReading(dwTimeout);
if (!pNode) return 0;
// Copy the data
DWORD dwAmount = min(m_pNodeHeader->NodeBufferSize, buffSize);
memcpy(pBuff, m_pNodeHeader->Data[pNode->Index], dwAmount);
// Return the block
ReturnNode(pNode);
// Success
return dwAmount;
};
osIPC::Client::Client(void)
{
// Set default params
m_hMapFile = 0;
m_hSignal = 0;
m_hAvail = 0;
m_pNodeHeader = NULL;
};
osIPC::Client::Client(char * smName)
{
// Set default params
m_hMapFile = 0;
m_hSignal = 0;
m_hAvail = 0;
m_pNodeHeader = NULL;
// Determine the name of the memory
m_sAddr = (char*)malloc(IPC_MAX_NAME);
if (!m_sAddr) return;
strcpy_s(m_sAddr, IPC_MAX_NAME, smName);
char *m_sEvtAvail = (char*)malloc(IPC_MAX_NAME);
if (!m_sEvtAvail) return;
sprintf_s(m_sEvtAvail, IPC_MAX_NAME, "%s_evt_nodeavail", m_sAddr);
char *m_sEvtFilled = (char*)malloc(IPC_MAX_NAME);
if (!m_sEvtFilled) { free(m_sEvtAvail); return; }
sprintf_s(m_sEvtFilled, IPC_MAX_NAME, "%s_evt_dataexists", m_sAddr);
char *m_sMemName = (char*)malloc(IPC_MAX_NAME);
if (!m_sMemName) { free(m_sEvtAvail); free(m_sEvtFilled); printf("server_create: failed: %01d\n", __LINE__); return; }
sprintf_s(m_sMemName, IPC_MAX_NAME, "%s", m_sAddr);
// Create the events
m_hSignal = CreateEventA(NULL, FALSE, FALSE, (LPCSTR)m_sEvtFilled);
if (m_hSignal == NULL || m_hSignal == INVALID_HANDLE_VALUE) { free(m_sEvtAvail); free(m_sEvtFilled); free(m_sMemName); return; }
m_hAvail = CreateEventA(NULL, FALSE, FALSE, (LPCSTR)m_sEvtAvail);
if (m_hAvail == NULL || m_hSignal == INVALID_HANDLE_VALUE) { free(m_sEvtAvail); free(m_sEvtFilled); free(m_sMemName); return; }
// Open the shared file
m_hMapFile = OpenFileMappingA( FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
(LPCSTR)m_sMemName); // name of mapping object
if (m_hMapFile == NULL || m_hMapFile == INVALID_HANDLE_VALUE) { free(m_sEvtAvail); free(m_sEvtFilled); free(m_sMemName); return; }
// Map to the file
Header * m_pHeader = (Header*)MapViewOfFile(m_hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
sizeof(Header));
if (m_pHeader == NULL) { free(m_sEvtAvail); free(m_sEvtFilled); free(m_sMemName); return; }
m_pNodeHeader = &m_pHeader->nodeHeader;
// Release memory
free(m_sEvtAvail);
free(m_sEvtFilled);
free(m_sMemName);
};
osIPC::Client::~Client(void)
{
// Close the event
CloseHandle(m_hSignal);
// Close the event
CloseHandle(m_hAvail);
// Unmap the memory
UnmapViewOfFile(m_pNodeHeader);
// Close the file handle
CloseHandle(m_hMapFile);
};
osIPC::Node* osIPC::Client::GetNodeForWriting(DWORD dwTimeout)
{
// Grab another block to write too
// Enter a continous loop (this is to make sure the operation is atomic)
for (;;)
{
// Check if there is room to expand the write start cursor
LONG blockIndex = m_pNodeHeader->m_WriteStart;
Node *pNode = m_pNodeHeader->m_Nodes + blockIndex;
if (pNode->Next == m_pNodeHeader->m_ReadEnd)
{
// No room is available, wait for room to become available
if (WaitForSingleObject(m_hAvail, dwTimeout) == WAIT_OBJECT_0)
continue;
// Timeout
return NULL;
}
// Make sure the operation is atomic
if (InterlockedCompareExchange(&m_pNodeHeader->m_WriteStart, pNode->Next, blockIndex) == blockIndex)
return pNode;
// The operation was interrupted by another thread.
// The other thread has already stolen this block, try again
continue;
}
};
void osIPC::Client::PostNode(Node *pNode)
{
// Set the done flag for this block
pNode->doneWrite = 1;
// Move the write pointer as far forward as we can
// always starting from WriteEnd to make all contiguous
// completed nodes available for reading.
for (;;)
{
// Try and get the right to move the poitner
DWORD blockIndex = m_pNodeHeader->m_WriteEnd;
pNode = m_pNodeHeader->m_Nodes + blockIndex;
if (InterlockedCompareExchange(&pNode->doneWrite, 0, 1) != 1)
{
// If we get here then another thread has already moved the pointer
// for us or we have reached as far as we can possible move the pointer
return;
}
// Move the pointer one forward (interlock protected)
InterlockedCompareExchange(&m_pNodeHeader->m_WriteEnd, pNode->Next, blockIndex);
// Signal availability of more data but only if threads are waiting
if (blockIndex == m_pNodeHeader->m_ReadStart)
SetEvent(m_hSignal);
}
};
DWORD osIPC::Client::write(void *pBuff, DWORD amount, DWORD dwTimeout)
{
// Grab a block
Node *pNode = GetNodeForWriting(dwTimeout);
if (!pNode) return 0;
// Copy the data
DWORD dwAmount = min(amount, IPC_BLOCK_SIZE);
memcpy(m_pNodeHeader->Data[pNode->Index], pBuff, dwAmount);
pNode->AmountWritten = dwAmount;
// Post the block
PostNode(pNode);
// Success
return dwAmount;
};