lichao
2021-04-21 f6535ea2ae09b3cdca9104fa19dfff39a47271ea
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "bh_api.h"
#include "defs.h"
#include "topic_node.h"
#include <memory>
 
using namespace bhome_shm;
using namespace bhome_msg;
 
namespace
{
TopicNode &ProcNode()
{
    static TopicNode node(BHomeShm());
    return node;
}
 
class TmpPtr : private boost::noncopyable
{
    void *ptr_ = 0;
    size_t size_ = 0;
 
public:
    explicit TmpPtr(const size_t size) :
        ptr_(malloc(size)), size_(size) {}
    explicit TmpPtr(const std::string &str) :
        TmpPtr(str.size())
    {
        if (ptr_) {
            memcpy(ptr_, str.data(), str.size());
        }
    }
    ~TmpPtr() { free(ptr_); }
    void *get() const { return ptr_; }
    void *release()
    {
        void *tmp = ptr_;
        ptr_ = 0;
        return tmp;
    }
    size_t size() const { return size_; }
    operator bool() const { return ptr_; }
    bool ReleaseTo(void **pdata, int *psize)
    {
        if (!ptr_) {
            return false;
        }
        if (pdata && psize) {
            *psize = size();
            *pdata = release();
        }
        return true;
    }
};
 
template <class Msg>
bool PackOutput(const Msg &msg, void **out, int *out_len)
{
    if (!out || !out_len) {
        return true; // not wanted.
    }
    auto size = msg.ByteSizeLong();
    TmpPtr p(size);
    if (!p) {
        SetLastError(ENOMEM, "not enough memory.");
        return false;
    }
    msg.SerializePartialToArray(p.get(), size);
    p.ReleaseTo(out, out_len);
    return true;
}
 
template <class MsgIn, class MsgOut = MsgCommonReply>
bool BHApiIn1Out1(bool (TopicNode::*mfunc)(MsgIn &, MsgOut &, const int),
                  const void *request,
                  const int request_len,
                  void **reply,
                  int *reply_len,
                  const int timeout_ms)
{
    MsgIn input;
    if (!input.ParseFromArray(request, request_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    MsgOut msg_reply;
    return (ProcNode().*mfunc)(input, msg_reply, timeout_ms) &&
           PackOutput(msg_reply, reply, reply_len);
}
 
} // namespace
 
int BHApiIn1Out1Proxy(FBHApiIn1Out1 func,
                      const void *request,
                      const int request_len,
                      void **reply,
                      int *reply_len,
                      const int timeout_ms)
{
    return (*func)(request, request_len, reply, reply_len, timeout_ms);
}
 
int BHRegister(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms)
{
    return BHApiIn1Out1<ProcInfo>(&TopicNode::Register, proc_info, proc_info_len, reply, reply_len, timeout_ms);
}
 
int BHHeartbeatEasy(const int timeout_ms)
{
    return ProcNode().Heartbeat(timeout_ms);
}
 
int BHHeartbeat(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms)
{
    return BHApiIn1Out1<ProcInfo>(&TopicNode::Heartbeat, proc_info, proc_info_len, reply, reply_len, timeout_ms);
}
 
int BHRegisterTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms)
{
    return BHApiIn1Out1<MsgTopicList>(&TopicNode::ServerRegisterRPC, topics, topics_len, reply, reply_len, timeout_ms);
}
 
int BHSubscribeTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms)
{
    return BHApiIn1Out1<MsgTopicList>(&TopicNode::Subscribe, topics, topics_len, reply, reply_len, timeout_ms);
}
 
