fpagliughi / sockpp

Modern C++ socket library.
BSD 3-Clause "New" or "Revised" License
769 stars 126 forks source link

SO_REUSEADDR #97

Open sheinb opened 4 months ago

sheinb commented 4 months ago

From what I can tell, the default for tcp acceptors is now to not reuse addresses but there is a possibility of passing this as a parameter. I can't quite get this to work, though. The examples, using the constructor with "ec" passed, work fine, but if I try the alternate, with the last argument specified as the reuse argument (1?) I get compiler errors. Is there a working example using this option (or can I set REUSEADDR another way)?

fpagliughi commented 4 months ago

The code that's in master/develop is exceptionally unstable at the moment as I move toward the v2.0 API. So be warned it's not meant for production applications yet, will likely change, and may have some bugs... and missing features!

That said, I appreciate any comments, suggestions, and bug reports as this comes together.

I had received complaints that the acceptor in v1 was opinionated in regard to its choices of reuse, and I always felt a little weird that I kept it in when I released the library publicly, since the original design choice was for a specific machine I was working on long ago.

The idea now, as you probably guessed. is that it doesn't chose a specific reuse strategy, but defaults to the same as the underlying library - which is none. The acceptor's open() and bind() calls now take a reuse parameter which you can use to select SO_REUSEADDR or SO_REUSEPORT, or keep the default as zero/none.

As you discovered, I forgot to add a noexcept version of the constructor which includes this parameter. Sounds like I need to add this:

acceptor(const sock_address& addr, int queSize, int reuse, error_code& ec) noexcept;

I will get that added right away, and look at updating an example or two to show how to use it.

sheinb commented 4 months ago

I ended up just creating the acceptor with no args and calling open on that (with the reuse arg). Seems to work as expected!On Feb 26, 2024, at 9:37 AM, Frank Pagliughi @.***> wrote: The code that's in master/develop is exceptionally unstable at the moment as I move toward the v2.0 API. So be warned it's not meant for production applications yet, will likely change, and may have some bugs... and missing features! That said, I appreciate any comments, suggestions, and bug reports as this comes together. I had received complaints that the acceptor in v1 was opinionated in regard to its choices of reuse, and I always felt a little weird that I kept it in when I released the library publicly, since the original design choice was for a specific machine I was working on long ago. The idea now, as you probably guessed. is that it doesn't chose a specific reuse strategy, but defaults to the same as the underlying library - which is none. The acceptor's open() and bind() calls now take a reuse parameter which you can use to select SO_REUSEADDR or SO_REUSEPORT, or keep the default as zero/none. As you discovered, I forgot to add a noexcept version of the constructor which includes this parameter. Sounds like I need to add this: acceptor(const sock_address& addr, int queSize, int reuse, error_code& ec) noexcept;

I will get that added right away, and look at updating an example or two to show how to use it.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

fpagliughi commented 4 months ago

Fix is up in the develop branch. I'll get it into master after some additional testing.