zhangmeng
2021-02-22 e888eb3aa0ee76cdbb9a8193be40075857d1cf41
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#ifdef __cplusplus
extern "C"{
#endif
 
#ifdef __cplusplus
}
#endif
 
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
 
#include "libcbhomebus.h"
 
#define check_only(fn, lib) \
do{ \
    if(!fn){ \
        dlclose(lib); \
        printf("run func %s error\n", #fn); \
        return; \
    } \
}while(0)
 
#define check_with_ret(fn, lib, flag) \
do{ \
    if(!fn){ \
        dlclose(lib); \
        printf("run func %s error\n", #fn); \
        return flag; \
    } \
}while(0)
 
 
hbhomebus c_bhomebus_handle(const char *so){
    hbhomebus lib = dlopen(so, RTLD_LAZY);
    if(lib){
 
        fn_shm_destroy = (tfn_shm_destroy)dlsym(lib, l_shm_destroy);
        check_with_ret(fn_shm_destroy, lib, NULL);
    }else{
        printf("softbus dlopen - %s\n", dlerror());
    }
    return lib;
}
 
void c_bhomebus_release(hbhomebus lib){
    if(lib) {
        dlclose(lib);
    }
}
 
void wrap_fn_shm_init(hbhomebus lib, int size){
    if (!fn_shm_init){
        fn_shm_init = (tfn_shm_init)dlsym(lib, l_shm_init);
        check_only(fn_shm_init, lib);
    }
    fn_shm_init(size);
}
 
int wrap_fn_shm_alloc_key(hbhomebus lib){
    if (!fn_shm_alloc_key){
        fn_shm_alloc_key = (tfn_shm_alloc_key)dlsym(lib, l_shm_alloc_key);
        check_with_ret(fn_shm_alloc_key, lib, -1);
    }
    return fn_shm_alloc_key();
}
 
int wrap_fn_shm_start_recycle(hbhomebus lib){
    if (!fn_shm_start_recycle){
        fn_shm_start_recycle = (tfn_shm_start_resycle)dlsym(lib, l_shm_start_recycle);
        check_with_ret(fn_shm_start_recycle, lib, -1);
    }
    return fn_shm_start_recycle();
}
 
void wrap_fn_shm_destroy(hbhomebus lib){
    if (!fn_shm_destroy){
        printf("shm destroy failed\n");
        return;
    }
    fn_shm_destroy();
}
 
int wrap_fn_shm_remove_keys(hbhomebus lib, void *keys, int length){
    if (!fn_shm_remove_keys){
        fn_shm_remove_keys = (tfn_shm_remove_keys)dlsym(lib, l_shm_remove_keys);
        check_with_ret(fn_shm_remove_keys, lib, -1);
    }
    return fn_shm_remove_keys((int*)keys, length);
}
 
int wrap_fn_shm_remove_keys_exclude(hbhomebus lib, void *keys, int length){
    if (!fn_shm_remove_keys_exclude){
        fn_shm_remove_keys_exclude = (tfn_shm_remove_keys_exclude)dlsym(lib, l_shm_remove_keys_exclude);
        check_with_ret(fn_shm_remove_keys_exclude, lib, -1);
    }
    return fn_shm_remove_keys_exclude((int*)keys, length);
}
 
////////////////////////////////////////////
// dgram socket mode
////////////////////////////////////////////
 
void * wrap_fn_socket_open(hbhomebus lib)
{
    if(!fn_socket_open){
        fn_socket_open = (tfn_net_mod_socket_open)dlsym(lib,l_net_mod_socket_open);
        check_with_ret(fn_socket_open, lib, NULL);
    }
    return fn_socket_open();
}
 
void wrap_fn_socket_close(hbhomebus lib, void *_socket)
{
    if(!fn_socket_close){
        fn_socket_close = (tfn_net_mod_socket_close)dlsym(lib,l_net_mod_socket_close);
        check_only(fn_socket_close, lib);
    }
    return fn_socket_close(_socket);
}
 
int wrap_fn_socket_bind(hbhomebus lib, void * _socket, int key)
{
    if(!fn_socket_bind){
        fn_socket_bind = (tfn_net_mod_socket_bind)dlsym(lib,l_net_mod_socket_bind);
        check_with_ret(fn_socket_bind, lib, -1);
    }
    return fn_socket_bind(_socket, key);
}
 
int wrap_fn_socket_force_bind(hbhomebus lib, void * _socket, int key)
{
    if(!fn_socket_force_bind){
        fn_socket_force_bind = (tfn_net_mod_socket_force_bind)dlsym(lib,l_net_mod_socket_force_bind);
        check_with_ret(fn_socket_force_bind, lib, -1);
    }
    return fn_socket_force_bind(_socket, key);
}
 
int wrap_fn_socket_sendto(hbhomebus lib, void *_socket, const void *buf, const int size, const int key)
{
    if(!fn_socket_sendto){
        fn_socket_sendto = (tfn_net_mod_socket_sendto)dlsym(lib,l_net_mod_socket_sendto);
        check_with_ret(fn_socket_sendto, lib, -1);
    }
    return fn_socket_sendto(_socket, buf, size, key);
}
 
int wrap_fn_socket_sendto_timeout(hbhomebus lib, void *_socket, const void *buf, const int size, const int key, int sec, int nsec)
{
    if(!fn_socket_sendto_timeout){
        fn_socket_sendto_timeout = (tfn_net_mod_socket_sendto_timeout)dlsym(lib,l_net_mod_socket_sendto_timeout);
        check_with_ret(fn_socket_sendto_timeout, lib, -1);
    }
    return fn_socket_sendto_timeout(_socket, buf, size, key, sec, nsec);
}
 
int wrap_fn_socket_sendto_nowait(hbhomebus lib, void *_socket, const void *buf, const int size, const int key)
{
    if(!fn_socket_sendto_nowait){
        fn_socket_sendto_nowait = (tfn_net_mod_socket_sendto_nowait)dlsym(lib,l_net_mod_socket_sendto_nowait);
        check_with_ret(fn_socket_sendto_nowait, lib, -1);
    }
    return fn_socket_sendto_nowait(_socket, buf, size, key);
}
 
int wrap_fn_socket_recvfrom(hbhomebus lib, void *_socket, void **buf, int *size, int *key)
{
    if(!fn_socket_recvfrom){
        fn_socket_recvfrom = (tfn_net_mod_socket_recvfrom)dlsym(lib,l_net_mod_socket_recvfrom);
        check_with_ret(fn_socket_recvfrom, lib, -1);
    }
    return fn_socket_recvfrom(_socket, buf, size, key);
}
 
int wrap_fn_socket_recvfrom_timeout(hbhomebus lib, void *_socket, void **buf, int *size, int *key, int sec, int nsec)
{
    if(!fn_socket_recvfrom_timeout){
        fn_socket_recvfrom_timeout = (tfn_net_mod_socket_recvfrom_timeout)dlsym(lib,l_net_mod_socket_recvfrom_timeout);
        check_with_ret(fn_socket_recvfrom_timeout, lib, -1);
    }
    return fn_socket_recvfrom_timeout(_socket, buf, size, key, sec, nsec);
}
 
int wrap_fn_socket_recvfrom_nowait(hbhomebus lib, void *_socket, void **buf, int *size, int *key)
{
    if(!fn_socket_recvfrom_nowait){
        fn_socket_recvfrom_nowait = (tfn_net_mod_socket_recvfrom_nowait)dlsym(lib,l_net_mod_socket_recvfrom_nowait);
        check_with_ret(fn_socket_recvfrom_nowait, lib, -1);
    }
    return fn_socket_recvfrom_nowait(_socket, buf, size, key);
}
 
int wrap_fn_socket_sendandrecv(hbhomebus lib, void *_sockt, void *node_arr, int arrlen, void *send_buf, int send_size,
        void ** recv_arr, int *recv_arr_size)
{
    if(!fn_socket_sendandrecv){
        fn_socket_sendandrecv = (tfn_net_mod_socket_sendandrecv)dlsym(lib,l_net_mod_socket_sendandrecv);
        check_with_ret(fn_socket_sendandrecv, lib, -1);
    }
    return fn_socket_sendandrecv(_sockt, (net_node_t*) node_arr, arrlen, send_buf, send_size, (net_mod_recv_msg_t**)recv_arr, recv_arr_size);
}
 
int wrap_fn_socket_sendandrecv_timeout(hbhomebus lib, void *_socket, void *node_arr, int arrlen, void *send_buf, int send_size,
        void ** recv_arr, int *recv_arr_size, int timeout)
{
    if(!fn_socket_sendandrecv_timeout){
        fn_socket_sendandrecv_timeout = (tfn_net_mod_socket_sendandrecv_timeout)dlsym(lib,l_net_mod_socket_sendandrecv_timeout);
        check_with_ret(fn_socket_sendandrecv_timeout, lib, -1);
    }
    return fn_socket_sendandrecv_timeout(_socket, (net_node_t*)node_arr, arrlen, send_buf, send_size, (net_mod_recv_msg_t**)recv_arr, recv_arr_size, timeout);
}
 
int wrap_fn_socket_sendandrecv_nowait(hbhomebus lib, void *_socket, void *node_arr, int arrlen, void *send_buf, int send_size,
        void ** recv_arr, int *recv_arr_size)
{
    if(!fn_socket_sendandrecv_nowait){
        fn_socket_sendandrecv_nowait = (tfn_net_mod_socket_sendandrecv_nowait)dlsym(lib,l_net_mod_socket_sendandrecv_nowait);
        check_with_ret(fn_socket_sendandrecv_nowait, lib, -1);
    }
    return fn_socket_sendandrecv_nowait(_socket, (net_node_t*)node_arr, arrlen, send_buf, send_size, (net_mod_recv_msg_t**)recv_arr, recv_arr_size);
}
 
// add recvandsend funcs
int wrap_fn_socket_recvandsend(hbhomebus lib, void *_sockt, recvandsend_callback_fn fn, void *user_data){
    if (!fn_socket_recvandsend){
        fn_socket_recvandsend = (tfn_net_mod_socket_recvandsend)dlsym(lib, l_net_mod_socket_recvandsend);
        check_with_ret(fn_socket_recvandsend, lib, -1);
    }
    return fn_socket_recvandsend(_sockt, fn, user_data);
}
 
int wrap_fn_socket_recvandsend_timeout(hbhomebus lib, void *_sockt, recvandsend_callback_fn fn, int sec, int nsec, void *user_data){
    if (!fn_socket_recvandsend_timeout){
        fn_socket_recvandsend_timeout = (tfn_net_mod_socket_recvandsend_timeout)dlsym(lib, l_net_mod_socket_recvandsend_timeout);
        check_with_ret(fn_socket_recvandsend_timeout, lib, -1);
    }
    return fn_socket_recvandsend_timeout(_sockt, fn, sec, nsec, user_data);
}
 
int wrap_fn_socket_recvandsend_nowait (hbhomebus lib, void *_sockt, recvandsend_callback_fn fn, void *user_data){
    if (!fn_socket_recvandsend_nowait){
        fn_socket_recvandsend_nowait = (tfn_net_mod_socket_recvandsend_nowait)dlsym(lib, l_net_mod_socket_recvandsend_nowait);
        check_with_ret(fn_socket_recvandsend_nowait, lib, -1);
    }
    return fn_socket_recvandsend_nowait(_sockt, fn, user_data);
}
 
//int  wrap_fn_socket_start_bus(hbhomebus lib, void * _socket)
//{
//    if(!fn_socket_start_bus){
//        fn_socket_start_bus = (tfn_net_mod_socket_start_bus)dlsym(lib,l_net_mod_socket_start_bus);
//        check_with_ret(fn_socket_start_bus, lib, -1);
//    }
//    return fn_socket_start_bus(_socket);
//}
 
int wrap_fn_socket_pub(hbhomebus lib, void *_socket, void *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size)
{
    if(!fn_socket_pub){
        fn_socket_pub = (tfn_net_mod_socket_pub)dlsym(lib,l_net_mod_socket_pub);
        check_with_ret(fn_socket_pub, lib, -1);
    }
    return fn_socket_pub(_socket, (net_node_t*)node_arr, node_arr_len, topic, topic_size, content, content_size);
}
 
int wrap_fn_socket_pub_timeout(hbhomebus lib, void *_socket, void *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size, int timeout)
{
    if(!fn_socket_pub_timeout){
        fn_socket_pub_timeout = (tfn_net_mod_socket_pub_timeout)dlsym(lib,l_net_mod_socket_pub_timeout);
        check_with_ret(fn_socket_pub_timeout, lib, -1);
    }
    return fn_socket_pub_timeout(_socket, (net_node_t*)node_arr, node_arr_len, topic, topic_size, content, content_size, timeout);
}
 
int wrap_fn_socket_pub_nowait(hbhomebus lib, void *_socket, void *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size)
{
    if(!fn_socket_pub_nowait){
        fn_socket_pub_nowait = (tfn_net_mod_socket_pub_nowait)dlsym(lib,l_net_mod_socket_pub_nowait);
        check_with_ret(fn_socket_pub_nowait, lib, -1);
    }
    return fn_socket_pub_nowait(_socket, (net_node_t*)node_arr, node_arr_len, topic, topic_size, content, content_size);
}
 
int  wrap_fn_socket_sub(hbhomebus lib, void * _socket, void *topic, int size)
{
    if(!fn_socket_sub){
        fn_socket_sub = (tfn_net_mod_socket_sub)dlsym(lib,l_net_mod_socket_sub);
        check_with_ret(fn_socket_sub, lib, -1);
    }
    return fn_socket_sub(_socket, topic, size);
}
 
int  wrap_fn_socket_sub_timeout(hbhomebus lib, void * _socket, void *topic, int size, int sec, int nsec)
{
    if(!fn_socket_sub_timeout){
        fn_socket_sub_timeout = (tfn_net_mod_socket_sub_timeout)dlsym(lib,l_net_mod_socket_sub_timeout);
        check_with_ret(fn_socket_sub_timeout, lib, -1);
    }
    return fn_socket_sub_timeout(_socket, topic, size, sec, nsec);
}
 
int  wrap_fn_socket_sub_nowait(hbhomebus lib, void * _socket, void *topic, int size)
{
    if(!fn_socket_sub_nowait){
        fn_socket_sub_nowait = (tfn_net_mod_socket_sub_nowait)dlsym(lib,l_net_mod_socket_sub_nowait);
        check_with_ret(fn_socket_sub_nowait, lib, -1);
    }
    return fn_socket_sub_nowait(_socket, topic, size);
}
 
int  wrap_fn_socket_desub(hbhomebus lib, void * _socket, void *topic, int size)
{
    if(!fn_socket_desub){
        fn_socket_desub = (tfn_net_mod_socket_desub)dlsym(lib,l_net_mod_socket_desub);
        check_with_ret(fn_socket_desub, lib, -1);
    }
    return fn_socket_desub(_socket, topic, size);
}
 
int  wrap_fn_socket_desub_timeout(hbhomebus lib, void * _socket, void *topic, int size, int sec, int nsec)
{
    if(!fn_socket_desub_timeout){
        fn_socket_desub_timeout = (tfn_net_mod_socket_desub_timeout)dlsym(lib,l_net_mod_socket_desub_timeout);
        check_with_ret(fn_socket_desub_timeout, lib, -1);
    }
    return fn_socket_desub_timeout(_socket, topic, size, sec, nsec);
}
 
int  wrap_fn_socket_desub_nowait(hbhomebus lib, void * _socket, void *topic, int size)
{
    if(!fn_socket_desub_nowait){
        fn_socket_desub_nowait = (tfn_net_mod_socket_sub_nowait)dlsym(lib,l_net_mod_socket_sub_nowait);
        check_with_ret(fn_socket_desub_nowait, lib, -1);
    }
    return fn_socket_desub_nowait(_socket, topic, size);
}
 
int wrap_fn_socket_get_key(hbhomebus lib, void * _socket)
{
    if(!fn_socket_get_key){
        fn_socket_get_key = (tfn_net_mod_socket_get_key)dlsym(lib,l_net_mod_socket_get_key);
        check_with_ret(fn_socket_get_key, lib, -1);
    }
    return fn_socket_get_key(_socket);
}
 
void  wrap_fn_socket_free_recv_msg_arr(hbhomebus lib, void * arr, int size)
{
    if(!fn_socket_free_recv_msg_arr){
        fn_socket_free_recv_msg_arr = (tfn_net_mod_socket_free_recv_msg_arr)dlsym(lib,l_net_mod_socket_free_recv_msg_arr);
        check_only(fn_socket_free_recv_msg_arr, lib);
    }
    return fn_socket_free_recv_msg_arr((net_mod_recv_msg_t*)arr, size);
}
 
void wrap_fn_socket_free(hbhomebus lib, void *buf)
{
    if(!fn_socket_free){
        fn_socket_free = (tfn_net_mod_socket_free)dlsym(lib,l_net_mod_socket_free);
        check_only(fn_socket_free, lib);
    }
    return fn_socket_free(buf);
}
 
void *wrap_fn_server_socket_open(hbhomebus lib, int port){
    if (!fn_server_socket_open){
        fn_server_socket_open = (tfn_net_mod_server_socket_open)dlsym(lib, l_net_mod_server_socket_open);
        check_with_ret(fn_server_socket_open, lib, NULL);
    }
    return fn_server_socket_open(port);
}
 
void wrap_fn_server_socket_close(hbhomebus lib, void* _socket){
    if (!fn_server_socket_close){
        fn_server_socket_close = (tfn_net_mod_server_socket_close)dlsym(lib, l_net_mod_server_socket_close);
        check_only(fn_server_socket_close, lib);
    }
    fn_server_socket_close(_socket);
}
 
int wrap_fn_server_socket_start(hbhomebus lib, void* _socket){
    if (!fn_server_socket_start){
        fn_server_socket_start = (tfn_net_mod_server_socket_start)dlsym(lib, l_net_mod_server_socket_start);
        check_with_ret(fn_server_socket_start, lib, -1);
    }
    return fn_server_socket_start(_socket);
}
 
void *wrap_fn_bus_server_socket_open(hbhomebus lib){
    if (!fn_bus_server_socket_open){
        fn_bus_server_socket_open = (tfn_bus_server_socket_wrapper_open)dlsym(lib, l_bus_server_socket_wrapper_open);
        check_with_ret(fn_bus_server_socket_open, lib, NULL);
    }
    return fn_bus_server_socket_open();
}
 
void wrap_fn_bus_server_socket_close(hbhomebus lib, void* _socket){
    if (!fn_bus_server_socket_close){
        fn_bus_server_socket_close = (tfn_bus_server_socket_wrapper_close)dlsym(lib, l_bus_server_socket_wrapper_close);
        check_only(fn_bus_server_socket_close, lib);
    }
    fn_bus_server_socket_close(_socket);
}
 
int wrap_fn_bus_server_socket_start(hbhomebus lib, void* _socket){
    if (!fn_bus_server_socket_start){
        fn_bus_server_socket_start = (tfn_bus_server_socket_wrapper_start_bus)dlsym(lib, l_bus_server_socket_wrapper_start_bus);
        check_with_ret(fn_bus_server_socket_start, lib, -1);
    }
    return fn_bus_server_socket_start(_socket);
}