change mq id from int to uuid.
| | |
| | | #include "shm.h" |
| | | #include "bh_util.h" |
| | | #include <mutex> |
| | | #include <boost/uuid/uuid_io.hpp> |
| | | #include <boost/uuid/uuid_generators.hpp> |
| | | |
| | | namespace bhome_shm { |
| | | using namespace boost::interprocess; |
| | | using namespace boost::uuids; |
| | | |
| | | namespace { |
| | | std::string MsgQIdToName(const int id) { return "shmq" + std::to_string(id); } |
| | | } |
| | | |
| | | ShmMsgQueue::ShmMsgQueue(MQId id, ShmType &segment, const std::string &name, const uint32_t len): |
| | | SharedQueue(segment, name, id, len, segment.get_segment_manager()) |
| | | { |
| | | printf("queue size: %ld cap: %ld\n", data()->size(), data()->capacity()); |
| | | std::string MsgQIdToName(const MQId& id) { return "shmq" + to_string(id); } |
| | | MQId EmptyId() { return nil_uuid(); } |
| | | MQId NewId() { return random_generator()(); } |
| | | } |
| | | |
| | | ShmMsgQueue::ShmMsgQueue(MQId id, ShmType &segment, const uint32_t len): |
| | | ShmMsgQueue(id, segment, MsgQIdToName(id), len) |
| | | {} |
| | | SharedQueue(segment, MsgQIdToName(id), id, len, segment.get_segment_manager()) |
| | | { |
| | | printf("queue size: %ld cap: %ld\n", data()->size(), data()->capacity()); |
| | | } |
| | | ShmMsgQueue::ShmMsgQueue(ShmType &segment, const uint32_t len):ShmMsgQueue(NewId(), segment, len) {} |
| | | |
| | | ShmMsgQueue::~ShmMsgQueue() |
| | | { |
| | | |
| | | Remove(); |
| | | } |
| | | |
| | | bool ShmMsgQueue::Send(MQId remote_id, const void *data, const size_t size, const int timeout_ms) |
| | |
| | | void *p = shm().allocate(size, std::nothrow); |
| | | bool r = false; |
| | | if (p) { |
| | | Msg buf = { id(), p, size}; |
| | | Msg buf = { Id(), p, size}; |
| | | memcpy(p, data, size); |
| | | if (remote->Write(buf, timeout_ms)) { |
| | | return true; |
| | |
| | | return true; |
| | | } |
| | | } |
| | | source_id = 0; |
| | | source_id = EmptyId(); |
| | | data = 0; |
| | | size = 0; |
| | | return false; |
| | |
| | | |
| | | #include <boost/interprocess/managed_shared_memory.hpp> |
| | | #include <boost/interprocess/sync/interprocess_condition.hpp> |
| | | #include <boost/interprocess/containers/string.hpp> |
| | | #include <boost/circular_buffer.hpp> |
| | | #include <boost/noncopyable.hpp> |
| | | #include <boost/date_time/posix_time/posix_time.hpp> |
| | | #include <boost/uuid/uuid.hpp> |
| | | |
| | | namespace bhome_shm { |
| | | |
| | |
| | | |
| | | template <class D> using Circular = boost::circular_buffer<D, Allocator<D> >; |
| | | |
| | | typedef int MQId; |
| | | typedef boost::uuids::uuid MQId; |
| | | template <class D> |
| | | class SyncedQueue : private Circular<D> |
| | | { |
| | |
| | | template <class...T> SyncedQueue(MQId id, T&&...t):Super(t...), id_(id) {} |
| | | using Super::size; |
| | | using Super::capacity; |
| | | MQId id() const { return id_; } |
| | | MQId Id() const { return id_; } |
| | | bool Write(D buf, const int timeout_ms) { |
| | | Guard lock(mutex()); |
| | | if (cond_write_.timed_wait(lock, MSFromNow(timeout_ms), [&]() { return !this->full(); })) { |
| | |
| | | size_t size_; |
| | | }; |
| | | |
| | | |
| | | class ShmMsgQueue : private ShmObject<SyncedQueue<Msg> > |
| | | { |
| | | typedef ShmObject<SyncedQueue<Msg> > SharedQueue; |
| | | typedef SharedQueue::Data Queue; |
| | | ShmMsgQueue(MQId id, ShmType &segment, const std::string &name, const uint32_t len); |
| | | bool Write(const Msg &buf, const int timeout_ms) { return data()->Write(buf, timeout_ms); } |
| | | bool Read(Msg &buf, const int timeout_ms) { return data()->Read(buf, timeout_ms); } |
| | | public: |
| | | ShmMsgQueue(MQId id, ShmType &segment, const uint32_t len); |
| | | ShmMsgQueue(ShmType &segment, const uint32_t len); |
| | | ~ShmMsgQueue(); |
| | | bool Send(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); |
| | | using SharedQueue::Remove; |
| | | MQId id() const { return data()->id(); } |
| | | MQId Id() const { return data()->Id(); } |
| | | }; |
| | | |
| | | } // namespace bhome_shm |
| | |
| | | const std::string shm_name("shm_wait"); |
| | | ShmRemover auto_remove(shm_name); |
| | | SharedMemory shm(shm_name, 1024*1024); |
| | | ShmMsgQueue q(10000, shm, 64); |
| | | ShmMsgQueue q(shm, 64); |
| | | for (int i = 0; i < 5; ++i) { |
| | | int ms = i * 100; |
| | | printf("Timeout Test %d: ", ms); |
| | |
| | | |
| | | auto f0 = init_avail; |
| | | const int qlen = 64; |
| | | ShmMsgQueue srv(1, shm, qlen); |
| | | ShmMsgQueue cli(2, shm, qlen); |
| | | ShmMsgQueue srv(shm, qlen); |
| | | ShmMsgQueue cli(shm, qlen); |
| | | auto f1= shm.get_free_memory(); |
| | | |
| | | const size_t msg_length = 1000; |
| | |
| | | |
| | | auto Client = [&](int tid, int nmsg){ |
| | | for (int i = 0; i < nmsg; ++i) { |
| | | if (!cli.Send(srv.id(), msg_content.data(), msg_content.size(), 1000)) { |
| | | if (!cli.Send(srv.Id(), msg_content.data(), msg_content.size(), 1000)) { |
| | | printf("********** client send error.\n"); |
| | | continue; |
| | | } |
| | | MQId id = 0; |
| | | MQId id; |
| | | void *data = 0; |
| | | size_t size = 0; |
| | | if (!cli.Recv(id, data, size, 1000)) { |
| | |
| | | auto Server = [&](){ |
| | | void *data = 0; |
| | | size_t size = 0; |
| | | MQId src_id = 0; |
| | | MQId src_id; |
| | | while (!stop) { |
| | | if (srv.Recv(src_id, data, size, 100)) { |
| | | DEFER1(free(data)); |