zhangmeng
2024-04-09 2561a007b8d8999a4750046d0cfb3b1ad5af50ac
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
package bhsgo
 
/*
#include "../../src/bh_api.h"
 
*/
// #cgo LDFLAGS: -L/usr/local/lib -lshm_queue -lstdc++ -lpthread -lrt
import "C"
 
import (
    "unsafe"
 
    bh "basic.com/valib/c_bhomebus.git/proto/source/bhome_msg"
)
 
func unsafeGetBytes(p *unsafe.Pointer, s int) []byte {
    if *p == nil || s == 0 {
        return nil
    }
    return (*[0x7fffffff]byte)(*p)[:s:s]
}
 
func ByteSlice2String(bs []byte) string {
    return *(*string)(unsafe.Pointer(&bs))
}
 
/*func unsafeGetBytes(s string) []byte {
    return (*[0x7fff0000]byte)(unsafe.Pointer(
        (*reflect.StringHeader)(unsafe.Pointer(&s)).Data),
    )[:len(s):len(s)]
}*/
 
func getPtr(n *[]byte) unsafe.Pointer {
    if len(*n) > 0 {
        return unsafe.Pointer(&((*n)[0]))
    } else {
        return unsafe.Pointer(nil)
    }
}
 
func Register(proc *bh.ProcInfo, reply *bh.MsgCommonReply, timeout_ms int) bool {
    data, _ := proc.Marshal()
    var creply unsafe.Pointer
    var creply_len C.int
 
    r := C.BHRegister(getPtr(&data), C.int(len(data)), &creply, &creply_len, C.int(timeout_ms)) > 0
    defer C.BHFree(creply, creply_len)
    if r {
        // reply.Unmarshal(C.GoBytes(creply, creply_len))
        reply.Unmarshal(unsafeGetBytes(&creply, int(creply_len)))
    }
    return r
}
 
func RegisterTopics(topics *bh.MsgTopicList, reply *bh.MsgCommonReply, timeout_ms int) bool {
    data, _ := topics.Marshal()
    var creply unsafe.Pointer
    var creply_len C.int
 
    r := C.BHRegisterTopics(getPtr(&data), C.int(len(data)), &creply, &creply_len, C.int(timeout_ms)) > 0
    defer C.BHFree(creply, creply_len)
    if r {
        // reply.Unmarshal(C.GoBytes(creply, creply_len))
        reply.Unmarshal(unsafeGetBytes(&creply, int(creply_len)))
    }
    return r
}
 
func Subscribe(topics *bh.MsgTopicList, reply *bh.MsgCommonReply, timeout_ms int) bool {
    data, _ := topics.Marshal()
    var creply unsafe.Pointer
    var creply_len C.int
 
    r := C.BHSubscribeTopics(getPtr(&data), C.int(len(data)), &creply, &creply_len, C.int(timeout_ms)) > 0
    defer C.BHFree(creply, creply_len)
    if r {
        // reply.Unmarshal(C.GoBytes(creply, creply_len))
        d := unsafeGetBytes(&creply, int(creply_len))
        reply.Unmarshal(d)
    }
    return r
}
 
func SubscribeNet(topics *bh.MsgTopicList, reply *bh.MsgCommonReply, timeout_ms int) bool {
    data, _ := topics.Marshal()
    var creply unsafe.Pointer
    var creply_len C.int
 
    r := C.BHSubscribeNetTopics(getPtr(&data), C.int(len(data)), &creply, &creply_len, C.int(timeout_ms)) > 0
    defer C.BHFree(creply, creply_len)
    if r {
        // reply.Unmarshal(C.GoBytes(creply, creply_len))
        reply.Unmarshal(unsafeGetBytes(&creply, int(creply_len)))
    }
    return r
}
 
