mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-02-25 18:35:32 +00:00
Allow to provide user data for the filter subscribe callback
This commit is contained in:
parent
f0a5d8d83c
commit
cfdab7614d
@ -5,10 +5,10 @@ package main
|
||||
#include "libnimbus.h"
|
||||
|
||||
// receiveHandler gateway function
|
||||
void receiveHandler_cgo(received_message * msg)
|
||||
void receiveHandler_cgo(received_message * msg, void* udata)
|
||||
{
|
||||
void receiveHandler(received_message*);
|
||||
receiveHandler(msg);
|
||||
void receiveHandler(received_message* msg, void* udata);
|
||||
receiveHandler(msg, udata);
|
||||
}
|
||||
*/
|
||||
import "C"
|
@ -45,7 +45,7 @@ typedef struct {
|
||||
uint8_t topic[4];
|
||||
} topic;
|
||||
|
||||
typedef void (*received_msg_handler)(received_message* msg);
|
||||
typedef void (*received_msg_handler)(received_message* msg, void* udata);
|
||||
|
||||
/** Initialize Nim and the status library */
|
||||
void NimMain();
|
||||
@ -88,7 +88,7 @@ int nimbus_get_symkey(const char* id, uint8_t* symkey);
|
||||
|
||||
/* Subscribe to given filter */
|
||||
const char* nimbus_subscribe_filter(filter_options* filter_options,
|
||||
received_msg_handler msg);
|
||||
received_msg_handler msg, void* udata);
|
||||
int nimbus_unsubscribe_filter(const char* id);
|
||||
/* Post Whisper message */
|
||||
int nimbus_post(post_message* msg);
|
||||
|
@ -338,8 +338,8 @@ proc nimbus_post(message: ptr CPostMessage): bool {.exportc,foreignThreadGc.} =
|
||||
powTarget = message.powTarget)
|
||||
|
||||
proc nimbus_subscribe_filter(options: ptr CFilterOptions,
|
||||
handler: proc (msg: ptr CReceivedMessage) {.gcsafe, cdecl.}):
|
||||
cstring {.exportc, foreignThreadGc.} =
|
||||
handler: proc (msg: ptr CReceivedMessage, udata: pointer) {.gcsafe, cdecl.},
|
||||
udata: pointer = nil): cstring {.exportc, foreignThreadGc.} =
|
||||
## In case of a passed handler, the received msg needs to be copied before the
|
||||
## handler ends.
|
||||
## TODO: provide some user context passing here else this is rather useless?
|
||||
@ -379,7 +379,7 @@ proc nimbus_subscribe_filter(options: ptr CFilterOptions,
|
||||
if msg.dst.isSome():
|
||||
cmsg.recipientPublicKey = msg.decoded.src.get()
|
||||
|
||||
handler(addr cmsg)
|
||||
handler(addr cmsg, udata)
|
||||
|
||||
result = node.subscribeFilter(filter, c_handler)
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
void NimMain();
|
||||
|
||||
void print_msg(received_message* msg) {
|
||||
void print_msg(received_message* msg, void* udata) {
|
||||
// Note: early null chars will terminate string early
|
||||
printf("received message %.*s\n", (int)msg->decodedLen, msg->decoded);
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
#cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lnimbus -lm
|
||||
#include "libnimbus.h"
|
||||
|
||||
void receiveHandler_cgo(received_message * msg); // Forward declaration.
|
||||
void receiveHandler_cgo(received_message * msg, void* udata); // Forward declaration.
|
||||
*/
|
||||
import "C"
|
||||
|
||||
@ -30,10 +30,13 @@ func poll() {
|
||||
}
|
||||
|
||||
//export receiveHandler
|
||||
func receiveHandler(msg *C.received_message) {
|
||||
func receiveHandler(msg *C.received_message, udata unsafe.Pointer) {
|
||||
fmt.Printf("[nim-status] received message %s\n",
|
||||
C.GoStringN((*C.char)(msg.decoded), (C.int)(msg.decodedLen)) )
|
||||
fmt.Printf("[nim-status] source public key %x\n", msg.source)
|
||||
msgCount := (*int)(udata)
|
||||
*msgCount += 1
|
||||
fmt.Printf("[nim-status] message count %d\n", *msgCount)
|
||||
}
|
||||
|
||||
func Start() {
|
||||
@ -51,11 +54,14 @@ func StatusListenAndPost(channel string) {
|
||||
symKeyId := C.GoString(C.nimbus_add_symkey_from_password(C.CString(channel)))
|
||||
asymKeyId := C.GoString(C.nimbus_new_keypair())
|
||||
|
||||
msgCount := 0
|
||||
options := C.filter_options{symKeyID: C.CString(symKeyId),
|
||||
minPow: 0.002,
|
||||
topic: C.nimbus_string_to_topic(C.CString(channel)).topic}
|
||||
C.nimbus_subscribe_filter(&options,
|
||||
(C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo)))
|
||||
filterId := C.GoString(C.nimbus_subscribe_filter(&options,
|
||||
(C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo)),
|
||||
unsafe.Pointer(&msgCount)))
|
||||
fmt.Printf("[nim-status] filter subscribed, id: %s\n", filterId)
|
||||
|
||||
postMessage := C.post_message{symKeyID: C.CString(symKeyId),
|
||||
sourceID: C.CString(asymKeyId),
|
||||
|
Loading…
x
Reference in New Issue
Block a user