Allow binaries compiled on a system that doesn't support SO_REUSEPORT to use the option
on systems that do support it
This commit is contained in:
parent
5fa3cb8632
commit
256df78fc6
1 changed files with 33 additions and 9 deletions
|
|
@ -13,6 +13,11 @@
|
||||||
|
|
||||||
namespace fc {
|
namespace fc {
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
bool have_so_reuseport = true;
|
||||||
|
}
|
||||||
|
|
||||||
class tcp_socket::impl : public tcp_socket_io_hooks {
|
class tcp_socket::impl : public tcp_socket_io_hooks {
|
||||||
public:
|
public:
|
||||||
impl() :
|
impl() :
|
||||||
|
|
@ -205,13 +210,24 @@ namespace fc {
|
||||||
FC_ASSERT(my->_sock.is_open());
|
FC_ASSERT(my->_sock.is_open());
|
||||||
boost::asio::socket_base::reuse_address option(enable);
|
boost::asio::socket_base::reuse_address option(enable);
|
||||||
my->_sock.set_option(option);
|
my->_sock.set_option(option);
|
||||||
#if defined(__APPLE__) || (defined(__linux__) && defined(SO_REUSEPORT))
|
#if defined(__APPLE__) || defined(__linux__)
|
||||||
|
# ifndef SO_REUSEPORT
|
||||||
|
# define SO_REUSEPORT 15
|
||||||
|
# endif
|
||||||
// OSX needs SO_REUSEPORT in addition to SO_REUSEADDR.
|
// OSX needs SO_REUSEPORT in addition to SO_REUSEADDR.
|
||||||
// This probably needs to be set for any BSD
|
// This probably needs to be set for any BSD
|
||||||
|
if (detail::have_so_reuseport)
|
||||||
|
{
|
||||||
int reuseport_value = 1;
|
int reuseport_value = 1;
|
||||||
if (setsockopt(my->_sock.native(), SOL_SOCKET, SO_REUSEPORT,
|
if (setsockopt(my->_sock.native(), SOL_SOCKET, SO_REUSEPORT,
|
||||||
(char*)&reuseport_value, sizeof(reuseport_value)) < 0)
|
(char*)&reuseport_value, sizeof(reuseport_value)) < 0)
|
||||||
|
{
|
||||||
|
if (errno == ENOPROTOOPT)
|
||||||
|
detail::have_so_reuseport = false;
|
||||||
|
else
|
||||||
wlog("Error setting SO_REUSEPORT");
|
wlog("Error setting SO_REUSEPORT");
|
||||||
|
}
|
||||||
|
}
|
||||||
#endif // __APPLE__
|
#endif // __APPLE__
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -267,10 +283,18 @@ namespace fc {
|
||||||
#if defined(__APPLE__) || (defined(__linux__) && defined(SO_REUSEPORT))
|
#if defined(__APPLE__) || (defined(__linux__) && defined(SO_REUSEPORT))
|
||||||
// OSX needs SO_REUSEPORT in addition to SO_REUSEADDR.
|
// OSX needs SO_REUSEPORT in addition to SO_REUSEADDR.
|
||||||
// This probably needs to be set for any BSD
|
// This probably needs to be set for any BSD
|
||||||
|
if (detail::have_so_reuseport)
|
||||||
|
{
|
||||||
int reuseport_value = 1;
|
int reuseport_value = 1;
|
||||||
if (setsockopt(my->_accept.native(), SOL_SOCKET, SO_REUSEPORT,
|
if (setsockopt(my->_accept.native(), SOL_SOCKET, SO_REUSEPORT,
|
||||||
(char*)&reuseport_value, sizeof(reuseport_value)) < 0)
|
(char*)&reuseport_value, sizeof(reuseport_value)) < 0)
|
||||||
|
{
|
||||||
|
if (errno == ENOPROTOOPT)
|
||||||
|
detail::have_so_reuseport = false;
|
||||||
|
else
|
||||||
wlog("Error setting SO_REUSEPORT");
|
wlog("Error setting SO_REUSEPORT");
|
||||||
|
}
|
||||||
|
}
|
||||||
#endif // __APPLE__
|
#endif // __APPLE__
|
||||||
}
|
}
|
||||||
void tcp_server::listen( uint16_t port )
|
void tcp_server::listen( uint16_t port )
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue