zhangmeng
2020-08-03 0bde715af72b7b3d55ad3aac816d7cd153a60b42
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
package softbus
 
/*
#include <stdlib.h>
#include "libcsoftbus.h"
*/
import "C"
import (
    "fmt"
    "unsafe"
)
 
// DgramSocket dgram
type DgramSocket struct {
    dgram unsafe.Pointer
}
 
// OpenDgramSocket dgram
func OpenDgramSocket() *DgramSocket {
    if libsoftbus == nil {
        return nil
    }
 
    d := C.wrap_fn_dgram_socket_open(libsoftbus)
    if d == nil {
        return nil
    }
 
    return &DgramSocket{
        dgram: d,
    }
}
 
// Close close dgram socket
func (d *DgramSocket) Close() int {
    if libsoftbus != nil && d.dgram != nil {
        r := C.wrap_fn_dgram_socket_close(libsoftbus, d.dgram)
        return int(r)
    }
 
    return RETVAL
}
 
// Bind bind
func (d *DgramSocket) Bind(port int) int {
    if libsoftbus == nil {
        return RETVAL
    }
 
    r := C.wrap_fn_dgram_socket_bind(libsoftbus, d.dgram, C.int(port))
    return int(r)
}
 
// ForceBind bind
func (d *DgramSocket) ForceBind(port int) int {
    if libsoftbus == nil {
        return RETVAL
    }
 
    r := C.wrap_fn_dgram_socket_force_bind(libsoftbus, d.dgram, C.int(port))
    return int(r)
}
 
// SendTo port
func (d *DgramSocket) SendTo(data []byte, port int) int {
    if libsoftbus == nil {
        return RETVAL
    }
 
    r := C.wrap_fn_dgram_socket_sendto(libsoftbus, d.dgram, unsafe.Pointer(&data[0]), C.int(len(data)), C.int(port))
    return int(r)
}
 
// SendToTimeout port
func (d *DgramSocket) SendToTimeout(data []byte, port int, sec, usec int) int {
    if libsoftbus == nil {
        return RETVAL
    }
 
    r := C.wrap_fn_dgram_socket_sendto_timeout(libsoftbus, d.dgram, unsafe.Pointer(&data[0]), C.int(len(data)), C.int(port), C.int(sec), C.int(usec))
    return int(r)
}
 
// SendToNoWait port
func (d *DgramSocket) SendToNoWait(data []byte, port int) int {
    if libsoftbus == nil {
        return RETVAL
    }
 
    r := C.wrap_fn_dgram_socket_sendto_nowait(libsoftbus, d.dgram, unsafe.Pointer(&data[0]), C.int(len(data)), C.int(port))
    return int(r)
}
 
// RecvFrom data and port
func (d *DgramSocket) RecvFrom() ([]byte, int, error) {
    if libsoftbus == nil {
        return nil, -1, fmt.Errorf("RecvFrom Func Test libsoftbus Is Nil")
    }
 
    var rb unsafe.Pointer
    var rs C.int
    var rp C.int
 
    r := C.wrap_fn_dgram_socket_recvfrom(libsoftbus, d.dgram, &rb, &rs, &rp)
    if r != 0 {
        return nil, int(rp), fmt.Errorf("RecvFrom Func Failed %d", int(r))
    }
 
    data := C.GoBytes(rb, rs)
    C.wrap_fn_dgram_socket_free(libsoftbus, rb)
 
    return data, int(rp), nil
}
 
// RecvFromTimeout data and port
func (d *DgramSocket) RecvFromTimeout(sec, usec int) ([]byte, int, error) {
    if libsoftbus == nil {
        return nil, -1, fmt.Errorf("RecvFromTimeout Func Test libsoftbus Is Nil")
    }
 
    var rb unsafe.Pointer
    var rs C.int
    var rp C.int
 
    r := C.wrap_fn_dgram_socket_recvfrom_timeout(libsoftbus, d.dgram, &rb, &rs, &rp, C.int(sec), C.int(usec))
    if r != 0 {
        return nil, int(rp), fmt.Errorf("RecvFromTimeout Func Failed %d", int(r))
    }
 
    data := C.GoBytes(rb, rs)
    C.wrap_fn_dgram_socket_free(libsoftbus, rb)
 
    return data, int(rp), nil
}
 
// RecvFromNoWait data and port
func (d *DgramSocket) RecvFromNoWait() ([]byte, int, error) {
    if libsoftbus == nil {
        return nil, -1, fmt.Errorf("RecvFromNoWait Func Test libsoftbus Is Nil")
    }
 
    var rb unsafe.Pointer
    var rs C.int
    var rp C.int
 
    r := C.wrap_fn_dgram_socket_recvfrom_nowait(libsoftbus, d.dgram, &rb, &rs, &rp)
    if r != 0 {
        return nil, int(rp), fmt.Errorf("RecvFromNoWait Func Failed %d", int(r))
    }
 
    data := C.GoBytes(rb, rs)
    C.wrap_fn_dgram_socket_free(libsoftbus, rb)
 
    return data, int(rp), nil
}
 
// SendAndRecv sync
func (d *DgramSocket) SendAndRecv(sdata []byte, port int) ([]byte, error) {
    if libsoftbus == nil {
        return nil, fmt.Errorf("SendAndRecv Func Test libsoftbus Is Nil")
    }
 
    var rb unsafe.Pointer
    var rs C.int
 
    r := C.wrap_fn_dgram_socket_sendandrecv(libsoftbus, d.dgram, unsafe.Pointer(&sdata[0]), C.int(len(sdata)), C.int(port), &rb, &rs)
    if r != 0 {
        return nil, fmt.Errorf("SendAndRecv Send To %d And Recv From It Failed", port)
    }
 
    data := C.GoBytes(rb, rs)
    C.wrap_fn_dgram_socket_free(libsoftbus, rb)
 
    return data, nil
}
 
