-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.h
50 lines (40 loc) · 1.21 KB
/
Client.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
#include <math.h>
#define BOOST_ALL_NO_LIB
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace boost::interprocess;
#define SHAREDLENGTH (1024*1024) // 1mb of shared memory
// This structure is extremely brittle
struct sharedPt
{
enum { dataLength = SHAREDLENGTH};
interprocess_mutex mutex;
uint8_t data[dataLength];
};
shared_memory_object shm;
mapped_region region;
void getSharedData(char* name, uint8_t *data, int ofs, int sz)
{
try
{
//Open the shared memory object.
shared_memory_object shm(open_only ,name, read_write);
//Map the whole shared memory in this process
mapped_region region(shm ,read_write);
//Get the address of the mapped region
void * addr = region.get_address();
//Construct the shared structure in memory
sharedPt * spt_ = static_cast<sharedPt*>(addr);
//scoped_lock<interprocess_mutex> lock(sIm->mutex);
memcpy(data, (char*)spt_->data + ofs, sz);
}
catch (interprocess_exception &ex)
{
printf("getFrame:%s\n", ex.what());
}
}