From 1fbfef2a51db4a3bac9d8a5b87af94a40a913b7a Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期日, 25 四月 2021 15:33:40 +0800 Subject: [PATCH] change mqid from uuid to uint64. --- src/shm_queue.h | 34 ++++++++++++++++------------------ 1 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/shm_queue.h b/src/shm_queue.h index 93d77df..70039b5 100644 --- a/src/shm_queue.h +++ b/src/shm_queue.h @@ -21,6 +21,7 @@ #include "msg.h" #include "shm.h" +#include <atomic> #include <boost/circular_buffer.hpp> #include <boost/date_time/posix_time/posix_time.hpp> @@ -29,8 +30,6 @@ template <class D> using Circular = boost::circular_buffer<D, Allocator<D>>; - -typedef boost::uuids::uuid MQId; template <class D> class SharedQueue : private Circular<D> @@ -137,32 +136,32 @@ using namespace bhome_msg; -class ShmMsgQueue : private ShmObject<SharedQueue<MsgI>> +class ShmMsgQueue : private ShmObject<SharedQueue<MsgI>>, public StaticDataRef<std::atomic<uint64_t>, ShmMsgQueue> { typedef ShmObject<SharedQueue<MsgI>> Super; typedef Super::Data Queue; typedef std::function<void()> OnSend; - MQId id_; -protected: - ShmMsgQueue(const std::string &raw_name, ShmType &segment, const int len); // internal use. public: - ShmMsgQueue(const MQId &id, ShmType &segment, const int len); + typedef uint64_t MQId; + + static MQId NewId(); + + ShmMsgQueue(const MQId id, ShmType &segment, const int len); ShmMsgQueue(ShmType &segment, const int len); ~ShmMsgQueue(); - static bool Remove(SharedMemory &shm, const MQId &id); - const MQId &Id() const { return id_; } + static bool Remove(SharedMemory &shm, const MQId id); + MQId Id() const { return id_; } using Super::shm; bool Recv(MsgI &msg, const int timeout_ms) { return data()->Read(msg, timeout_ms); } bool TryRecv(MsgI &msg) { return data()->TryRead(msg); } template <class OnData> int TryRecvAll(OnData const &onData) { return data()->TryReadAll(onData); } - static Queue *Find(SharedMemory &shm, const MQId &remote_id); - // static bool Send(SharedMemory &shm, const MQId &remote_id, const MsgI &msg, const int timeout_ms, OnSend const &onsend = OnSend()); - static bool TrySend(SharedMemory &shm, const MQId &remote_id, const MsgI &msg, OnSend const &onsend = OnSend()); + static Queue *Find(SharedMemory &shm, const MQId remote_id); + static bool TrySend(SharedMemory &shm, const MQId remote_id, const MsgI &msg, OnSend const &onsend = OnSend()); template <class Iter> - static int TrySendAll(SharedMemory &shm, const MQId &remote_id, const Iter begin, const Iter end, OnSend const &onsend = OnSend()) + static int TrySendAll(SharedMemory &shm, const MQId remote_id, const Iter begin, const Iter end, OnSend const &onsend = OnSend()) { Queue *remote = Find(shm, remote_id); if (remote) { @@ -177,14 +176,13 @@ } } - // template <class... Rest> - // bool Send(const MQId &remote_id, Rest const &...rest) { return Send(shm(), remote_id, rest...); } template <class... Rest> - bool TrySend(const MQId &remote_id, Rest const &...rest) { return TrySend(shm(), remote_id, rest...); } + bool TrySend(const MQId remote_id, Rest const &...rest) { return TrySend(shm(), remote_id, rest...); } template <class... Rest> - int TrySendAll(const MQId &remote_id, Rest const &...rest) { return TrySendAll(shm(), remote_id, rest...); } + int TrySendAll(const MQId remote_id, Rest const &...rest) { return TrySendAll(shm(), remote_id, rest...); } - size_t Pending() const { return data()->size(); } +private: + MQId id_; }; } // namespace bhome_shm -- Gitblit v1.8.0