Use pointers types for source and recipientPublicKey

This commit is contained in:
kdeme 2019-11-05 11:09:23 +01:00 committed by zah
parent d07ecbbf68
commit e4ca4b7d31
3 changed files with 25 additions and 15 deletions

View File

@ -12,8 +12,8 @@ extern "C" {
typedef struct { typedef struct {
uint8_t* decoded; uint8_t* decoded;
size_t decodedLen; size_t decodedLen;
uint8_t source[64]; /* TODO: change to ptr so they can be checked on nil? */ uint8_t* source; /* 64 bytes public key, can be nil */
uint8_t recipientPublicKey[64]; /* TODO: change to ptr so they can be checked on nil? */ uint8_t* recipientPublicKey; /* 64 bytes public key, can be nil */
uint32_t timestamp; uint32_t timestamp;
uint32_t ttl; uint32_t ttl;
uint8_t topic[4]; uint8_t topic[4];
@ -22,11 +22,11 @@ typedef struct {
} received_message; } received_message;
typedef struct { typedef struct {
const char* symKeyID; const char* symKeyID; /* Identifier for symmetric key, set to nil if none */
const char* privateKeyID; const char* privateKeyID; /* Identifier for asymmetric key, set to nil if none */
uint8_t* source; /* 64 bytes public key */ uint8_t* source; /* 64 bytes public key, set to nil if none */
double minPow; double minPow;
uint8_t topic[4]; uint8_t topic[4]; /* Will default to 0x00000000 if not provided */
int allowP2P; int allowP2P;
} filter_options; } filter_options;
@ -35,10 +35,10 @@ typedef struct {
uint8_t* pubKey; /* 64 bytes public key, set to nil if none */ uint8_t* pubKey; /* 64 bytes public key, set to nil if none */
const char* sourceID; /* Identifier for asymmetric key, set to nil if none */ const char* sourceID; /* Identifier for asymmetric key, set to nil if none */
uint32_t ttl; uint32_t ttl;
uint8_t topic[4]; /* default of 0 is OK */ uint8_t topic[4]; /* Will default to 0x00000000 if not provided */
uint8_t* payload; /* payload to be send, can be len=0 but can not be nil */ uint8_t* payload; /* Payload to be send, can be len=0 but can not be nil */
size_t payloadLen; size_t payloadLen;
uint8_t* padding; /* custom padding, can be set to nil */ uint8_t* padding; /* Custom padding, can be set to nil */
size_t paddingLen; size_t paddingLen;
double powTime; double powTime;
double powTarget; double powTarget;

View File

@ -31,8 +31,8 @@ type
CReceivedMessage* = object CReceivedMessage* = object
decoded*: ptr byte decoded*: ptr byte
decodedLen*: csize decodedLen*: csize
source*: PublicKey source*: ref PublicKey
recipientPublicKey*: PublicKey recipientPublicKey*: ref PublicKey
timestamp*: uint32 timestamp*: uint32
ttl*: uint32 ttl*: uint32
topic*: Topic topic*: Topic
@ -430,11 +430,18 @@ proc nimbus_subscribe_filter(options: ptr CFilterOptions,
hash: msg.hash hash: msg.hash
) )
# TODO: change this to ptr, so that C/go code can check on nil? # Should be GCed when handler goes out of scope
var
source: ref PublicKey
recipientPublicKey: ref PublicKey
if msg.decoded.src.isSome(): if msg.decoded.src.isSome():
cmsg.source = msg.decoded.src.get() new(source)
source[] = msg.decoded.src.get()
cmsg.source = source
if msg.dst.isSome(): if msg.dst.isSome():
cmsg.recipientPublicKey = msg.dst.get() new(recipientPublicKey)
recipientPublicKey[] = msg.dst.get()
cmsg.recipientPublicKey = recipientPublicKey
handler(addr cmsg, udata) handler(addr cmsg, udata)

View File

@ -37,7 +37,10 @@ func poll() {
func receiveHandler(msg *C.received_message, udata unsafe.Pointer) { func receiveHandler(msg *C.received_message, udata unsafe.Pointer) {
receivedMsg := C.GoBytes(unsafe.Pointer(msg.decoded), C.int(msg.decodedLen)) receivedMsg := C.GoBytes(unsafe.Pointer(msg.decoded), C.int(msg.decodedLen))
fmt.Printf("[nim-status] received message %s\n", string(receivedMsg)) fmt.Printf("[nim-status] received message %s\n", string(receivedMsg))
fmt.Printf("[nim-status] source public key %x\n", msg.source) if msg.source != nil {
source := C.GoBytes(unsafe.Pointer(msg.source), 64)
fmt.Printf("[nim-status] source public key %x\n", string(source))
}
msgCount := (*int)(udata) msgCount := (*int)(udata)
*msgCount += 1 *msgCount += 1
fmt.Printf("[nim-status] message count %d\n", *msgCount) fmt.Printf("[nim-status] message count %d\n", *msgCount)