lichao
2021-04-14 aa1542b6d6a4680088ac715c4ce40f97ada554fb
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
#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_; }
};
 
template <class Msg>
bool PackOutput(const Msg &msg, void **out, int *out_len)
{
    auto size = msg.ByteSizeLong();
    TmpPtr p(size);
    if (!p) {
        SetLastError(ENOMEM, "not enough memory.");
        return false;
    }
    msg.SerializePartialToArray(p.get(), size);
    *out = p.release();
    *out_len = size;
    return true;
}
 
} // namespace
 
bool BHRegister(const void *proc_info,
                const int proc_info_len,
                void **reply,
                int *reply_len,
                const int timeout_ms)
{
    ProcInfo pi;
    if (!pi.ParseFromArray(proc_info, proc_info_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    MsgCommonReply msg_reply;
    if (ProcNode().Register(pi, msg_reply, timeout_ms)) {
        return PackOutput(msg_reply, reply, reply_len);
    } else {
        return false;
    }
}
 
bool BHHeartBeatEasy(const int timeout_ms)
{
    return ProcNode().Heartbeat(timeout_ms);
}
 
bool BHHeartBeat(const void *proc_info,
                 const int proc_info_len,
                 void **reply,
                 int *reply_len,
                 const int timeout_ms)
{
    ProcInfo pi;
    if (!pi.ParseFromArray(proc_info, proc_info_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    MsgCommonReply msg_reply;
    if (ProcNode().Heartbeat(pi, msg_reply, timeout_ms)) {
        return PackOutput(msg_reply, reply, reply_len);
    } else {
        return false;
    }
}
 
bool 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);
}
 
bool 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)) {
            *proc_id = pproc.release();
            *proc_id_len = pproc.size();
        } else {
            SetLastError(ENOMEM, "out of mem");
        }
    }
    return false;
}
 
bool BHRequest(const void *request,
               const int request_len,
               void **proc_id,
               int *proc_id_len,
               void **reply,
               int *reply_len,
               const int timeout_ms)
{
    MsgRequestTopic req;
    if (!req.ParseFromArray(request, request_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    std::string proc;
    MsgRequestTopicReply out_msg;
    if (ProcNode().ClientSyncRequest(req, proc, out_msg, timeout_ms)) {
        TmpPtr pproc(proc);
        if (pproc && PackOutput(out_msg, reply, reply_len)) {
            *proc_id = pproc.release();
            *proc_id_len = pproc.size();
        } else {
            SetLastError(ENOMEM, "out of mem");
        }
    }
    return false;
}
 
bool 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)) {
            *proc_id = pproc.release();
            *proc_id_len = pproc.size();
            *src = src_info;
        } else {
            SetLastError(ENOMEM, "out of mem");
        }
    }
    return false;
}
 
bool 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);
}
 
int BHCleanUp()
{
    return 0;
}
 
namespace
{
typedef std::function<bool(const void *, const int)> ServerSender;
} // namespace
 
void BHStartWorker(FServerCallback server_cb, FSubDataCallback sub_cb)
{
    TopicNode::ServerCB on_req;
    TopicNode::SubDataCB on_sub;
    if (server_cb) {
        on_req = [server_cb](const std::string &proc_id, const MsgRequestTopic &request, MsgRequestTopicReply &reply) {
            std::string sreq(request.SerializeAsString());
            bool r = false;
            ServerSender sender = [&](const void *p, const int len) {
                r = reply.ParseFromArray(p, len);
                return r;
            };
            server_cb(proc_id.data(), proc_id.size(), sreq.data(), sreq.size(), (BHServerCallbackTag *) (&sender));
            return r;
        };
    }
    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());
        };
    }
 
    ProcNode().Start(on_req, on_sub);
}
bool BHServerCallbackReply(const BHServerCallbackTag *tag,
                           const void *data,
                           const int data_len)
{
    auto &sender = *(const ServerSender *) (tag);
    return sender(data, data_len);
}
 
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);
        if (p) {
            *msg = p.release();
            *msg_len = p.size();
        }
    }
    return ec;
}
 
#undef BH_SOCKET_MEMF_CALL