Cannot setopt with empty string
Closed this issue · 4 comments
While trying to use nngpp with a pub/sub protocol, I spent hours trying to diagnose why I never received anything, while the exact same sequence using nng calls worked. The problem was this:
socket.set_opt( NNG_OPT_SUB_SUBSCRIBE, "" );
The problem is that the type translation results in this becoming:
nng_setopt( socket, NNG_OPT_SUB_SUBSCRIBE, "", 1 );
as a one-byte buffer (containing \0). To succeed, the fourth parameter needs to be 0. This can be force by using
socket.set_opt( NNG_OPT_SUB_SUBSCRIBE, nng:view("",0) );
but that seems to be a hack.
I cannot think of any situations where the zero-terminator should be included in a set_opt string.
This behaviour is consistent with nng, which by default will include the null terminator when setting string options (see here).
A couple of things that should work:
- Use the generic setter with a default-constructed view:
sock.set_opt( NNG_OPT_SUB_SUBSCRIBE, {} )
- Use the string setter with a nullptr:
sock.set_opt_string( NNG_OPT_SUB_SUBSCRIBE, nullptr )
Got it, thanks.
OMG, I was absolutely at a deadlock for a couple of days.
Thank you @cwzx, it's great!
Super helpful! Saved my day! 🤯🤯🤯
This issue should be pinned.
Do you think this should also be added in the docs?