Interprocess.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #pragma once
  2. #include <boost/interprocess/sync/scoped_lock.hpp>
  3. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  4. #include <boost/interprocess/sync/interprocess_condition.hpp>
  5. #include <boost/interprocess/mapped_region.hpp>
  6. #include <boost/interprocess/shared_memory_object.hpp>
  7. /*
  8. * Interprocess.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. struct ServerReady
  17. {
  18. bool ready;
  19. uint16_t port; //ui16?
  20. boost::interprocess::interprocess_mutex mutex;
  21. boost::interprocess::interprocess_condition cond;
  22. ServerReady()
  23. {
  24. ready = false;
  25. port = 0;
  26. }
  27. void setToTrueAndNotify(uint16_t Port)
  28. {
  29. {
  30. boost::unique_lock<boost::interprocess::interprocess_mutex> lock(mutex);
  31. ready = true;
  32. port = Port;
  33. }
  34. cond.notify_all();
  35. }
  36. };
  37. struct SharedMem
  38. {
  39. boost::interprocess::shared_memory_object smo;
  40. boost::interprocess::mapped_region *mr;
  41. ServerReady *sr;
  42. SharedMem() //c-tor
  43. :smo(boost::interprocess::open_or_create,"vcmi_memory",boost::interprocess::read_write)
  44. {
  45. smo.truncate(sizeof(ServerReady));
  46. mr = new boost::interprocess::mapped_region(smo,boost::interprocess::read_write);
  47. sr = new(mr->get_address())ServerReady();
  48. };
  49. ~SharedMem() //d-tor
  50. {
  51. delete mr;
  52. boost::interprocess::shared_memory_object::remove("vcmi_memory");
  53. }
  54. };