From 13c503b73b4ecc8ce4a6e344f9ac15202985d686 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期二, 20 七月 2021 19:48:58 +0800
Subject: [PATCH] fix memory leak
---
api/bhsgo/bhome_node.go | 170 ++++++++++++++++++++++++++++++++++++--------------------
1 files changed, 109 insertions(+), 61 deletions(-)
diff --git a/api/bhsgo/bhome_node.go b/api/bhsgo/bhome_node.go
index dcc8248..3fbb294 100644
--- a/api/bhsgo/bhome_node.go
+++ b/api/bhsgo/bhome_node.go
@@ -4,20 +4,76 @@
#include "bh_api_go.h"
*/
-// #cgo LDFLAGS: -L/home/lichao/code/shmsg/build/lib -L/usr/local/lib -lbhome_shmq -lbhome_msg -lprotobuf-lite -lstdc++ -lpthread -lrt
+// #cgo LDFLAGS: -L/home/lichao/code/shmsg/build/lib -L/usr/local/lib -lbhome_shmq -lstdc++ -lpthread -lrt
import "C"
import (
- bh "bhshmq/proto/source/bhome_msg"
- "fmt"
"unsafe"
+
+ bh "basic.com/valib/bhshmq.git/proto/source/bhome_msg"
)
-func BHApiIn1Out1(bhfunc C.FBHApiIn1Out1, data []byte, reply *bh.MsgCommonReply, timeout_ms int) bool {
+func getPtr(n *[]byte) unsafe.Pointer {
+ if len(*n) > 0 {
+ return unsafe.Pointer(&((*n)[0]))
+ } else {
+ return unsafe.Pointer(nil)
+ }
+}
+
+func bhApiIn1Out1(bhfunc C.FBHApiIn1Out1, data []byte, reply *bh.MsgCommonReply, timeout_ms int) bool {
creply := unsafe.Pointer(nil)
creply_len := C.int(0)
+
+ r := C.BHApiIn1Out1Proxy(bhfunc, getPtr(&data), C.int(len(data)), &creply, &creply_len, C.int(timeout_ms)) > 0
defer C.BHFree(creply, creply_len)
- r := C.BHApiIn1Out1Proxy(bhfunc, unsafe.Pointer(&data[0]), C.int(len(data)), &creply, &creply_len, C.int(timeout_ms)) > 0
+ if r {
+ reply.Unmarshal(C.GoBytes(creply, creply_len))
+ }
+ return r
+}
+
+func Register(proc *bh.ProcInfo, reply *bh.MsgCommonReply, timeout_ms int) bool {
+ data, _ := proc.Marshal()
+ return bhApiIn1Out1(C.FBHApiIn1Out1(C.BHRegister), data, reply, timeout_ms)
+}
+
+func RegisterTopics(topics *bh.MsgTopicList, reply *bh.MsgCommonReply, timeout_ms int) bool {
+ data, _ := topics.Marshal()
+ return bhApiIn1Out1(C.FBHApiIn1Out1(C.BHRegisterTopics), data, reply, timeout_ms)
+}
+
+func Subscribe(topics *bh.MsgTopicList, reply *bh.MsgCommonReply, timeout_ms int) bool {
+ data, _ := topics.Marshal()
+ return bhApiIn1Out1(C.FBHApiIn1Out1(C.BHSubscribeTopics), data, reply, timeout_ms)
+}
+
+func SubscribeNet(topics *bh.MsgTopicList, reply *bh.MsgCommonReply, timeout_ms int) bool {
+ data, _ := topics.Marshal()
+ return bhApiIn1Out1(C.FBHApiIn1Out1(C.BHSubscribeNetTopics), data, reply, timeout_ms)
+}
+
+func Heartbeat(proc *bh.ProcInfo, reply *bh.MsgCommonReply, timeout_ms int) bool {
+ data, _ := proc.Marshal()
+ return bhApiIn1Out1(C.FBHApiIn1Out1(C.BHHeartbeat), data, reply, timeout_ms)
+}
+
+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()
+ return bhApiIn1Out1(C.FBHApiIn1Out1(C.BHUnregister), data, reply, timeout_ms)
+}
+
+func QueryTopicAddress(dest_addr *bh.BHAddress, topic *bh.MsgQueryTopic, reply *bh.MsgQueryTopicReply, timeout_ms int) bool {
+ dest, _ := dest_addr.Marshal()
+ data, _ := topic.Marshal()
+ creply := unsafe.Pointer(nil)
+ creply_len := C.int(0)
+ r := C.BHQueryTopicAddress(getPtr(&dest), C.int(len(dest)), 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))
}
@@ -25,70 +81,69 @@
}
-func Register(proc *bh.ProcInfo, reply *bh.MsgCommonReply, timeout_ms int) bool {
- data, _ := proc.Marshal()
- return BHApiIn1Out1(C.FBHApiIn1Out1(C.BHRegister), data, reply, timeout_ms)
-}
+func QueryProcs(dest_addr *bh.BHAddress, topic *bh.MsgQueryProc, reply *bh.MsgQueryProcReply, timeout_ms int) bool {
+ dest, _ := dest_addr.Marshal()
+ data, _ := topic.Marshal()
+ creply := unsafe.Pointer(nil)
+ creply_len := C.int(0)
+ r := C.BHQueryProcs(getPtr(&dest), C.int(len(dest)), 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))
+ }
+ return r
-func RegisterTopics(topics *bh.MsgTopicList, reply *bh.MsgCommonReply, timeout_ms int) bool {
- data, _ := topics.Marshal()
- return BHApiIn1Out1(C.FBHApiIn1Out1(C.BHRegisterTopics), data, reply, timeout_ms)
-}
-
-func Subscribe(topics *bh.MsgTopicList, reply *bh.MsgCommonReply, timeout_ms int) bool {
- data, _ := topics.Marshal()
- return BHApiIn1Out1(C.FBHApiIn1Out1(C.BHSubscribeTopics), data, reply, timeout_ms)
-}
-
-func Heartbeat(topics *bh.MsgTopicList, reply *bh.MsgCommonReply, timeout_ms int) bool {
- data, _ := topics.Marshal()
- return BHApiIn1Out1(C.FBHApiIn1Out1(C.BHHeartbeat), data, reply, timeout_ms)
-}
-
-func HeartbeatEasy(timeout_ms int) bool {
- return C.BHHeartbeatEasy(C.int(timeout_ms)) > 0
}
func Publish(pub *bh.MsgPublish, timeout_ms int) bool {
data, _ := pub.Marshal()
- return C.BHPublish(unsafe.Pointer(&data[0]), C.int(len(data)), C.int(timeout_ms)) > 0
+ 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 {
cpid := unsafe.Pointer(nil)
cpid_len := C.int(0)
- defer C.BHFree(cpid, cpid_len)
+
creply := unsafe.Pointer(nil)
creply_len := C.int(0)
- defer C.BHFree(creply, creply_len)
r := C.BHReadSub(&cpid, &cpid_len, &creply, &creply_len, C.int(timeout_ms)) > 0
+ defer C.BHFree(cpid, cpid_len)
+ defer C.BHFree(creply, creply_len)
+
*proc_id = string(C.GoBytes(cpid, cpid_len))
pub.Unmarshal(C.GoBytes(creply, creply_len))
return r
}
-func AsyncRequest(req *bh.MsgRequestTopic, msg_id *[]byte) bool {
+func AsyncRequest(dest_addr *bh.BHAddress, req *bh.MsgRequestTopic, msg_id *[]byte) bool {
+ dest, _ := dest_addr.Marshal()
data, _ := req.Marshal()
creply := unsafe.Pointer(nil)
creply_len := C.int(0)
+
+ 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)
- r := C.BHAsyncRequest(unsafe.Pointer(&data[0]), C.int(len(data)), &creply, &creply_len) > 0
+
if r {
*msg_id = C.GoBytes(creply, creply_len)
}
return r
}
-func Request(req *bh.MsgRequestTopic, proc_id *string, reply *bh.MsgRequestTopicReply, timeout_ms int) bool {
+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()
cpid := unsafe.Pointer(nil)
cpid_len := C.int(0)
- defer C.BHFree(cpid, cpid_len)
+
creply := unsafe.Pointer(nil)
creply_len := C.int(0)
+
+ 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
+
+ defer C.BHFree(cpid, cpid_len)
defer C.BHFree(creply, creply_len)
- r := C.BHRequest(unsafe.Pointer(&data[0]), 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))
@@ -99,11 +154,15 @@
func ReadRequest(proc_id *string, req *bh.MsgRequestTopic, psrc *unsafe.Pointer, timeout_ms int) bool {
cpid := unsafe.Pointer(nil)
cpid_len := C.int(0)
- defer C.BHFree(cpid, cpid_len)
+
creply := unsafe.Pointer(nil)
creply_len := C.int(0)
- defer C.BHFree(creply, creply_len)
+
r := C.BHReadRequest(&cpid, &cpid_len, &creply, &creply_len, psrc, C.int(timeout_ms)) > 0
+
+ defer C.BHFree(cpid, cpid_len)
+ defer C.BHFree(creply, creply_len)
+
if r {
*proc_id = string(C.GoBytes(cpid, cpid_len))
req.Unmarshal(C.GoBytes(creply, creply_len))
@@ -113,16 +172,22 @@
func SendReply(src unsafe.Pointer, rep *bh.MsgRequestTopicReply) bool {
data, _ := rep.Marshal()
- return C.BHSendReply(src, unsafe.Pointer(&data[0]), C.int(len(data))) > 0
+ return C.BHSendReply(src, getPtr(&data), C.int(len(data))) > 0
}
func GetLastError() (int, string) {
creply := unsafe.Pointer(nil)
creply_len := C.int(0)
- defer C.BHFree(creply, creply_len)
+
r := C.BHGetLastError(&creply, &creply_len)
+ defer C.BHFree(creply, creply_len)
+
return int(r), string(C.GoBytes(creply, creply_len))
+}
+
+func Cleanup() {
+ C.BHCleanup()
}
type ServerCB func(src unsafe.Pointer, proc_id *string, req *bh.MsgRequestTopic)
@@ -133,24 +198,24 @@
var cgoClientCB ClientCB
var cgoSubDataCB SubDataCB
-//export CGoSubDataCallback
-func CGoSubDataCallback(cpid C.PCVoid, pid_len C.int, data C.PCVoid, data_len C.int) {
+//export cgoSubDataCallback
+func cgoSubDataCallback(cpid C.PCVoid, pid_len C.int, data C.PCVoid, data_len C.int) {
proc_id := string(C.GoBytes(unsafe.Pointer(cpid), pid_len))
msg := bh.MsgPublish{}
msg.Unmarshal(C.GoBytes(unsafe.Pointer(data), data_len))
cgoSubDataCB(&proc_id, &msg)
}
-//export CGoServerCallback
-func CGoServerCallback(cpid C.PCVoid, pid_len C.int, data C.PCVoid, data_len C.int, src unsafe.Pointer) {
+//export cgoServerCallback
+func cgoServerCallback(cpid C.PCVoid, pid_len C.int, data C.PCVoid, data_len C.int, src unsafe.Pointer) {
proc_id := string(C.GoBytes(unsafe.Pointer(cpid), pid_len))
msg := bh.MsgRequestTopic{}
msg.Unmarshal(C.GoBytes(unsafe.Pointer(data), data_len))
cgoServerCB(src, &proc_id, &msg)
}
-//export CGoClientCallback
-func CGoClientCallback(cpid C.PCVoid, pid_len C.int, msgid C.PCVoid, msgid_len C.int, data C.PCVoid, data_len C.int) {
+//export cgoClientCallback
+func cgoClientCallback(cpid C.PCVoid, pid_len C.int, msgid C.PCVoid, msgid_len C.int, data C.PCVoid, data_len C.int) {
proc_id := string(C.GoBytes(unsafe.Pointer(cpid), pid_len))
msg_id := C.GoBytes(unsafe.Pointer(msgid), msgid_len)
var msg bh.MsgRequestTopicReply
@@ -164,20 +229,3 @@
cgoSubDataCB = sub
C.CGoStartWorker()
}
-
-/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// user code:
-
-func ServerCallback(src unsafe.Pointer, proc_id *string, req *bh.MsgRequestTopic) {
- fmt.Println("user server cb called, request topic: " + string(req.Topic) + ", data:" + string(req.Data))
- reply := bh.MsgRequestTopicReply{}
- reply.Data = []byte("reply 1234")
- SendReply(src, &reply)
-}
-
-func SubDataCallback(proc_id *string, pub *bh.MsgPublish) {
- fmt.Println("user sub data cb called")
-}
-func ClientCallback(proc_id *string, msg_id *[]byte, reply *bh.MsgRequestTopicReply) {
- fmt.Println("user client cb reply: " + string(reply.Data))
-}
\ No newline at end of file
--
Gitblit v1.8.0