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) {
errno);
}
// put the socket in reuse mode
int value = 1;
if (setsockopt(socket,
SOL_SOCKET,
SO_REUSEADDR,
&value,
sizeof(value)) != 0) {
throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
"failed to put socket in reuse mode",
errno);
if (reuseAddr_) {
// put the socket in reuse mode
int value = 1;
if (setsockopt(socket,
SOL_SOCKET,
SO_REUSEADDR,
&value,
sizeof(value)) != 0) {
throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
"failed to put socket in reuse mode",
errno);
}
}
if (reusePort_) {
......
......@@ -150,6 +150,14 @@ class AsyncUDPSocket : public EventHandler {
void setReusePort(bool reusePort) {
reusePort_ = reusePort;
}
/**
* Set SO_REUSEADDR flag on the socket. Default is ON.
*/
void setReuseAddr(bool reuseAddr) {
reuseAddr_ = reuseAddr;
}
private:
AsyncUDPSocket(const AsyncUDPSocket&) = delete;
AsyncUDPSocket& operator=(const AsyncUDPSocket&) = delete;
......@@ -172,6 +180,7 @@ class AsyncUDPSocket : public EventHandler {
// Non-null only when we are reading
ReadCallback* readCallback_;
bool reuseAddr_{true};
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