github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/common/ipc/shm_base.h (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // (at your option) any later version. 10 // 11 // the go-nebulas library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with the go-nebulas library. If not, see 18 // <http://www.gnu.org/licenses/>. 19 // 20 #pragma once 21 #include "common/common.h" 22 #include <boost/interprocess/allocators/allocator.hpp> 23 #include <boost/interprocess/containers/string.hpp> 24 #include <boost/interprocess/containers/vector.hpp> 25 #include <boost/interprocess/managed_shared_memory.hpp> 26 #include <boost/interprocess/sync/named_condition.hpp> 27 #include <boost/interprocess/sync/named_mutex.hpp> 28 #include <boost/interprocess/sync/named_semaphore.hpp> 29 #include <boost/interprocess/sync/scoped_lock.hpp> 30 #include <boost/process/child.hpp> 31 32 namespace neb { 33 namespace ipc { 34 35 enum shm_role { role_util, role_server, role_client }; 36 struct shm_server { 37 inline static std::string role_name(const std::string &name) { 38 return name + std::string(".server"); 39 } 40 }; 41 struct shm_client { 42 inline static std::string role_name(const std::string &name) { 43 return name + std::string(".client"); 44 } 45 }; 46 typedef uint32_t shm_type_id_t; 47 namespace internal { 48 template <typename T> struct shm_other_side_role {}; 49 template <> struct shm_other_side_role<shm_server> { typedef shm_client type; }; 50 template <> struct shm_other_side_role<shm_client> { typedef shm_server type; }; 51 } 52 53 template <typename A> 54 auto check_exists(const std::string &v) -> typename std::enable_if< 55 std::is_same<A, boost::interprocess::named_mutex>::value || 56 std::is_same<A, boost::interprocess::named_semaphore>::value || 57 std::is_same<A, boost::interprocess::named_condition>::value, 58 bool>::type { 59 try { 60 A _l(boost::interprocess::open_only, v.c_str()); 61 } catch (...) { 62 return false; 63 } 64 return true; 65 } 66 67 struct shm_service_failure : public std::exception { 68 inline shm_service_failure(const std::string &msg) : m_msg(msg) {} 69 inline const char *what() const throw() { return m_msg.c_str(); } 70 71 protected: 72 const std::string m_msg; 73 }; 74 75 struct shm_init_failure : public std::exception { 76 public: 77 inline shm_init_failure(const std::string &msg) : m_msg(msg) {} 78 inline const char *what() const throw() { return m_msg.c_str(); } 79 80 protected: 81 const std::string m_msg; 82 }; 83 84 struct shm_handle_recv_failure : public std::exception { 85 public: 86 inline shm_handle_recv_failure(const std::string &msg) : m_msg(msg) {} 87 inline const char *what() const throw() { return m_msg.c_str(); } 88 89 protected: 90 const std::string m_msg; 91 }; 92 93 typedef boost::interprocess::managed_shared_memory::segment_manager 94 segment_manager_t; 95 typedef boost::interprocess::allocator<void, segment_manager_t> 96 default_allocator_t; 97 98 namespace internal { 99 template <typename T> struct boost_ipc_vector_helper { 100 typedef boost::interprocess::allocator<T, segment_manager_t> elem_allocator_t; 101 typedef boost::interprocess::vector<T, elem_allocator_t> vector_t; 102 }; 103 } // namespace internal 104 typedef boost::interprocess::allocator<char, segment_manager_t> 105 char_allocator_t; 106 typedef boost::interprocess::basic_string<char, std::char_traits<char>, 107 char_allocator_t> 108 char_string_t; 109 110 template <typename T> 111 using vector = typename internal::boost_ipc_vector_helper<T>::vector_t; 112 113 } 114 }