From 77a6c3512a44dfe6540dde71946e6484fe4f173f Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期一, 10 五月 2021 16:05:28 +0800
Subject: [PATCH] test lock code.

---
 api/bhsgo/bhome_node.go |   55 ++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 44 insertions(+), 11 deletions(-)

diff --git a/api/bhsgo/bhome_node.go b/api/bhsgo/bhome_node.go
index c5d4019..6a91f34 100644
--- a/api/bhsgo/bhome_node.go
+++ b/api/bhsgo/bhome_node.go
@@ -8,20 +8,28 @@
 import "C"
 
 import (
-	bh "bhshmq/proto/source/bhome_msg"
 	"unsafe"
+
+	bh "basic.com/valib/bhshmq.git/proto/source/bhome_msg"
 )
+
+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)
 	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
+	r := C.BHApiIn1Out1Proxy(bhfunc, getPtr(&data), 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 {
@@ -39,8 +47,8 @@
 	return bhApiIn1Out1(C.FBHApiIn1Out1(C.BHSubscribeTopics), data, reply, timeout_ms)
 }
 
-func Heartbeat(topics *bh.ProcInfo, reply *bh.MsgCommonReply, timeout_ms int) bool {
-	data, _ := topics.Marshal()
+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)
 }
 
@@ -48,9 +56,28 @@
 	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)
+	defer C.BHFree(creply, creply_len)
+	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))
+	}
+	return r
+
+}
+
 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 {
@@ -67,19 +94,21 @@
 	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)
 	defer C.BHFree(creply, creply_len)
-	r := C.BHAsyncRequest(unsafe.Pointer(&data[0]), C.int(len(data)), &creply, &creply_len) > 0
+	r := C.BHAsyncRequest(getPtr(&dest), C.int(len(dest)), getPtr(&data), 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)
@@ -87,7 +116,7 @@
 	creply := unsafe.Pointer(nil)
 	creply_len := C.int(0)
 	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
+	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))
@@ -112,7 +141,7 @@
 
 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) {
@@ -124,6 +153,10 @@
 
 }
 
+func Cleanup() {
+	C.BHCleanup()
+}
+
 type ServerCB func(src unsafe.Pointer, proc_id *string, req *bh.MsgRequestTopic)
 type ClientCB func(proc_id *string, msg_id *[]byte, reply *bh.MsgRequestTopicReply)
 type SubDataCB func(proc_id *string, pub *bh.MsgPublish)

--
Gitblit v1.8.0