cwzx/nngpp

Changing the listening address

Closed this issue · 4 comments

Not really an issue, just a question :)

If I do:

socket.listen(somePath);
...
socket.listen(newPath);

do I need to do anything to release the listener (eg. socket = nng::something::open) or is it ok to directly call listen the second time?

cwzx commented

Yes you need to close the existing listener. Two options:

  1. As you suggested: Destroy the socket, which will close the existing listener, and replace with a new one.

  2. Use an explicit listener:

auto socket = ...

// instead of socket.listen
auto listener = nng::listener(socket, "some_path", 0);

// close the old listener, replacing with a new one
listener = nng::listener(socket, "new_path", 0);

Thank you!

By peeking at the source I confirmed my suspect that a second call to listen would leak the previous listener. I don't think that properly written C++ should allow that (smart pointers?) but this is quite a corner case so this gotcha should not catch many others...

The provided solutions work for me.

cwzx commented

By peeking at the source I confirmed my suspect that a second call to listen would leak the previous listener. I don't think that properly written C++ should allow that (smart pointers?) but this is quite a corner case so this gotcha should not catch many others...

This is not true.
Calling sock.listen() a second time results in two listeners associated with the socket (many to one relation).
Both are destroyed when the socket is closed. Nothing is leaked.
This is fine if you are ok with both being active.

In your case we needed to manually close one in order to effectively change the address.

Calling sock.listen() a second time results in two listeners associated with the socket (many to one relation).
Both are destroyed when the socket is closed. Nothing is leaked.
This is fine if you are ok with both being active.

Thanks for the clarification.