lichao
2021-06-03 8967e7f2f8b94dc032135707e16c8a9f233d0db6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 * =====================================================================================
 *
 *       Filename:  shm_msg_queue.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年04月26日 16时25分21秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Chao (), lichao@aiotlink.com
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef SHM_MSG_QUEUE_D847TQXH
#define SHM_MSG_QUEUE_D847TQXH
 
#include "defs.h"
#include "msg.h"
#include "shm_queue.h"
 
using namespace bhome_shm;
using namespace bhome_msg;
 
class ShmMsgQueue
{
public:
    typedef int64_t RawData;
    typedef NamedShmObject<SharedQ63> Shmq;
    typedef Shmq::Data Queue;
    typedef Shmq::ShmType ShmType;
    typedef uint64_t MQId;
 
    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);
    MQId Id() const { return id_; }
    ShmType &shm() const { return queue_.shm(); }
    int64_t AbsAddr() const { return queue_.offset(); }
 
    // bool Recv(RawData &val, const int timeout_ms) { return queue().Read(val, timeout_ms); }
    bool TryRecv(RawData &val) { return queue().TryRead(val); }
 
    // bool Recv(MsgI &msg, const int timeout_ms) { return Recv(msg.OffsetRef(), timeout_ms); }
    bool TryRecv(MsgI &msg) { return TryRecv(msg.OffsetRef()); }
    static Queue *Find(ShmType &shm, const MQId remote);
    static bool TrySend(ShmType &shm, const MQInfo &remote, const RawData val);
    bool TrySend(const MQInfo &remote, const RawData val) { return TrySend(shm(), remote, val); }
 
private:
    MQId id_;
    Queue &queue() { return *queue_.data(); }
    Shmq queue_;
};
 
#endif // end of include guard: SHM_MSG_QUEUE_D847TQXH