zhangmeng
2020-07-31 dba48754d9623a49b155e94c65341b773bf4eeef
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
#ifdef __cplusplus
extern "C"{
#endif
 
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
 
#include "libcsoftbus.h"
 
#ifdef __cplusplus
}
#endif
 
#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)
 
 
hcsoftbus c_softbus_handle(const char *so){
    hcsoftbus 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_softbus_release(hcsoftbus lib){
    if(lib) {
        dlclose(lib);
    }
}
 
void wrap_fn_shm_init(hcsoftbus 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(hcsoftbus 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();
}
 
void wrap_fn_shm_destroy(hcsoftbus lib){
    if (!fn_shm_destroy){
        printf("shm destroy failed\n");
        return;
    }
    fn_shm_destroy();
}
 
void wrap_fn_shm_rm_dead_queue(hcsoftbus lib, void *array, int len){
    if (!fn_shm_rm_dead_queue){
        fn_shm_rm_dead_queue = (tfn_shm_rm_dead_queue)dlsym(lib, l_shm_rm_dead_queue);
        check_only(fn_shm_rm_dead_queue, lib);
    }
    fn_shm_rm_dead_queue(array, len);
}
 
void* wrap_fn_queue_create(hcsoftbus lib, int *key, int queue_size){
    if (!fn_queue_create){
        fn_queue_create = (tfn_queue_create)dlsym(lib, l_queue_create);
        check_with_ret(fn_queue_create, lib, NULL);
    }
    return fn_queue_create(key, queue_size);
}
 
void* wrap_fn_queue_attach(hcsoftbus lib, int key){
    if (!fn_queue_attach){
        fn_queue_attach = (tfn_queue_attach)dlsym(lib, l_queue_attach);
        check_with_ret(fn_queue_attach, lib, NULL);
    }
    return fn_queue_attach(key);
}
 
void wrap_fn_queue_drop(hcsoftbus lib, void* shmq){
    if (!fn_queue_drop){
        fn_queue_drop = (tfn_queue_drop)dlsym(lib, l_queue_drop);
        check_only(fn_queue_drop, lib);
    }
    fn_queue_drop(shmq);
}
 
int wrap_fn_queue_size(hcsoftbus lib, void *shmq){
    if (!fn_queue_size){
        fn_queue_size = (tfn_queue_size)dlsym(lib, l_queue_size);
        check_with_ret(fn_queue_size, lib, 0);
    }
    return fn_queue_size(shmq);
}
 
int wrap_fn_queue_full(hcsoftbus lib, void *shmq){
    if (!fn_queue_full){
        fn_queue_full = (tfn_queue_full)dlsym(lib, l_queue_full);
        check_with_ret(fn_queue_full, lib, 1);
    }
    return fn_queue_full(shmq);
}
 
int wrap_fn_queue_empty(hcsoftbus lib, void *shmq){
    if (!fn_queue_empty){
        fn_queue_empty = (tfn_queue_empty)dlsym(lib, l_queue_empty);
        check_with_ret(fn_queue_empty, lib, 0);
    }
    return fn_queue_empty(shmq);
}
 
int wrap_fn_queue_push(hcsoftbus lib, void *shmq, void *ele, int ele_size){
    if (!fn_queue_push){
        fn_queue_push = (tfn_queue_push)dlsym(lib, l_queue_push);
        check_with_ret(fn_queue_push, lib, 0);
    }
 
    return fn_queue_push(shmq, ele, ele_size);
}
 
int wrap_fn_queue_push_nowait(hcsoftbus lib, void *shmq, void *ele, int ele_size){
    if (!fn_queue_push_nowait){
        fn_queue_push_nowait = (tfn_queue_push_nowait)dlsym(lib, l_queue_push_nowait);
        check_with_ret(fn_queue_push_nowait, lib, 0);
    }
    return fn_queue_push_nowait(shmq, ele, ele_size);
}
 
int wrap_fn_queue_push_timeout(hcsoftbus lib, void *shmq, void *ele, int ele_size, int sec, int nsec){
    if (!fn_queue_push_timeout){
        fn_queue_push_timeout = (tfn_queue_push_timeout)dlsym(lib, l_queue_push_timeout);
        check_with_ret(fn_queue_push_timeout, lib, 0);
    }
    return fn_queue_push_timeout(shmq, ele, ele_size, sec, nsec);
}
 
int wrap_fn_queue_pop(hcsoftbus lib, void *shmq, void **ele, int *ele_size){
    if (!fn_queue_pop){
        fn_queue_pop = (tfn_queue_pop)dlsym(lib, l_queue_pop);
        check_with_ret(fn_queue_pop, lib, 0);
    }
    return fn_queue_pop(shmq, ele, ele_size);
}
 
int wrap_fn_queue_pop_nowait(hcsoftbus lib, void *shmq, void **ele, int *ele_size){
    if (!fn_queue_pop_nowait){
        fn_queue_pop_nowait = (tfn_queue_pop_nowait)dlsym(lib, l_queue_pop_nowait);
        check_with_ret(fn_queue_pop_nowait, lib, 0);
    }
    return fn_queue_pop_nowait(shmq, ele, ele_size);
}
 
int wrap_fn_queue_pop_timeout(hcsoftbus lib, void *shmq, void **ele, int *ele_size, int sec, int nsec){
    if (!fn_queue_pop_timeout){
        fn_queue_pop_timeout = (tfn_queue_pop_timeout)dlsym(lib, l_queue_pop_timeout);
        check_with_ret(fn_queue_pop_timeout, lib, 0);
    }
 
    return fn_queue_pop_timeout(shmq, ele, ele_size, sec, nsec);
}
 
void wrap_fn_queue_free_poped(hcsoftbus lib, void *ptr){
    if (!fn_queue_free_poped){
        fn_queue_free_poped = (tfn_queue_free_poped)dlsym(lib, l_queue_free_poped);
        check_only(fn_queue_free_poped, lib);
    }
    fn_queue_free_poped(ptr);
}
 
////////////////////////////////////////////
// socket mode
////////////////////////////////////////////
void *wrap_fn_socket_open(hcsoftbus lib, int mod){
    if (!fn_socket_open) {
        fn_socket_open = (tfn_socket_open)dlsym(lib, l_socket_open);
        check_with_ret(fn_socket_open, lib, NULL);
    }
    return fn_socket_open(mod);
}
 
int wrap_fn_socket_close(hcsoftbus lib, void* s){
    if(!fn_socket_close){
        fn_socket_close = (tfn_socket_close)dlsym(lib, l_socket_close);
        check_with_ret(fn_socket_close, lib, -1);
    }
    return fn_socket_listen(s);
}
 
int wrap_fn_socket_bind(hcsoftbus lib, void *s, int port){
    if (!fn_socket_bind){
        fn_socket_bind = (tfn_socket_bind)dlsym(lib, l_socket_bind);
        check_with_ret(fn_socket_bind, lib, -1);
    }
 
    return fn_socket_bind(s, port);
}
 
int wrap_fn_socket_listen(hcsoftbus lib, void *s){
    if (!fn_socket_listen){
        fn_socket_listen = (tfn_socket_listen)dlsym(lib, l_socket_listen);
        check_with_ret(fn_socket_listen, lib, -1);
    }
    return fn_socket_listen(s);
}
 
int wrap_fn_socket_connect(hcsoftbus lib, void *s, int port){
    if (!fn_socket_connect){
        fn_socket_connect = (tfn_socket_connect)dlsym(lib, l_socket_connect);
        check_with_ret(fn_socket_connect, lib, -1);
    }
    return fn_socket_connect(s, port);
}
 
int wrap_fn_socket_send(hcsoftbus lib, void *s, const void *buf, const int size){
    if (!fn_socket_send){
        fn_socket_send = (tfn_socket_send)dlsym(lib, l_socket_send);
        check_with_ret(fn_socket_send, lib, -1);
    }
 
    return fn_socket_send(s, buf, size);
}
 
int wrap_fn_socket_recv(hcsoftbus lib, void *s, void **buf, int *size){
    if (!fn_socket_recv){
        fn_socket_recv = (tfn_socket_recv)dlsym(lib, l_socket_recv);
        check_with_ret(fn_socket_recv, lib, -1);
    }
    return fn_socket_recv(s, buf, size);
}
 
void wrap_fn_socket_buf_free(hcsoftbus lib, void *buf){
    if (!fn_socket_buf_free){
        fn_socket_buf_free = (tfn_socket_buf_free)dlsym(lib, l_socket_free);
    }
    if (fn_socket_buf_free){
        fn_socket_buf_free(buf);
    }
}
 
int wrap_fn_socket_port(hcsoftbus lib, void *s){
    if (!fn_socket_port){
        fn_socket_port = (tfn_socket_port)dlsym(lib, l_socket_port);
        check_with_ret(fn_socket_port, lib, -1);
    }
 
    return fn_socket_port(s);
}
////////////////////////////////////////////
// dgram socket mode
////////////////////////////////////////////
void *wrap_fn_dgram_socket_open(hcsoftbus lib){
    if (!fn_dgram_socket_open){
        fn_dgram_socket_open = (tfn_dgram_socket_open)dlsym(lib , l_dgram_socket_open);
        check_with_ret(fn_dgram_socket_open, lib, NULL);
    }
    return fn_dgram_socket_open();
}
 
int wrap_fn_dgram_socket_close(hcsoftbus lib, void *s){
    if (!fn_dgram_socket_close){
        fn_dgram_socket_close = (tfn_dgram_socket_close)dlsym(lib, l_dgram_socket_close);
        check_with_ret(fn_dgram_socket_close, lib, -1);
    }
    return fn_dgram_socket_close(s);
}
 
int wrap_fn_dgram_socket_bind(hcsoftbus lib, void *s, int port){
    if (!fn_dgram_socket_bind){
        fn_dgram_socket_bind = (tfn_dgram_socket_bind)dlsym(lib, l_dgram_socket_bind);
        check_with_ret(fn_dgram_socket_bind, lib, -1);
    }
    return fn_dgram_socket_bind(s, port);
}
 
int wrap_fn_dgram_socket_force_bind(hcsoftbus lib, void *s, int port){
    if (!fn_dgram_socket_force_bind){
        fn_dgram_socket_force_bind = (tfn_dgram_socket_force_bind)dlsym(lib, l_dgram_socket_force_bind);
        check_with_ret(fn_dgram_socket_force_bind, lib, -1);
    }
    return fn_dgram_socket_force_bind(s, port);
}
 
int wrap_fn_dgram_socket_sendto(hcsoftbus lib, void *s, const void *buf, const int size, const int port){
    if (!fn_dgram_socket_sendto){
        fn_dgram_socket_sendto = (tfn_dgram_socket_sendto)dlsym(lib, l_dgram_socket_sendto);
        check_with_ret(fn_dgram_socket_sendto, lib, -1);
    }
    return fn_dgram_socket_sendto(s, buf, size, port);
}
 
int wrap_fn_dgram_socket_recvfrom(hcsoftbus lib, void *s, void **buf, int *size, int *port){
    if (!fn_dgram_socket_recvfrom){
        fn_dgram_socket_recvfrom = (tfn_dgram_socket_recvfrom)dlsym(lib, l_dgram_socket_recvfrom);
        check_with_ret(fn_dgram_socket_recvfrom, lib, -1);
    }
    return fn_dgram_socket_recvfrom(s, buf, size, port);
}
 
int wrap_fn_dgram_socket_sendandrecv(hcsoftbus lib, void *s, const void *sbuf, const int ssize, const int port, void **rbuf, int *rsize){
    if (!fn_dgram_socket_sendandrecv){
        fn_dgram_socket_sendandrecv = (tfn_dgram_socket_sendandrecv)dlsym(lib, l_dgram_socket_sendandrecv);
        check_with_ret(fn_dgram_socket_sendandrecv, lib, -1);
    }
    return fn_dgram_socket_sendandrecv(s, sbuf, ssize, port, rbuf, rsize);
}
 
int wrap_fn_dgram_socket_start_bus(hcsoftbus lib, void *s){
    if (!fn_dgram_socket_start_bus){
        fn_dgram_socket_start_bus = (tfn_dgram_socket_start_bus)dlsym(lib, l_dgram_socket_start_bus);
        check_with_ret(fn_dgram_socket_start_bus, lib, -1);
    }
    return fn_dgram_socket_start_bus(s);
}
 
int wrap_fn_dgram_socket_sub(hcsoftbus lib, void *s, void *topic, int size, int port){
    if (!fn_dgram_socket_sub){
        fn_dgram_socket_sub = (tfn_dgram_socket_sub)dlsym(lib, l_dgram_socket_sub);
        check_with_ret(fn_dgram_socket_sub, lib, -1);
    }
    return fn_dgram_socket_sub(s, topic, size, port);
}
 
int wrap_fn_dgram_socket_pub(hcsoftbus lib, void *s, void *topic, int tsize, void *content, int csize, int port){
    if (!fn_dgram_socket_pub){
        fn_dgram_socket_pub = (tfn_dgram_socket_pub)dlsym(lib, l_dgram_socket_pub);
        check_with_ret(fn_dgram_socket_pub, lib, -1);
    }
    return fn_dgram_socket_pub(s, topic, tsize, content, csize, port);
}
 
int wrap_fn_dgram_socket_port(hcsoftbus lib, void *s){
    if (!fn_dgram_socket_port){
        fn_dgram_socket_port = (tfn_dgram_socket_port)dlsym(lib, l_dgram_socket_port);
        check_with_ret(fn_dgram_socket_port, lib, -1);
    }
    return fn_dgram_socket_port(s);
}
 
void wrap_fn_dgram_socket_free(hcsoftbus lib, void *buf){
    if (!fn_dgram_socket_free){
        fn_dgram_socket_free = (tfn_dgram_socket_free)dlsym(lib, l_dgram_socket_free);
    }
    if (fn_dgram_socket_free){
        fn_dgram_socket_free(buf);
    }else{
        free(buf);
    }
}