remove length in ShmSocket ctor, not used.
| | |
| | | |
| | | // create sockets. |
| | | try { |
| | | ShmSocket tmp(shm, true, ssn, 16); |
| | | ShmSocket tmp(shm, ssn, eCreate); |
| | | node->addrs_.emplace(ssn, tmp.AbsAddr()); |
| | | return true; |
| | | } catch (...) { |
| | |
| | | auto &node = pos->second; |
| | | try { |
| | | for (int i = 0; i < msg.extra_mq_num(); ++i) { |
| | | ShmSocket tmp(node->shm_, true, head.ssn_id() + i + 1, 16); |
| | | ShmSocket tmp(node->shm_, head.ssn_id() + i + 1, eCreate); |
| | | node->addrs_.emplace(tmp.id(), tmp.AbsAddr()); |
| | | auto addr = reply.add_extra_mqs(); |
| | | addr->set_mq_id(tmp.id()); |
| | |
| | | |
| | | auto InitMQ = [&](auto &mq, auto &&id) { |
| | | mq.id_ = id; |
| | | ShmSocket tmp(shm, id, 16); |
| | | ShmSocket tmp(shm, id, eOpenOrCreate); |
| | | mq.offset_ = tmp.AbsAddr(); |
| | | }; |
| | | |
| | |
| | | template <class D> |
| | | using SharedPtr = shared_ptr<D, Allocator<void>, Deleter<D>>; |
| | | |
| | | enum Mode { |
| | | eOpen = 0, |
| | | eCreate = 1, |
| | | eOpenOrCreate = 2 |
| | | }; |
| | | |
| | | // ShmObject manages an object in shared memory, but ShmObject itself is not in shared memory. |
| | | template <class T> |
| | | class ShmObject : private boost::noncopyable |
| | |
| | | ShmType &shm() const { return shm_; } |
| | | |
| | | template <class... Params> |
| | | ShmObject(ShmType &segment, const std::string &name, Params &&...t) : |
| | | ShmObject(ShmType &segment, const std::string &name, Mode mode, Params &&...t) : |
| | | shm_(segment), name_(name) |
| | | { |
| | | pdata_ = shm_.FindOrCreate<Data>(ObjName(name_), std::forward<decltype(t)>(t)...); |
| | | if (!IsOk()) { |
| | | throw("Error: Not enough memory, can not allocate \"" + name_ + "\""); |
| | | } |
| | | } |
| | | template <class... Params> |
| | | ShmObject(ShmType &segment, const bool create_or_else_find, const std::string &name, Params &&...t) : |
| | | shm_(segment), name_(name) |
| | | { |
| | | if (create_or_else_find) { |
| | | pdata_ = shm_.Create<Data>(ObjName(name_), std::forward<decltype(t)>(t)...); |
| | | } else { |
| | | switch (mode) { |
| | | case eOpen: |
| | | pdata_ = shm_.Find<Data>(ObjName(name_)); |
| | | break; |
| | | case eCreate: |
| | | pdata_ = shm_.Create<Data>(ObjName(name_), std::forward<decltype(t)>(t)...); |
| | | break; |
| | | case eOpenOrCreate: |
| | | pdata_ = shm_.FindOrCreate<Data>(ObjName(name_), std::forward<decltype(t)>(t)...); |
| | | break; |
| | | default: break; |
| | | } |
| | | if (!IsOk()) { |
| | | throw("Error: shm can not create/open \"" + name_ + "\""); |
| | | } |
| | | } |
| | | |
| | | ShmObject(const int64_t offset, ShmType &segment, const std::string &name) : |
| | | shm_(segment), name_(name) |
| | | { |
| | |
| | | |
| | | } // namespace |
| | | |
| | | ShmMsgQueue::ShmMsgQueue(ShmType &segment, const MQId id, const int len) : |
| | | ShmMsgQueue::ShmMsgQueue(ShmType &segment, const MQId id, Mode mode) : |
| | | id_(id), |
| | | queue_(segment, MsgQIdToName(id_), len, segment.get_segment_manager()) |
| | | queue_(segment, MsgQIdToName(id_), mode) |
| | | { |
| | | } |
| | | |
| | | ShmMsgQueue::ShmMsgQueue(ShmType &segment, const bool create_or_else_find, const MQId id, const int len) : |
| | | id_(id), |
| | | queue_(segment, create_or_else_find, MsgQIdToName(id_), len, segment.get_segment_manager()) |
| | | { |
| | | if (!queue_.IsOk()) { |
| | | throw("error create/find msgq " + std::to_string(id_)); |
| | | } |
| | | } |
| | | ShmMsgQueue::ShmMsgQueue(const int64_t abs_addr, ShmType &segment, const MQId id) : |
| | | id_(id), queue_(abs_addr, segment, MsgQIdToName(id_)) |
| | |
| | | typedef Shmq::ShmType ShmType; |
| | | typedef uint64_t MQId; |
| | | |
| | | ShmMsgQueue(ShmType &segment, const MQId id, const int len); |
| | | ShmMsgQueue(ShmType &segment, const bool create_or_else_find, const MQId id, const int len); |
| | | ShmMsgQueue(ShmType &segment, const MQId id, Mode mode); |
| | | ShmMsgQueue(const int64_t abs_addr, ShmType &segment, const MQId id); |
| | | ~ShmMsgQueue(); |
| | | static bool Remove(ShmType &shm, const MQId id); |
| | |
| | | using namespace bhome_msg; |
| | | using namespace bhome_shm; |
| | | |
| | | ShmSocket::ShmSocket(Shm &shm, const MQId id, const int len) : |
| | | run_(false), mq_(shm, id, len), alloc_id_(0), send_buffer_(shm) { Start(); } |
| | | ShmSocket::ShmSocket(Shm &shm, const bool create_or_else_find, const MQId id, const int len) : |
| | | run_(false), mq_(shm, create_or_else_find, id, len), alloc_id_(0), send_buffer_(shm) { Start(); } |
| | | ShmSocket::ShmSocket(int64_t abs_addr, Shm &shm, const MQId id) : |
| | | run_(false), mq_(abs_addr, shm, id), alloc_id_(0), send_buffer_(shm) { Start(); } |
| | | |
| | | ShmSocket::~ShmSocket() { Stop(); } |
| | | |
| | | bool ShmSocket::Start(int nworker, const RecvCB &onData, const RawRecvCB &onRaw, const IdleCB &onIdle) |
| | |
| | | typedef std::function<bool(ShmSocket &sock, MsgI &imsg, BHMsgHead &head)> PartialRecvCB; |
| | | typedef std::function<void(ShmSocket &sock)> IdleCB; |
| | | |
| | | ShmSocket(Shm &shm, const MQId id, const int len); |
| | | ShmSocket(Shm &shm, const bool create_or_else_find, const MQId id, const int len); |
| | | ShmSocket(int64_t offset, Shm &shm, const MQId id); |
| | | ShmSocket(Shm &shm, const MQId id, Mode mode) : |
| | | run_(false), mq_(shm, id, mode), alloc_id_(0), send_buffer_(shm) { Start(); } |
| | | ShmSocket(int64_t abs_addr, Shm &shm, const MQId id) : |
| | | run_(false), mq_(abs_addr, shm, id), alloc_id_(0), send_buffer_(shm) { Start(); } |
| | | |
| | | ~ShmSocket(); |
| | | static bool Remove(SharedMemory &shm, const MQId id) { return Queue::Remove(shm, id); } |
| | | bool Remove() { return Remove(shm(), id()); } |
| | |
| | | int64_t d; |
| | | BOOST_CHECK(tmp.pop(d)); |
| | | |
| | | ShmObject<Rcb> rcb(shm, "test_rcb"); |
| | | ShmObject<Rcb> rcb(shm, "test_rcb", eOpenOrCreate); |
| | | bool try_more = true; |
| | | |
| | | auto Writer = [&]() { |
| | |
| | | typedef ShmObject<s1000> Int; |
| | | std::string name = std::to_string(id); |
| | | auto a0 = Avail(); |
| | | Int i1(shm, name); |
| | | Int i1(shm, name, eOpenOrCreate); |
| | | auto a1 = Avail(); |
| | | BOOST_CHECK_LT(a1, a0); |
| | | printf("s1000 size: %ld\n", a0 - a1); |
| | | i1->a[0] = 5; |
| | | Int i2(shm, name); |
| | | Int i2(shm, name, eOpenOrCreate); |
| | | auto a2 = Avail(); |
| | | BOOST_CHECK_EQUAL(a1, a2); |
| | | BOOST_CHECK_EQUAL(i1.data(), i2.data()); |