func Heartbeat(proc *bh.ProcInfo, reply *bh.MsgCommonReply, timeout_ms int) bool {
    data, _ := proc.Marshal()
    var creply unsafe.Pointer
    var creply_len C.int
 
    r := C.BHHeartbeat(getPtr(&data), C.int(len(data)), &creply, &creply_len, C.int(timeout_ms)) > 0
    defer C.BHFree(creply, creply_len)
    if r {
        // reply.Unmarshal(C.GoBytes(creply, creply_len))
        reply.Unmarshal(unsafeGetBytes(&creply, int(creply_len)))
    }
    return r
}
 
func HeartbeatEasy(timeout_ms int) bool {
    return C.BHHeartbeatEasy(C.int(timeout_ms)) > 0
}
 
func Unregister(proc *bh.ProcInfo, reply *bh.MsgCommonReply, timeout_ms int) bool {
    data, _ := proc.Marshal()
    var creply unsafe.Pointer
    var creply_len C.int
 
    r := C.BHUnregister(getPtr(&data), C.int(len(data)), &creply, &creply_len, C.int(timeout_ms)) > 0
    defer C.BHFree(creply, creply_len)
    if r {
        // reply.Unmarshal(C.GoBytes(creply, creply_len))
        reply.Unmarshal(unsafeGetBytes(&creply, int(creply_len)))
    }
    return r
}
 
func QueryTopicAddress(dest_addr *bh.BHAddress, topic *bh.MsgQueryTopic, reply *bh.MsgQueryTopicReply, timeout_ms int) bool {
    dest, _ := dest_addr.Marshal()
    data, _ := topic.Marshal()
    var creply unsafe.Pointer
    var creply_len C.int
 
    r := C.BHQueryTopicAddress(getPtr(&dest), C.int(len(dest)), getPtr(&data), C.int(len(data)), &creply, &creply_len, C.int(timeout_ms)) > 0
    if r {
        // reply.Unmarshal(C.GoBytes(creply, creply_len))
        reply.Unmarshal(unsafeGetBytes(&creply, int(creply_len)))
    }
    C.BHFree(creply, creply_len)
    return r
 
}
 
func QueryProcs(dest_addr *bh.BHAddress, topic *bh.MsgQueryProc, reply *bh.MsgQueryProcReply, timeout_ms int) bool {
    dest, _ := dest_addr.Marshal()
    data, _ := topic.Marshal()
    var creply unsafe.Pointer
    var creply_len C.int
 
    r := C.BHQueryProcs(getPtr(&dest), C.int(len(dest)), getPtr(&data), C.int(len(data)), &creply, &creply_len, C.int(timeout_ms)) > 0
    if r {
        // reply.Unmarshal(C.GoBytes(creply, creply_len))
        reply.Unmarshal(unsafeGetBytes(&creply, int(creply_len)))
    }
    C.BHFree(creply, creply_len)
    return r
 
}
 
func Publish(pub *bh.MsgPublish, timeout_ms int) bool {
    data, _ := pub.Marshal()
    return C.BHPublish(getPtr(&data), C.int(len(data)), C.int(timeout_ms)) > 0
}
 
func ReadSub(proc_id *string, pub *bh.MsgPublish, timeout_ms int) bool {
    var cpid, creply unsafe.Pointer
    var cpid_len, creply_len C.int
 
    r := C.BHReadSub(&cpid, &cpid_len, &creply, &creply_len, C.int(timeout_ms)) > 0
    if r {
        *proc_id = string(unsafeGetBytes(&cpid, int(cpid_len)))
        pub.Unmarshal(unsafeGetBytes(&creply, int(creply_len)))
    }
    C.BHFree(cpid, cpid_len)
    C.BHFree(creply, creply_len)
 
    return r
}
 
func AsyncRequest(dest_addr *bh.BHAddress, req *bh.MsgRequestTopic, msg_id *[]byte) bool {
    dest, _ := dest_addr.Marshal()
    data, _ := req.Marshal()
    var creply unsafe.Pointer
    var creply_len C.int
 
    r := C.BHAsyncRequest(getPtr(&dest), C.int(len(dest)), getPtr(&data), C.int(len(data)), &creply, &creply_len) > 0
    defer C.BHFree(creply, creply_len)
    if r {
        *msg_id = C.GoBytes(creply, creply_len)
    }
    return r
}
 
