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
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_) {
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;
// Non-null only when we are reading
ReadCallback* readCallback_;
+ bool reuseAddr_{true};
bool reusePort_{false};
};