zhangmeng
2020-07-31 9cdfda6d2095657394d9a537e87a5fec1d24b742
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
package softbus
 
/*
#cgo LDFLAGS: -ldl
#include <stdlib.h>
#include "libcsoftbus.h"
 
void *get_array(const int size){
    int *arr = (int*)malloc(sizeof(int)*size);
    return arr;
}
 
void set_array(void *arr, const int index, const int value){
    int *iarr = (int*)arr;
    iarr[index] = value;
}
*/
import "C"
import (
    "errors"
    "unsafe"
)
 
var libsoftbus C.hcsoftbus
 
// Init Dynamic library
func Init(so string) error {
    cso := C.CString(so)
    defer C.free(unsafe.Pointer(cso))
 
    handle := C.c_softbus_handle(cso)
    if handle == nil {
        // fmt.Errorf("Go SoftBus Init From %s Error\n", so)
        return errors.New("Init SoftBus Error")
    }
 
    libsoftbus = handle
    return nil
}
 
// Release handle of c softbus
func Release() {
    if libsoftbus != nil {
        C.c_softbus_release(libsoftbus)
    }
}
 
// ShmInit block size
func ShmInit(size int) error {
    if libsoftbus == nil {
        return errors.New("C SoftBus Handle Is Nil")
    }
    C.wrap_fn_shm_init(libsoftbus, C.int(size))
    return nil
}
 
// ShmAllocKey alloc key
func ShmAllocKey() int {
    if libsoftbus == nil {
        return -1
    }
    r := C.wrap_fn_shm_alloc_key(libsoftbus)
    return int(r)
}
 
// ShmDestroy destroy shm block, every softbus proc MUST call it
func ShmDestroy() {
    if libsoftbus != nil {
        C.wrap_fn_shm_destroy(libsoftbus)
    }
}
 
// ShmRmDeadQueue remove dead queue but pass live queue keys
func ShmRmDeadQueue(keys []int) error {
    if libsoftbus == nil {
        return errors.New("C SoftBus Handle Is Nil")
    }
    if len(keys) == 0 {
        C.wrap_fn_shm_rm_dead_queue(libsoftbus, nil, 0)
        return nil
    }
 
    l := len(keys)
    ca := C.get_array(C.int(l))
    defer C.free(ca)
    for k, v := range keys {
        C.set_array(ca, C.int(k), C.int(v))
    }
 
    C.wrap_fn_shm_rm_dead_queue(libsoftbus, ca, C.int(l))
    return nil
}
 
// Queue struct wrap shmqueue pointer
type Queue struct {
    shmqueue unsafe.Pointer
    key      int
}
 
// Create shm queue
func Create(qSize int) *Queue {
    if libsoftbus == nil {
        return nil
    }
 
    var key C.int
    csb := C.wrap_fn_queue_create(libsoftbus, &key, C.int(qSize))
    if csb != nil {
        return &Queue{
            shmqueue: csb,
            key:      int(key),
        }
    }
    return nil
}
 
// Attach shm queue with key
func Attach(key int) *Queue {
    if libsoftbus == nil {
        return nil
    }
 
    csb := C.wrap_fn_queue_attach(libsoftbus, C.int(key))
    if csb != nil {
        return &Queue{
            shmqueue: csb,
            key:      key,
        }
    }
 
    return nil
}
 
// Drop shm queue
func (q *Queue) Drop() error {
    if libsoftbus == nil {
        return errors.New("C SoftBus Handle Is Nil")
    }
 
    C.wrap_fn_queue_drop(libsoftbus, q.shmqueue)
    return nil
}
 
// Size shm queue
func (q *Queue) Size() int {
    if libsoftbus == nil {
        return 0
    }
 
    cs := C.wrap_fn_queue_size(libsoftbus, q.shmqueue)
    return int(cs)
}
 
// Full shm queue
func (q *Queue) Full() bool {
    if libsoftbus == nil {
        return true
    }
 
    f := C.wrap_fn_queue_full(libsoftbus, q.shmqueue)
    if f != 0 {
        return true
    }
    return false
}
 
// Empty shm queue
func (q *Queue) Empty() bool {
    if libsoftbus == nil {
        return false
    }
 
    f := C.wrap_fn_queue_full(libsoftbus, q.shmqueue)
    if f != 0 {
        return true
    }
    return false
 
}
 
// Push shm queue
func (q *Queue) Push(data []byte) error {
    if libsoftbus == nil {
        return errors.New("C SoftBus Handle Is Nil")
    }
 
    r := C.wrap_fn_queue_push(libsoftbus, q.shmqueue, unsafe.Pointer(&data[0]), C.int(len(data)))
    if r == 0 {
        return nil
    }
    return errors.New("Queue Push Data Error")
}
 
// PushNoWait shm queue
func (q *Queue) PushNoWait(data []byte) error {
    if libsoftbus == nil {
        return errors.New("C SoftBus Handle Is Nil")
    }
 
    r := C.wrap_fn_queue_push_nowait(libsoftbus, q.shmqueue, unsafe.Pointer(&data[0]), C.int(len(data)))
    if r == 0 {
        return nil
    }
    return errors.New("Queue PushNoWait Data Error")
}
 
// PushTimeout shm queue push with timeout
func (q *Queue) PushTimeout(data []byte, sec, nsec int) error {
    if libsoftbus == nil {
        return errors.New("C SoftBus Handle Is Nil")
    }
 
    r := C.wrap_fn_queue_push_timeout(libsoftbus, q.shmqueue, unsafe.Pointer(&data[0]), C.int(len(data)), C.int(sec), C.int(nsec))
    if r == 0 {
        return nil
    }
    return errors.New("Queue PushTimeout Data Error")
}
 
// Pop from shm queue
func (q *Queue) Pop() ([]byte, error) {
    if libsoftbus == nil {
        return nil, errors.New("C SoftBus Handle Is Nil")
    }
 
    var data unsafe.Pointer
    var size C.int
    r := C.wrap_fn_queue_pop(libsoftbus, q.shmqueue, &data, &size)
    if r == 0 {
        gd := C.GoBytes(data, size)
        C.wrap_fn_queue_free_poped(libsoftbus, data)
        return gd, nil
    }
    return nil, errors.New("Queue Pop Data Error")
}
 
// PopNoWait from shm queue
func (q *Queue) PopNoWait() ([]byte, error) {
    if libsoftbus == nil {
        return nil, errors.New("C SoftBus Handle Is Nil")
    }
 
    var data unsafe.Pointer
    var size C.int
    r := C.wrap_fn_queue_pop_nowait(libsoftbus, q.shmqueue, &data, &size)
    if r == 0 {
        gd := C.GoBytes(data, size)
        C.wrap_fn_queue_free_poped(libsoftbus, data)
        return gd, nil
    }
    return nil, errors.New("Queue Pop Data Error")
}
 
// PopTimeout from shm queue
func (q *Queue) PopTimeout(sec, nsec int) ([]byte, error) {
    if libsoftbus == nil {
        return nil, errors.New("C SoftBus Handle Is Nil")
    }
 
    var data unsafe.Pointer
    var size C.int
    r := C.wrap_fn_queue_pop_timeout(libsoftbus, q.shmqueue, &data, &size, C.int(sec), C.int(nsec))
    if r == 0 {
        gd := C.GoBytes(data, size)
        C.wrap_fn_queue_free_poped(libsoftbus, data)
        return gd, nil
    }
    return nil, errors.New("Queue Pop Data Error")
}