SHARE
TWEET

Untitled

a guest Jan 18th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <boost/interprocess/managed_shared_memory.hpp>
  2. #include <boost/interprocess/containers/vector.hpp>
  3. #include <boost/interprocess/containers/string.hpp>
  4.  
  5. #include <boost/interprocess/allocators/allocator.hpp>
  6. #include <boost/interprocess/sync/named_mutex.hpp>
  7.  
  8. #include <string>
  9. #include <exception>
  10.  
  11. namespace my_shared_memory
  12. {
  13.   typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator;
  14.   typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> IPCString;
  15.   typedef boost::interprocess::allocator<IPCString, boost::interprocess::managed_shared_memory::segment_manager> StringAllocator;
  16.   typedef boost::interprocess::vector<IPCString, StringAllocator> ShmVector;
  17.  
  18.   bool write_to_memory(std::string wsuid, std::string loop_val, std::string should_intercept, std::string post_data) ;
  19.  
  20.   const std::string shm_prefix = "shm_";
  21.   const std::string mutex_prefix = "mtx_";
  22. }
  23.    
  24. #include "shared_memory.h"
  25.  
  26. namespace apl_shared_memory
  27. {
  28.  
  29.   bool write_to_memory(std::string wsuid, std::string loop_val, std::string should_intercept, std::string post_data)
  30.   {
  31.     bool ret_val;
  32.     std::string shm_name = shm_prefix + wsuid;
  33.     std::string mtx_name = mutex_prefix + wsuid;
  34.     boost::interprocess::named_mutex named_mtx{boost::interprocess::open_or_create, mtx_name.c_str()};
  35.     size_t size = (sizeof(loop_val) + loop_val.size() + sizeof(should_intercept) + should_intercept.size() + sizeof post_data + post_data.size()) * 5;
  36.  
  37.     try
  38.     {
  39.       named_mtx.lock();
  40.       boost::interprocess::shared_memory_object::remove(shm_name.c_str());
  41.       boost::interprocess::managed_shared_memory segment(boost::interprocess::create_only, shm_name.c_str(), size);
  42.       CharAllocator     charallocator  (segment.get_segment_manager());
  43.       StringAllocator   stringallocator(segment.get_segment_manager());
  44.  
  45.       IPCString shm_loop_val(charallocator);
  46.       IPCString shm_should_intercept(charallocator);
  47.       IPCString shm_intercepted_data(charallocator);
  48.       shm_loop_val = loop_val.c_str();
  49.       shm_should_intercept = should_intercept.c_str();
  50.       shm_intercepted_data = post_data.c_str();
  51.  
  52.       segment.destroy<ShmVector>("ShmVector");
  53.       ShmVector *shmVector = segment.construct<ShmVector>("ShmVector")(stringallocator);
  54.       shmVector->clear();
  55.       shmVector->push_back(shm_loop_val);
  56.       shmVector->push_back(shm_should_intercept);
  57.       shmVector->push_back(shm_intercepted_data);
  58.  
  59.       named_mtx.unlock();
  60.       ret_val = true;
  61.     } catch(const std::exception& ex)
  62.     {
  63.       ret_val = false;
  64.       named_mtx.unlock();
  65.       boost::interprocess::shared_memory_object::remove(shm_name.c_str());
  66.     }
  67.  
  68.     named_mtx.unlock();
  69.     return ret_val;
  70.   }
  71.  
  72. }
  73.    
  74. size_t size = (sizeof(loop_val) + loop_val.size() + sizeof(should_intercept) + should_intercept.size() + sizeof post_data + post_data.size()) * 5;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top