Commit 5333a914 authored by Sean Cannella's avatar Sean Cannella Committed by Noam Lerner

SocketAddress::anyAddress should be a method

Summary:
Calling getaddrinfo from a static constructor is not the most
reliable thing to do. Stop doing it.

Test Plan: existing tests

Reviewed By: davejwatson@fb.com

Subscribers: dancol, mzlee, shikong, kmdent, fma, bmatheny, benyluo, ranjeeth, folly-diffs@, jsedgwick, yfeldblum

FB internal diff: D1924578

Tasks: 6534662

Signature: t1:1924578:1426718586:d73bc2f001095e66fa77fde9c027af050cc26d2a
parent efecbe5f
...@@ -308,7 +308,7 @@ class AsyncSSLSocket : public virtual AsyncSocket { ...@@ -308,7 +308,7 @@ class AsyncSSLSocket : public virtual AsyncSocket {
const folly::SocketAddress& address, const folly::SocketAddress& address,
int timeout = 0, int timeout = 0,
const OptionMap &options = emptyOptionMap, const OptionMap &options = emptyOptionMap,
const folly::SocketAddress& bindAddr = anyAddress) const folly::SocketAddress& bindAddr = anyAddress())
noexcept override; noexcept override;
using AsyncSocket::connect; using AsyncSocket::connect;
......
...@@ -37,8 +37,6 @@ namespace folly { ...@@ -37,8 +37,6 @@ namespace folly {
// static members initializers // static members initializers
const AsyncSocket::OptionMap AsyncSocket::emptyOptionMap; const AsyncSocket::OptionMap AsyncSocket::emptyOptionMap;
const folly::SocketAddress AsyncSocket::anyAddress =
folly::SocketAddress("0.0.0.0", 0);
const AsyncSocketException socketClosedLocallyEx( const AsyncSocketException socketClosedLocallyEx(
AsyncSocketException::END_OF_FILE, "socket closed locally"); AsyncSocketException::END_OF_FILE, "socket closed locally");
...@@ -273,6 +271,12 @@ int AsyncSocket::detachFd() { ...@@ -273,6 +271,12 @@ int AsyncSocket::detachFd() {
return fd; return fd;
} }
const folly::SocketAddress& AsyncSocket::anyAddress() {
static const folly::SocketAddress anyAddress =
folly::SocketAddress("0.0.0.0", 0);
return anyAddress;
}
void AsyncSocket::setShutdownSocketSet(ShutdownSocketSet* newSS) { void AsyncSocket::setShutdownSocketSet(ShutdownSocketSet* newSS) {
if (shutdownSocketSet_ == newSS) { if (shutdownSocketSet_ == newSS) {
return; return;
...@@ -371,7 +375,7 @@ void AsyncSocket::connect(ConnectCallback* callback, ...@@ -371,7 +375,7 @@ void AsyncSocket::connect(ConnectCallback* callback,
<< ", fd=" << fd_ << ", host=" << address.describe().c_str(); << ", fd=" << fd_ << ", host=" << address.describe().c_str();
// bind the socket // bind the socket
if (bindAddr != anyAddress) { if (bindAddr != anyAddress()) {
int one = 1; int one = 1;
if (::setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) { if (::setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
doClose(); doClose();
......
...@@ -238,7 +238,7 @@ class AsyncSocket : virtual public AsyncTransportWrapper { ...@@ -238,7 +238,7 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
typedef std::map<OptionKey, int> OptionMap; typedef std::map<OptionKey, int> OptionMap;
static const OptionMap emptyOptionMap; static const OptionMap emptyOptionMap;
static const folly::SocketAddress anyAddress; static const folly::SocketAddress& anyAddress();
/** /**
* Initiate a connection. * Initiate a connection.
...@@ -254,7 +254,7 @@ class AsyncSocket : virtual public AsyncTransportWrapper { ...@@ -254,7 +254,7 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
const folly::SocketAddress& address, const folly::SocketAddress& address,
int timeout = 0, int timeout = 0,
const OptionMap &options = emptyOptionMap, const OptionMap &options = emptyOptionMap,
const folly::SocketAddress& bindAddr = anyAddress const folly::SocketAddress& bindAddr = anyAddress()
) noexcept; ) noexcept;
void connect(ConnectCallback* callback, const std::string& ip, uint16_t port, void connect(ConnectCallback* callback, const std::string& ip, uint16_t port,
int timeout = 00, int timeout = 00,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment