Commit e71c842c authored by Naizhi Li's avatar Naizhi Li Committed by facebook-github-bot-9

Allow AysncUDPSocket to work without SO_REUSEADDR flag

Summary: Today it's hardcoded to use the flag, which could result
in some problem. We should allow callers to choose.

Reviewed By: @djwatson

Differential Revision: D2345036
parent bb7d2e02
...@@ -63,6 +63,7 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) { ...@@ -63,6 +63,7 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
errno); errno);
} }
if (reuseAddr_) {
// put the socket in reuse mode // put the socket in reuse mode
int value = 1; int value = 1;
if (setsockopt(socket, if (setsockopt(socket,
...@@ -74,6 +75,7 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) { ...@@ -74,6 +75,7 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
"failed to put socket in reuse mode", "failed to put socket in reuse mode",
errno); errno);
} }
}
if (reusePort_) { if (reusePort_) {
// put the socket in port reuse mode // put the socket in port reuse mode
......
...@@ -150,6 +150,14 @@ class AsyncUDPSocket : public EventHandler { ...@@ -150,6 +150,14 @@ class AsyncUDPSocket : public EventHandler {
void setReusePort(bool reusePort) { void setReusePort(bool reusePort) {
reusePort_ = reusePort; reusePort_ = reusePort;
} }
/**
* Set SO_REUSEADDR flag on the socket. Default is ON.
*/
void setReuseAddr(bool reuseAddr) {
reuseAddr_ = reuseAddr;
}
private: private:
AsyncUDPSocket(const AsyncUDPSocket&) = delete; AsyncUDPSocket(const AsyncUDPSocket&) = delete;
AsyncUDPSocket& operator=(const AsyncUDPSocket&) = delete; AsyncUDPSocket& operator=(const AsyncUDPSocket&) = delete;
...@@ -172,6 +180,7 @@ class AsyncUDPSocket : public EventHandler { ...@@ -172,6 +180,7 @@ class AsyncUDPSocket : public EventHandler {
// Non-null only when we are reading // Non-null only when we are reading
ReadCallback* readCallback_; ReadCallback* readCallback_;
bool reuseAddr_{true};
bool reusePort_{false}; bool reusePort_{false};
}; };
......
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