// SendAndRecvTimeout sync
func (d *DgramSocket) SendAndRecvTimeout(sdata []byte, port int, sec, usec int) ([]byte, error) {
    if libsoftbus == nil {
        return nil, fmt.Errorf("SendAndRecvTimeout Func Test libsoftbus Is Nil")
    }
 
    var rb unsafe.Pointer
    var rs C.int
 
    r := C.wrap_fn_dgram_socket_sendandrecv_timeout(libsoftbus, d.dgram, unsafe.Pointer(&sdata[0]), C.int(len(sdata)), C.int(port), &rb, &rs, C.int(sec), C.int(usec))
    if r != 0 {
        return nil, fmt.Errorf("SendAndRecvTimeout Send To %d And Recv From It Failed", port)
    }
 
    data := C.GoBytes(rb, rs)
    C.wrap_fn_dgram_socket_free(libsoftbus, rb)
 
    return data, nil
}
 
// SendAndRecvNoWait sync
func (d *DgramSocket) SendAndRecvNoWait(sdata []byte, port int) ([]byte, error) {
    if libsoftbus == nil {
        return nil, fmt.Errorf("SendAndRecvNoWait Func Test libsoftbus Is Nil")
    }
 
    var rb unsafe.Pointer
    var rs C.int
 
    r := C.wrap_fn_dgram_socket_sendandrecv_nowait(libsoftbus, d.dgram, unsafe.Pointer(&sdata[0]), C.int(len(sdata)), C.int(port), &rb, &rs)
    if r != 0 {
        return nil, fmt.Errorf("SendAndRecvNoWait Send To %d And Recv From It Failed", port)
    }
 
    data := C.GoBytes(rb, rs)
    C.wrap_fn_dgram_socket_free(libsoftbus, rb)
 
    return data, nil
}
 
// StartBus bus
func (d *DgramSocket) StartBus() int {
    if libsoftbus == nil {
        return RETVAL
    }
 
    r := C.wrap_fn_dgram_socket_start_bus(libsoftbus, d.dgram)
    return int(r)
}
 
// Sub sub bus
func (d *DgramSocket) Sub(topic string, port int) int {
    if libsoftbus == nil {
        return RETVAL
    }
 
    ct := C.CString(topic)
    defer C.free(unsafe.Pointer(ct))
 
    r := C.wrap_fn_dgram_socket_sub(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), C.int(port))
    return int(r)
}
 
// SubTimeout sub bus
func (d *DgramSocket) SubTimeout(topic string, port int, sec, usec int) int {
    if libsoftbus == nil {
        return RETVAL
    }
 
    ct := C.CString(topic)
    defer C.free(unsafe.Pointer(ct))
 
    r := C.wrap_fn_dgram_socket_sub_timeout(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), C.int(port), C.int(sec), C.int(usec))
    return int(r)
}
 
// SubNoWait sub bus
func (d *DgramSocket) SubNoWait(topic string, port int) int {
    if libsoftbus == nil {
        return RETVAL
    }
 
    ct := C.CString(topic)
    defer C.free(unsafe.Pointer(ct))
 
    r := C.wrap_fn_dgram_socket_sub_nowait(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), C.int(port))
    return int(r)
}
 
// Pub bus
func (d *DgramSocket) Pub(topic, msg string, port int) int {
    if libsoftbus == nil {
        return RETVAL
    }
 
    ct := C.CString(topic)
    defer C.free(unsafe.Pointer(ct))
    cm := C.CString(msg)
    defer C.free(unsafe.Pointer(cm))
 
    r := C.wrap_fn_dgram_socket_pub(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), unsafe.Pointer(cm), C.int(len(msg)), C.int(port))
    return int(r)
}
 
// PubTimeout bus
func (d *DgramSocket) PubTimeout(topic, msg string, port int, sec, usec int) int {
    if libsoftbus == nil {
        return RETVAL
    }
 
    ct := C.CString(topic)
    defer C.free(unsafe.Pointer(ct))
    cm := C.CString(msg)
    defer C.free(unsafe.Pointer(cm))
 
    r := C.wrap_fn_dgram_socket_pub_timeout(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), unsafe.Pointer(cm), C.int(len(msg)), C.int(port), C.int(sec), C.int(usec))
    return int(r)
}
 
// PubNoWait bus
func (d *DgramSocket) PubNoWait(topic, msg string, port int) int {
    if libsoftbus == nil {
        return RETVAL
    }
 
    ct := C.CString(topic)
    defer C.free(unsafe.Pointer(ct))
    cm := C.CString(msg)
    defer C.free(unsafe.Pointer(cm))
 
    r := C.wrap_fn_dgram_socket_pub_nowait(libsoftbus, d.dgram, unsafe.Pointer(ct), C.int(len(topic)), unsafe.Pointer(cm), C.int(len(msg)), C.int(port))
    return int(r)
}
 
// Port socket
func (d *DgramSocket) Port() int {
    if libsoftbus == nil {
        return -1
    }
 
    r := C.wrap_fn_dgram_socket_port(libsoftbus, d.dgram)
    return int(r)
}