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
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)
}
 
// 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
}
 
// 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
}
 
// 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)
}
 
// 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)
}
 
// 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)
}