#include <unistd.h>
#include <fcntl.h>
+// Due to the way kernel headers are included, this may or may not be defined.
+// Number pulled from 3.10 kernel headers.
+#ifndef SO_REUSEPORT
+#define SO_REUSEPORT 15
+#endif
+
namespace folly {
AsyncUDPSocket::AsyncUDPSocket(EventBase* evb)
errno);
}
+ if (reusePort_) {
+ // put the socket in port reuse mode
+ int value = 1;
+ if (setsockopt(socket,
+ SOL_SOCKET,
+ SO_REUSEPORT,
+ &value,
+ sizeof(value)) != 0) {
+ ::close(socket);
+ throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
+ "failed to put socket in reuse_port mode",
+ errno);
+
+ }
+ }
+
// bind to the address
sockaddr_storage addrStorage;
address.getAddress(&addrStorage);
CHECK_NE(-1, fd_) << "Need to bind before getting FD out";
return fd_;
}
+
+ /**
+ * Set reuse port mode to call bind() on the same address multiple times
+ */
+ void setReusePort(bool reusePort) {
+ reusePort_ = reusePort;
+ }
private:
AsyncUDPSocket(const AsyncUDPSocket&) = delete;
AsyncUDPSocket& operator=(const AsyncUDPSocket&) = delete;
// Non-null only when we are reading
ReadCallback* readCallback_;
+
+ bool reusePort_{false};
};
} // Namespace