func Request(dest_addr *bh.BHAddress, req *bh.MsgRequestTopic, proc_id *string, reply *bh.MsgRequestTopicReply, timeout_ms int) bool {
    dest, _ := dest_addr.Marshal()
    data, _ := req.Marshal()
    var cpid, creply unsafe.Pointer
    var cpid_len, creply_len C.int
 
    r := C.BHRequest(getPtr(&dest), C.int(len(dest)), getPtr(&data), C.int(len(data)), &cpid, &cpid_len, &creply, &creply_len, C.int(timeout_ms)) > 0
    if r {
        // *proc_id = string(C.GoBytes(cpid, cpid_len))
        // reply.Unmarshal(C.GoBytes(creply, creply_len))
        *proc_id = string(unsafeGetBytes(&cpid, int(cpid_len)))
        reply.Unmarshal(unsafeGetBytes(&creply, int(creply_len)))
    }
    C.BHFree(cpid, cpid_len)
    C.BHFree(creply, creply_len)
    return r
}
 
func ReadRequest(proc_id *string, req *bh.MsgRequestTopic, psrc *unsafe.Pointer, timeout_ms int) bool {
    var cpid, creply unsafe.Pointer
    var cpid_len, creply_len C.int
 
    r := C.BHReadRequest(&cpid, &cpid_len, &creply, &creply_len, psrc, C.int(timeout_ms)) > 0
    if r {
        // *proc_id = string(C.GoBytes(cpid, cpid_len))
        // req.Unmarshal(C.GoBytes(creply, creply_len))
        *proc_id = string(unsafeGetBytes(&cpid, int(cpid_len)))
        req.Unmarshal(unsafeGetBytes(&creply, int(creply_len)))
    }
    C.BHFree(cpid, cpid_len)
    C.BHFree(creply, creply_len)
    return r
}
 
func SendReply(src unsafe.Pointer, rep *bh.MsgRequestTopicReply) bool {
    data, _ := rep.Marshal()
    ret := C.BHSendReply(src, getPtr(&data), C.int(len(data))) > 0
    C.BHFree(src, 0)
    return ret
}
 
func GetLastError() (int, string) {
    var creply unsafe.Pointer
    var creply_len C.int
 
    r := C.BHGetLastError(&creply, &creply_len)
    defer C.BHFree(creply, creply_len)
    // return int(r), string(C.GoBytes(creply, creply_len))
    return int(r), string(unsafeGetBytes(&creply, int(creply_len)))
 
}
 
func Cleanup() {
    C.BHCleanup()
}
 
// ///////////////////////////////////////////////////
func Request2(dest_addr *bh.BHAddress, data []byte, proc_id *string, replyPtr *unsafe.Pointer, replyLen *int, timeout_ms int) bool {
    dest, _ := dest_addr.Marshal()
    var cpid, creply unsafe.Pointer
    var cpid_len, creply_len C.int
 
    r := C.BHRequest(getPtr(&dest), C.int(len(dest)), getPtr(&data), C.int(len(data)), &cpid, &cpid_len, &creply, &creply_len, C.int(timeout_ms)) > 0
    if r {
        *proc_id = string(unsafeGetBytes(&cpid, int(cpid_len)))
        *replyPtr = creply
        *replyLen = int(creply_len)
    }
    C.BHFree(cpid, cpid_len)
    return r
}
func CFree(p unsafe.Pointer, size int) {
    if p != nil {
        C.BHFree(p, C.int(size))
    }
}
func SendReply2(src unsafe.Pointer, data []byte) bool {
    return C.BHSendReply(src, getPtr(&data), C.int(len(data))) > 0
}