Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/interprocess/managed_shared_memory.hpp>
- #include <boost/interprocess/containers/vector.hpp>
- #include <boost/interprocess/containers/string.hpp>
- #include <boost/interprocess/allocators/allocator.hpp>
- #include <boost/interprocess/sync/named_mutex.hpp>
- #include <string>
- #include <exception>
- namespace my_shared_memory
- {
- typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator;
- typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> IPCString;
- typedef boost::interprocess::allocator<IPCString, boost::interprocess::managed_shared_memory::segment_manager> StringAllocator;
- typedef boost::interprocess::vector<IPCString, StringAllocator> ShmVector;
- bool write_to_memory(std::string wsuid, std::string loop_val, std::string should_intercept, std::string post_data) ;
- const std::string shm_prefix = "shm_";
- const std::string mutex_prefix = "mtx_";
- }
- #include "shared_memory.h"
- namespace apl_shared_memory
- {
- bool write_to_memory(std::string wsuid, std::string loop_val, std::string should_intercept, std::string post_data)
- {
- bool ret_val;
- std::string shm_name = shm_prefix + wsuid;
- std::string mtx_name = mutex_prefix + wsuid;
- boost::interprocess::named_mutex named_mtx{boost::interprocess::open_or_create, mtx_name.c_str()};
- size_t size = (sizeof(loop_val) + loop_val.size() + sizeof(should_intercept) + should_intercept.size() + sizeof post_data + post_data.size()) * 5;
- try
- {
- named_mtx.lock();
- boost::interprocess::shared_memory_object::remove(shm_name.c_str());
- boost::interprocess::managed_shared_memory segment(boost::interprocess::create_only, shm_name.c_str(), size);
- CharAllocator charallocator (segment.get_segment_manager());
- StringAllocator stringallocator(segment.get_segment_manager());
- IPCString shm_loop_val(charallocator);
- IPCString shm_should_intercept(charallocator);
- IPCString shm_intercepted_data(charallocator);
- shm_loop_val = loop_val.c_str();
- shm_should_intercept = should_intercept.c_str();
- shm_intercepted_data = post_data.c_str();
- segment.destroy<ShmVector>("ShmVector");
- ShmVector *shmVector = segment.construct<ShmVector>("ShmVector")(stringallocator);
- shmVector->clear();
- shmVector->push_back(shm_loop_val);
- shmVector->push_back(shm_should_intercept);
- shmVector->push_back(shm_intercepted_data);
- named_mtx.unlock();
- ret_val = true;
- } catch(const std::exception& ex)
- {
- ret_val = false;
- named_mtx.unlock();
- boost::interprocess::shared_memory_object::remove(shm_name.c_str());
- }
- named_mtx.unlock();
- return ret_val;
- }
- }
- size_t size = (sizeof(loop_val) + loop_val.size() + sizeof(should_intercept) + should_intercept.size() + sizeof post_data + post_data.size()) * 5;
Add Comment
Please, Sign In to add comment