Skip to content

Commit

Permalink
fix amop callback
Browse files Browse the repository at this point in the history
  • Loading branch information
bxq2011hust committed Feb 20, 2023
1 parent 97efc99 commit d9217a0
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions bindings/go/csdk/csdk_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ type CallbackChan struct {

//export on_recv_notify_resp_callback
func on_recv_notify_resp_callback(group *C.char, block C.int64_t, context unsafe.Pointer) {
chanData := (*CallbackChan)(unsafe.Pointer(context))
chanData := getContext(context, false)
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, uint64(block))
chanData.Data <- Response{b, nil}
}

//export on_recv_amop_subscribe_resp
func on_recv_amop_subscribe_resp(endpoint *C.char, seq *C.char, resp *C.struct_bcos_sdk_c_struct_response) {
chanData := getContext(resp.context)
chanData := getContext(resp.context, false)
if int(resp.error) != 0 {
chanData.Data <- Response{nil, fmt.Errorf("something is wrong, error: %d, errorMessage: %s", resp.error, C.GoString(resp.desc))}
} else {
Expand All @@ -77,23 +77,23 @@ func on_recv_amop_subscribe_resp(endpoint *C.char, seq *C.char, resp *C.struct_b

//export on_recv_amop_publish_resp
func on_recv_amop_publish_resp(resp *C.struct_bcos_sdk_c_struct_response) {
on_callback(resp)
on_callback(resp, true)
}

//export on_recv_resp_callback
func on_recv_resp_callback(resp *C.struct_bcos_sdk_c_struct_response) {
on_callback(resp)
on_callback(resp, true)
}

//export on_recv_event_resp_callback
func on_recv_event_resp_callback(resp *C.struct_bcos_sdk_c_struct_response) {
on_callback(resp)
on_callback(resp, false)
}

func on_callback(resp *C.struct_bcos_sdk_c_struct_response) {
chanData := getContext(resp.context)
func on_callback(resp *C.struct_bcos_sdk_c_struct_response, delete bool) {
chanData := getContext(resp.context, delete)
if chanData == nil {
chanData.Data <- Response{nil, fmt.Errorf("callback channel is nil")}
panic("callback channel is nil")
return
}
if int(resp.error) != 0 {
Expand All @@ -106,20 +106,25 @@ func on_callback(resp *C.struct_bcos_sdk_c_struct_response) {

func setContext(context *CallbackChan) unsafe.Pointer {
p := fmt.Sprintf("%p", context)
fmt.Printf(">>> setContext index: %s\n", p)
contextCache.Set(p, context, cache.NoExpiration)
parseI, _ := strconv.ParseUint(p, 0, 0)
// i := *(*int)(unsafe.Pointer(context))
return unsafe.Pointer(&parseI)
}

func getContext(index unsafe.Pointer) *CallbackChan {
func getContext(index unsafe.Pointer, delete bool) *CallbackChan {
i := *(*int)(index)
p := fmt.Sprintf("%#x", i)
context, found := contextCache.Get(p)
fmt.Printf("<<< getContext index: %s, found:%v\n", p, found)
if found {
contextCache.Delete(p)
if delete {
contextCache.Delete(p)
}
return context.(*CallbackChan)
}

return nil
}

Expand Down Expand Up @@ -434,6 +439,7 @@ func (csdk *CSDK) BroadcastAmopMsg(chanData *CallbackChan, topic string, data []
func (csdk *CSDK) SubscribeEvent(chanData *CallbackChan, params string) string {
cParams := C.CString(params)
defer C.free(unsafe.Pointer(cParams))
chanData.sdk = csdk.sdk
return C.GoString(C.bcos_event_sub_subscribe_event(csdk.sdk, csdk.groupID, cParams, C.bcos_sdk_c_struct_response_cb(C.on_recv_event_resp_callback), setContext(chanData)))
}

Expand Down

0 comments on commit d9217a0

Please sign in to comment.