New file |
| | |
| | | package bhsgo |
| | | |
| | | /* |
| | | #include "bh_api_go.h" |
| | | |
| | | */ |
| | | // #cgo LDFLAGS: -L/home/lichao/code/shmsg/build/lib -L/usr/local/lib -lbhome_shmq -lstdc++ -lpthread -lrt |
| | | import "C" |
| | | |
| | | import ( |
| | | "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 Register(proc *bh.ProcInfo, reply *bh.MsgCommonReply, timeout_ms int) bool { |
| | | data, _ := proc.Marshal() |
| | | creply := unsafe.Pointer(nil) |
| | | creply_len := C.int(0) |
| | | defer C.BHFree(creply, creply_len) |
| | | r := C.BHRegister(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 RegisterTopics(topics *bh.MsgTopicList, reply *bh.MsgCommonReply, timeout_ms int) bool { |
| | | data, _ := topics.Marshal() |
| | | creply := unsafe.Pointer(nil) |
| | | creply_len := C.int(0) |
| | | defer C.BHFree(creply, creply_len) |
| | | r := C.BHRegisterTopics(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 Subscribe(topics *bh.MsgTopicList, reply *bh.MsgCommonReply, timeout_ms int) bool { |
| | | data, _ := topics.Marshal() |
| | | creply := unsafe.Pointer(nil) |
| | | creply_len := C.int(0) |
| | | defer C.BHFree(creply, creply_len) |
| | | r := C.BHSubscribeTopics(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 SubscribeNet(topics *bh.MsgTopicList, reply *bh.MsgCommonReply, timeout_ms int) bool { |
| | | data, _ := topics.Marshal() |
| | | creply := unsafe.Pointer(nil) |
| | | creply_len := C.int(0) |
| | | defer C.BHFree(creply, creply_len) |
| | | r := C.BHSubscribeNetTopics(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 Heartbeat(proc *bh.ProcInfo, reply *bh.MsgCommonReply, timeout_ms int) bool { |
| | | data, _ := proc.Marshal() |
| | | creply := unsafe.Pointer(nil) |
| | | creply_len := C.int(0) |
| | | defer C.BHFree(creply, creply_len) |
| | | r := C.BHHeartbeat(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 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() |
| | | creply := unsafe.Pointer(nil) |
| | | creply_len := C.int(0) |
| | | defer C.BHFree(creply, creply_len) |
| | | r := C.BHUnregister(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 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 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) |
| | | defer C.BHFree(creply, creply_len) |
| | | 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)) |
| | | } |
| | | 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 { |
| | | 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 |
| | | *proc_id = string(C.GoBytes(cpid, cpid_len)) |
| | | pub.Unmarshal(C.GoBytes(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() |
| | | creply := unsafe.Pointer(nil) |
| | | creply_len := C.int(0) |
| | | defer C.BHFree(creply, creply_len) |
| | | 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(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) |
| | | defer C.BHFree(creply, creply_len) |
| | | 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)) |
| | | } |
| | | return r |
| | | } |
| | | |
| | | 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 |
| | | if r { |
| | | *proc_id = string(C.GoBytes(cpid, cpid_len)) |
| | | req.Unmarshal(C.GoBytes(creply, creply_len)) |
| | | } |
| | | return r |
| | | } |
| | | |
| | | func SendReply(src unsafe.Pointer, rep *bh.MsgRequestTopicReply) bool { |
| | | data, _ := rep.Marshal() |
| | | 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) |
| | | return int(r), string(C.GoBytes(creply, creply_len)) |
| | | |
| | | } |
| | | |
| | | func Cleanup() { |
| | | C.BHCleanup() |
| | | } |