lichao
2021-03-31 3c2b6739208d961cf8b86460d7f05516d044960c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * =====================================================================================
 *
 *       Filename:  msg.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年03月24日 16时48分42秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Chao (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#include "msg.h"
#include "bh_util.h"
 
namespace bhome_msg
{
 
const uint32_t kMsgTag = 0xf1e2d3c4;
const uint32_t kMsgPrefixLen = 4;
 
BHMsg InitMsg(MsgType type)
{
    BHMsg msg;
    msg.set_type(type);
    time_t tm = 0;
    msg.set_timestamp(time(&tm));
    return msg;
}
 
BHMsg MakeRequest(const MQId &src_id, const void *data, const size_t size)
{
    assert(data && size);
    BHMsg msg(InitMsg(kMsgTypeRequest));
    msg.set_body(data, size);
    msg.add_route()->set_mq_id(&src_id, sizeof(src_id));
    return msg;
}
BHMsg MakeRequest(const MQId &src_id, const std::string &topic, const void *data, const size_t size)
{
    DataRequest req;
    req.set_topic(topic);
    req.set_data(data, size);
    const std::string &body(req.SerializeAsString());
    return MakeRequest(src_id, body.data(), body.size());
}
 
BHMsg MakeReply(const void *data, const size_t size)
{
    assert(data && size);
    BHMsg msg(InitMsg(kMsgTypeReply));
    DataReply reply;
    reply.set_data(data, size);
    msg.set_body(reply.SerializeAsString());
    return msg;
}
 
BHMsg MakeSubUnsub(const MQId &client, const std::vector<std::string> &topics, const MsgType sub_unsub)
{
    assert(sub_unsub == kMsgTypeSubscribe || sub_unsub == kMsgTypeUnsubscribe);
    BHMsg msg(InitMsg(sub_unsub));
    msg.add_route()->set_mq_id(&client, sizeof(client));
    DataSub subs;
    for (auto &t : topics) {
        subs.add_topics(t);
    }
    msg.set_body(subs.SerializeAsString());
    return msg;
}
 
BHMsg MakeSub(const MQId &client, const std::vector<std::string> &topics) { return MakeSubUnsub(client, topics, kMsgTypeSubscribe); }
BHMsg MakeUnsub(const MQId &client, const std::vector<std::string> &topics) { return MakeSubUnsub(client, topics, kMsgTypeUnsubscribe); }
 
BHMsg MakePub(const std::string &topic, const void *data, const size_t size)
{
    assert(data && size);
    BHMsg msg(InitMsg(kMsgTypePublish));
    DataPub pub;
    pub.set_topic(topic);
    pub.set_data(data, size);
    msg.set_body(pub.SerializeAsString());
    return msg;
}
 
BHMsg MakeQueryTopic(const std::string &topic)
{
    BHMsg msg(InitMsg(kMsgTypeQueryTopic));
    DataQueryTopic query;
    query.set_topic(topic);
    msg.set_body(query.SerializeAsString());
    return msg;
}
 
void *Pack(SharedMemory &shm, const BHMsg &msg)
{
    uint32_t msg_size = msg.ByteSizeLong();
    void *p = shm.Alloc(4 + msg_size);
    if (p) {
        Put32(p, msg_size);
        if (!msg.SerializeToArray(static_cast<char *>(p) + kMsgPrefixLen, msg_size)) {
            shm.Dealloc(p);
            p = 0;
        }
    }
    return p;
}
 
bool MsgI::Unpack(BHMsg &msg) const
{
    void *p = ptr_.get();
    assert(p);
    uint32_t msg_size = Get32(p);
    return msg.ParseFromArray(static_cast<char *>(p) + kMsgPrefixLen, msg_size);
}
 
// with ref count;
bool MsgI::MakeRC(SharedMemory &shm, const BHMsg &msg)
{
    void *p = Pack(shm, msg);
    if (!p) {
        return false;
    }
    RefCount *rc = shm.New<RefCount>();
    if (!rc) {
        shm.Dealloc(p);
        return false;
    }
    MsgI(p, rc).swap(*this);
    return true;
}
 
bool MsgI::Make(SharedMemory &shm, const BHMsg &msg)
{
    void *p = Pack(shm, msg);
    if (!p) {
        return false;
    }
    MsgI(p, 0).swap(*this);
    return true;
}
 
int MsgI::Release(SharedMemory &shm)
{
    if (IsCounted()) {
        const int n = count_->Dec();
        if (n != 0) {
            return n;
        }
    }
    // free data
    shm.Dealloc(ptr_);
    ptr_ = 0;
    shm.Delete(count_);
    count_ = 0;
    return 0;
}
 
} // namespace bhome_msg