Skip to content

Commit fe85de7

Browse files
committed
Adds configurable SO_REUSEADDR option. references XRPLF#311
1 parent ca97dd1 commit fe85de7

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ HEAD
1111
- Feature: Adds the ability to pause reading on a connection. Paused connections will not
1212
read more data from their socket, allowing TCP flow control to work without blocking
1313
the main thread.
14+
- Feature: Adds the ability to specify whether or not to use the `SO_REUSEADDR` TCP socket
15+
option. The default for this value has been changed from `true` to `false`.
1416
- Improvement: Open, close, and pong timeouts can be disabled entirely by setting their
1517
duration to 0.
1618
- Improvement: Numerous performance improvements. Including: tuned default

test/transport/integration.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ void run_server(server * s, int port, bool log = false) {
108108
}
109109

110110
s->init_asio();
111+
s->set_reuse_addr(true);
111112

112113
s->listen(port);
113114
s->start_accept();
@@ -124,6 +125,7 @@ void run_client(client & c, std::string uri, bool log = false) {
124125
}
125126
websocketpp::lib::error_code ec;
126127
c.init_asio(ec);
128+
c.set_reuse_addr(true);
127129
BOOST_CHECK(!ec);
128130

129131
client::connection_ptr con = c.get_connection(uri,ec);

websocketpp/transport/asio/endpoint.hpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class endpoint : public config::socket_type {
9191
explicit endpoint()
9292
: m_external_io_service(false)
9393
, m_listen_backlog(0)
94+
, m_reuse_addr(false)
9495
, m_state(UNINITIALIZED)
9596
{
9697
//std::cout << "transport::asio::endpoint constructor" << std::endl;
@@ -123,6 +124,7 @@ class endpoint : public config::socket_type {
123124
, m_external_io_service(src.m_external_io_service)
124125
, m_acceptor(src.m_acceptor)
125126
, m_listen_backlog(0)
127+
, m_reuse_addr(src.m_reuse_addr)
126128
, m_state(src.m_state)
127129
{
128130
src.m_io_service = NULL;
@@ -137,6 +139,7 @@ class endpoint : public config::socket_type {
137139
m_external_io_service = rhs.m_external_io_service;
138140
m_acceptor = rhs.m_acceptor;
139141
m_listen_backlog = rhs.m_listen_backlog
142+
m_reuse_addr = rhs.m_reuse_addr;
140143
m_state = rhs.m_state;
141144

142145
rhs.m_io_service = NULL;
@@ -287,6 +290,24 @@ class endpoint : public config::socket_type {
287290
void set_listen_backlog(int backlog) {
288291
m_listen_backlog = backlog;
289292
}
293+
294+
/// Sets whether or not to use the SO_REUSEADDR flag when opening a listening socket
295+
/**
296+
* Specifies whether or not to use the SO_REUSEADDR TCP socket option. What this flag
297+
* does depends on your operating system. Please consult operating system
298+
* documentation for more details.
299+
*
300+
* New values affect future calls to listen only.
301+
*
302+
* The default is false.
303+
*
304+
* @since 0.4.0-alpha1
305+
*
306+
* @param value Whether or not to use the SO_REUSEADDR option
307+
*/
308+
void set_reuse_addr(bool value) {
309+
m_reuse_addr = value;
310+
}
290311

291312
/// Retrieve a reference to the endpoint's io_service
292313
/**
@@ -324,7 +345,7 @@ class endpoint : public config::socket_type {
324345
m_alog->write(log::alevel::devel,"asio::listen");
325346

326347
m_acceptor->open(ep.protocol());
327-
m_acceptor->set_option(boost::asio::socket_base::reuse_address(true));
348+
m_acceptor->set_option(boost::asio::socket_base::reuse_address(m_reuse_addr));
328349
m_acceptor->bind(ep);
329350
if (m_listen_backlog == 0) {
330351
m_acceptor->listen();
@@ -1024,6 +1045,7 @@ class endpoint : public config::socket_type {
10241045

10251046
// Network constants
10261047
int m_listen_backlog;
1048+
bool m_reuse_addr;
10271049

10281050
elog_type* m_elog;
10291051
alog_type* m_alog;

0 commit comments

Comments
 (0)