| | |
| | | *static_cast<MsgMetaV1*>(p) = *this; |
| | | } |
| | | |
| | | bool Msg::Build(SharedMemory &shm, const MQId &src_id, const void *data, const size_t size, const bool refcount) |
| | | { |
| | | if (!data || !size) { |
| | | return false; |
| | | } |
| | | void *p = shm.Alloc(sizeof(MsgMetaV1) + size); |
| | | if (!p) { |
| | | return false; |
| | | } |
| | | RefCount *rc = 0; |
| | | if (refcount) { |
| | | rc = shm.New<RefCount>(); |
| | | if (!rc) { |
| | | shm.Dealloc(p); |
| | | return false; |
| | | } |
| | | } |
| | | MsgMetaV1 meta; |
| | | meta.data_size_ = size; |
| | | meta.src_id_ = src_id; |
| | | meta.Pack(p); |
| | | memcpy(static_cast<char *>(p) + sizeof(meta), data, size); |
| | | Msg(p, rc).swap(*this); |
| | | return true; |
| | | |
| | | } |
| | | |
| | | void Msg::FreeFrom(SharedMemory &shm) |
| | | { |
| | | shm.Dealloc(ptr_); |
| | | shm.Delete(count_); |
| | | } |
| | | } // namespace bhome_shm |
| | |
| | | #include <stdint.h> |
| | | #include "shm.h" |
| | | #include <boost/interprocess/offset_ptr.hpp> |
| | | #include <boost/uuid/uuid_generators.hpp> |
| | | |
| | | namespace bhome_shm { |
| | | |
| | |
| | | kMsgTypeMaxValue |
| | | }; |
| | | |
| | | typedef boost::uuids::uuid MQId; |
| | | |
| | | const uint32_t kMsgMetaTag = 0xf1e2d3c4; |
| | | |
| | | struct MsgMetaV1 { |
| | |
| | | uint16_t type_ = kMsgTypeNormal; // msg type. |
| | | uint32_t tag_ = kMsgMetaTag; |
| | | uint32_t data_size_ = 0; |
| | | unsigned char src_id_[16] = {0}; |
| | | MQId src_id_; |
| | | // more fields add at end, must not change |
| | | |
| | | MsgMetaV1(){} |
| | | MsgMetaV1():src_id_(boost::uuids::nil_uuid()){} |
| | | bool Parse(const void *p); |
| | | void Pack(void *p); |
| | | }; |
| | |
| | | private: |
| | | offset_ptr<void> ptr_; |
| | | offset_ptr<RefCount> count_; |
| | | void swap(Msg &a) { std::swap(ptr_, a.ptr_); std::swap(count_, a.count_); } |
| | | public: |
| | | class CountGuard : private boost::noncopyable { |
| | | Msg &msg_; |
| | |
| | | }; |
| | | |
| | | Msg(void *p=0, RefCount *c=0):ptr_(p), count_(c) {} |
| | | void swap(Msg &a) { std::swap(ptr_, a.ptr_); std::swap(count_, a.count_); } |
| | | |
| | | // ~Msg() { RemoveRef(); } |
| | | // Msg(const Msg &a):ptr_(a.ptr_), count_(a.count_) { AddRef(); } |
| | |
| | | // Msg & operator = (Msg &&a) { Msg(std::move(a)).swap(*this); } |
| | | |
| | | template <class T = void> T *get() { return static_cast<T*>(ptr_.get()); } |
| | | int AddRef() { return count_ ? count_->Inc() : 0; } |
| | | int RemoveRef() { return count_ ? count_->Dec() : 0; } |
| | | int Count() { return count_ ? count_->Get() : 0; } |
| | | int AddRef() const { return count_ ? count_->Inc() : 0; } |
| | | int RemoveRef() const{ return count_ ? count_->Dec() : 0; } |
| | | int Count() const { return count_ ? count_->Get() : 0; } |
| | | bool Build(SharedMemory &shm, const MQId &src_id, const void *p, const size_t size, const bool refcount); |
| | | void FreeFrom(SharedMemory &shm); |
| | | }; |
| | | |
| | | Msg BuildMsg(const MQId &src, const void *p, const size_t size); |
| | | |
| | | |
| | | } // namespace bhome_shm |
| | | |
| | |
| | | {} |
| | | std::string name() const { return name_; } |
| | | bool Remove() { return Remove(name()); } |
| | | |
| | | void *Alloc(const size_t size) { return allocate(size, std::nothrow); } |
| | | void Dealloc(void *p) { if(p) { deallocate(p); } } |
| | | template<class T> void Dealloc(offset_ptr<T> ptr) { return Dealloc(ptr.get()); } |
| | | |
| | | template <class T, class ...Params> T * New(Params const&...params) { return construct<T>(anonymous_instance, std::nothrow)(params...); } |
| | | template <class T> void Delete(T *p) { if (p) { destroy_ptr<T>(p); }; } |
| | | template <class T> void Delete(offset_ptr<T> p) { Delete(p.get()); } |
| | |
| | | |
| | | bool IsOk() const { return pdata_; } |
| | | protected: |
| | | ShmType &shm() { return shm_; } |
| | | ShmType &shm() const { return shm_; } |
| | | public: |
| | | template <class...Params> |
| | | ShmObject(ShmType &segment, const std::string &name, Params&&...t): |
| | |
| | | Remove(); |
| | | } |
| | | |
| | | bool ShmMsgQueue::Send(const MQId &remote_id, const Msg &msg, const int timeout_ms) |
| | | { |
| | | Queue *remote = find(MsgQIdToName(remote_id)); |
| | | |
| | | return remote && remote->Write(msg, timeout_ms); |
| | | |
| | | if(!remote) { |
| | | return false; |
| | | } |
| | | msg.AddRef(); |
| | | if (remote->Write(msg, timeout_ms)) { |
| | | return true; |
| | | } else { |
| | | msg.RemoveRef(); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | bool ShmMsgQueue::Send(const MQId &remote_id, const void *data, const size_t size, const int timeout_ms) |
| | | { |
| | | if (data && size) { |
| | | Queue *remote = find(MsgQIdToName(remote_id)); |
| | | if (remote) { |
| | | void *p = shm().allocate(sizeof(MsgMetaV1) + size, std::nothrow); |
| | | bool r = false; |
| | | if (p) { |
| | | MsgMetaV1 meta; |
| | | meta.data_size_ = size; |
| | | memcpy(meta.src_id_, &Id(), sizeof(MQId)); |
| | | meta.Pack(p); |
| | | |
| | | memcpy(static_cast<char*>(p) + sizeof(meta), data, size); |
| | | if (remote->Write(p, timeout_ms)) { |
| | | return true; |
| | | } else { |
| | | shm().deallocate(p); |
| | | } |
| | | } |
| | | Msg msg; |
| | | if (msg.Build(shm(), Id(), data, size, false)) { |
| | | if (Send(remote_id, msg, timeout_ms)) { |
| | | return true; |
| | | } else { |
| | | msg.FreeFrom(shm()); |
| | | } |
| | | } |
| | | return false; |
| | |
| | | if (Read(msg, timeout_ms)) { |
| | | auto ptr = msg.get<char>(); |
| | | if (ptr) { |
| | | DEFER1(shm().deallocate(ptr);); |
| | | DEFER1(shm().Dealloc(ptr);); |
| | | MsgMetaV1 meta; |
| | | meta.Parse(ptr); |
| | | memcpy(&source_id, meta.src_id_, sizeof(MQId)); |
| | | source_id = meta.src_id_; |
| | | size = meta.data_size_; |
| | | data = malloc(size); |
| | | if (data) { |
| | |
| | | bool Send(const MQId &remote_id, const void *data, const size_t size, const int timeout_ms); |
| | | bool Recv(MQId &source_id, void *&data, size_t &size, const int timeout_ms); |
| | | const MQId &Id() const { return data()->Id(); } |
| | | bool Send(const MQId &remote_id, const Msg &msg, const int timeout_ms); |
| | | }; |
| | | |
| | | } // namespace bhome_shm |
| | |
| | | ShmRemover auto_remove(shm_name); |
| | | SharedMemory shm(shm_name, 1024*1024*10); |
| | | |
| | | offset_ptr<const void> p; |
| | | BOOST_CHECK(!p); |
| | | BOOST_CHECK(p.get() == 0); |
| | | p = 0; |
| | | BOOST_CHECK(!p); |
| | | BOOST_CHECK(p.get() == 0); |
| | | const char *str = "basic"; |
| | | p = str; |
| | | BOOST_CHECK(p); |
| | | BOOST_CHECK(p.get() == str); |
| | | p = 0; |
| | | BOOST_CHECK(!p); |
| | | BOOST_CHECK(p.get() == 0); |
| | | |
| | | |
| | | auto Avail = [&]() { return shm.get_free_memory(); }; |
| | | auto init_avail = Avail(); |
| | | |
| | |
| | | |
| | | { |
| | | auto old = Avail(); |
| | | void *p = shm.allocate(1024); |
| | | shm.deallocate(p); |
| | | void *p = shm.Alloc(1024); |
| | | shm.Dealloc(p); |
| | | BOOST_CHECK_EQUAL(old, Avail()); |
| | | } |
| | | |
| | |
| | | ShmRemover auto_remove(shm_name); |
| | | SharedMemory shm(shm_name, 1024*1024); |
| | | |
| | | Msg m0(shm.allocate(1000), shm.New<RefCount>()); |
| | | Msg m0(shm.Alloc(1000), shm.New<RefCount>()); |
| | | BOOST_CHECK_EQUAL(m0.AddRef(), 1); |
| | | Msg m1 = m0; |
| | | BOOST_CHECK_EQUAL(m1.AddRef(), 2); |
| | |
| | | BOOST_CHECK_EQUAL(head.type_, kMsgTypeNormal); |
| | | BOOST_CHECK_EQUAL(head.tag_, kMsgMetaTag); |
| | | BOOST_CHECK_EQUAL(head.data_size_, 0); |
| | | BOOST_CHECK_EQUAL(head.src_id_[5], 0); |
| | | BOOST_CHECK(head.src_id_ == boost::uuids::nil_uuid()); |
| | | |
| | | head.data_size_ = 100; |
| | | auto rand_id = boost::uuids::random_generator()(); |
| | | memcpy(head.src_id_, &rand_id, sizeof(rand_id)); |
| | | head.src_id_ = boost::uuids::random_generator()(); |
| | | head.type_ = 123; |
| | | |
| | | BOOST_CHECK_EQUAL(sizeof(head.src_id_), sizeof(rand_id)); |
| | | |
| | | char buf[100] = {0}; |
| | | head.Pack(buf); |