int BHPublish(const void *msgpub,
              const int msgpub_len,
              const int timeout_ms)
{
    MsgPublish pub;
    if (!pub.ParseFromArray(msgpub, msgpub_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    return ProcNode().Publish(pub, timeout_ms);
}
 
int BHReadSub(void **proc_id,
              int *proc_id_len,
              void **msgpub,
              int *msgpub_len,
              const int timeout_ms)
{
    std::string proc;
    MsgPublish pub;
 
    if (ProcNode().RecvSub(proc, pub, timeout_ms)) {
        TmpPtr pproc(proc);
        if (pproc && PackOutput(pub, msgpub, msgpub_len)) {
            pproc.ReleaseTo(proc_id, proc_id_len);
            return true;
        } else {
            SetLastError(ENOMEM, "out of mem");
        }
    }
    return false;
}
 
int BHAsyncRequest(const void *remote,
                   const int remote_len,
                   const void *request,
                   const int request_len,
                   void **msg_id,
                   int *msg_id_len)
{
    BHAddress dest;
    MsgRequestTopic req;
    if (!dest.ParseFromArray(remote, remote_len) ||
        !req.ParseFromArray(request, request_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    std::string str_msg_id;
    MsgRequestTopicReply out_msg;
    if (ProcNode().ClientAsyncRequest(dest, req, str_msg_id)) {
        if (!msg_id || !msg_id_len) {
            return true;
        }
        TmpPtr ptr(str_msg_id);
        if (ptr) {
            ptr.ReleaseTo(msg_id, msg_id_len);
            return true;
        } else {
            SetLastError(ENOMEM, "out of mem");
        }
    }
    return false;
}
 
int BHRequest(const void *remote,
              const int remote_len,
              const void *request,
              const int request_len,
              void **proc_id,
              int *proc_id_len,
              void **reply,
              int *reply_len,
              const int timeout_ms)
{
    BHAddress dest;
    MsgRequestTopic req;
    if (!dest.ParseFromArray(remote, remote_len) ||
        !req.ParseFromArray(request, request_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    std::string proc;
    MsgRequestTopicReply out_msg;
    if (ProcNode().ClientSyncRequest(dest, req, proc, out_msg, timeout_ms)) {
        TmpPtr pproc(proc);
        if (pproc && PackOutput(out_msg, reply, reply_len)) {
            pproc.ReleaseTo(proc_id, proc_id_len);
            return true;
        } else {
            SetLastError(ENOMEM, "out of mem");
        }
    }
    return false;
}
 
int BHReadRequest(void **proc_id,
                  int *proc_id_len,
                  void **request,
                  int *request_len,
                  void **src,
                  const int timeout_ms)
{
    void *src_info = 0;
    std::string proc;
    MsgRequestTopic out_msg;
    if (ProcNode().ServerRecvRequest(src_info, proc, out_msg, timeout_ms)) {
        TmpPtr pproc(proc);
        if (pproc && PackOutput(out_msg, request, request_len)) {
            pproc.ReleaseTo(proc_id, proc_id_len);
            *src = src_info;
            return true;
        } else {
            SetLastError(ENOMEM, "out of mem");
        }
    }
    return false;
}
 
int BHSendReply(void *src,
                const void *reply,
                const int reply_len)
{
    MsgRequestTopicReply rep;
    if (!rep.ParseFromArray(reply, reply_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    return ProcNode().ServerSendReply(src, rep);
}
 
void BHStartWorker(FServerCallback server_cb, FSubDataCallback sub_cb, FClientCallback client_cb)
{
    TopicNode::ServerAsyncCB on_req;
    TopicNode::SubDataCB on_sub;
    TopicNode::RequestResultCB on_reply;
    if (server_cb) {
        on_req = [server_cb](void *src, std::string &proc_id, const MsgRequestTopic &request) {
            std::string sreq(request.SerializeAsString());
            server_cb(proc_id.data(), proc_id.size(), sreq.data(), sreq.size(), src);
        };
    }
    if (sub_cb) {
        on_sub = [sub_cb](const std::string &proc_id, const MsgPublish &pub) {
            std::string s(pub.SerializeAsString());
            sub_cb(proc_id.data(), proc_id.size(), s.data(), s.size());
        };
    }
    if (client_cb) {
        on_reply = [client_cb](const BHMsgHead &head, const MsgRequestTopicReply &rep) {
            std::string s(rep.SerializeAsString());
            client_cb(head.proc_id().data(), head.proc_id().size(),
                      head.msg_id().data(), head.msg_id().size(),
                      s.data(), s.size());
        };
    }
 
    ProcNode().Start(on_req, on_sub, on_reply);
}
 
void BHFree(void *data, int size)
{
    free(data);
}
 
int BHGetLastError(void **msg, int *msg_len)
{
    int ec = 0;
    if (msg && msg_len) {
        std::string err_msg;
        GetLastError(ec, err_msg);
        TmpPtr p(err_msg);
        p.ReleaseTo(msg, msg_len);
    }
    return ec;
}
 
#undef BH_SOCKET_MEMF_CALL