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,16 +63,18 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) { ...@@ -63,16 +63,18 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
errno); errno);
} }
// put the socket in reuse mode if (reuseAddr_) {
int value = 1; // put the socket in reuse mode
if (setsockopt(socket, int value = 1;
SOL_SOCKET, if (setsockopt(socket,
SO_REUSEADDR, SOL_SOCKET,
&value, SO_REUSEADDR,
sizeof(value)) != 0) { &value,
throw AsyncSocketException(AsyncSocketException::NOT_OPEN, sizeof(value)) != 0) {
"failed to put socket in reuse mode", throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
errno); "failed to put socket in reuse mode",
errno);
}
} }
if (reusePort_) { if (reusePort_) {
......
...@@ -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