github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/common/ipc/shm_service_recv_handler.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 "common/ipc/shm_base.h" 23 #include "common/ipc/shm_service_op_queue.h" 24 #include "common/quitable_thread.h" 25 26 namespace neb { 27 namespace ipc { 28 namespace internal { 29 class shm_service_recv_handler { 30 public: 31 shm_service_recv_handler(boost::interprocess::managed_shared_memory *shmem, 32 shm_service_op_queue *op_queue); 33 34 template <typename T, typename Func> void add_handler(Func &&f) { 35 std::lock_guard<std::mutex> _l(m_handlers_mutex); 36 m_all_handlers.insert(std::make_pair(T::pkg_identifier, [this, f](void *p) { 37 T *r = (T *)p; 38 f(r); 39 std::shared_ptr<shm_service_op_destroy> tp = 40 std::make_shared<shm_service_op_destroy>(m_shmem, r); 41 m_op_queue->push_back(tp); 42 })); 43 } 44 45 void handle_recv_op(const std::shared_ptr<shm_service_op_base> &op); 46 47 protected: 48 typedef std::function<void(void *)> pkg_handler_t; 49 boost::interprocess::managed_shared_memory *m_shmem; 50 std::mutex m_handlers_mutex; 51 std::unordered_map<shm_type_id_t, pkg_handler_t> m_all_handlers; 52 shm_service_op_queue *m_op_queue; 53 wakeable_thread m_handler_thread; 54 }; 55 } // namespace internal 56 } // namespace ipc 57 } // namespace neb