Commit 91aeb986 authored by Dongyi Ye's avatar Dongyi Ye Committed by Facebook Github Bot

Add API to Set Socket Option for AsyncUDPServerSocket.h

Summary: Add API to Set Socket Option for AsyncUDPServerSocket.h, which is the same as AsyncUDPSocket.

Reviewed By: yfeldblum, lnicco

Differential Revision: D20064430

fbshipit-source-id: d448fbc217d0098e0ab94e2f113d4eba97958b02
parent c3015ed0
...@@ -42,4 +42,23 @@ int applySocketOptions( ...@@ -42,4 +42,23 @@ int applySocketOptions(
return 0; return 0;
} }
SocketOptionMap validateSocketOptions(
const SocketOptionMap& options,
sa_family_t family,
SocketOptionKey::ApplyPos pos) {
SocketOptionMap validOptions;
for (const auto& option : options) {
if (pos != option.first.applyPos_) {
continue;
}
if ((family == AF_INET && option.first.level == IPPROTO_IP) ||
(family == AF_INET6 && option.first.level == IPPROTO_IPV6) ||
option.first.level == IPPROTO_UDP || option.first.level == SOL_SOCKET ||
option.first.level == SOL_UDP) {
validOptions.insert(option);
}
}
return validOptions;
}
} // namespace folly } // namespace folly
...@@ -56,4 +56,9 @@ int applySocketOptions( ...@@ -56,4 +56,9 @@ int applySocketOptions(
const SocketOptionMap& options, const SocketOptionMap& options,
SocketOptionKey::ApplyPos pos); SocketOptionKey::ApplyPos pos);
SocketOptionMap validateSocketOptions(
const SocketOptionMap& options,
sa_family_t family,
SocketOptionKey::ApplyPos pos);
} // namespace folly } // namespace folly
...@@ -97,13 +97,23 @@ class AsyncUDPServerSocket : private AsyncUDPSocket::ReadCallback, ...@@ -97,13 +97,23 @@ class AsyncUDPServerSocket : private AsyncUDPSocket::ReadCallback,
} }
} }
void bind(const folly::SocketAddress& addy) { void bind(
const folly::SocketAddress& addy,
const SocketOptionMap& options = emptySocketOptionMap) {
CHECK(!socket_); CHECK(!socket_);
socket_ = std::make_shared<AsyncUDPSocket>(evb_); socket_ = std::make_shared<AsyncUDPSocket>(evb_);
socket_->setReusePort(reusePort_); socket_->setReusePort(reusePort_);
socket_->setReuseAddr(reuseAddr_); socket_->setReuseAddr(reuseAddr_);
socket_->applyOptions(
validateSocketOptions(
options, addy.getFamily(), SocketOptionKey::ApplyPos::PRE_BIND),
SocketOptionKey::ApplyPos::PRE_BIND);
socket_->bind(addy); socket_->bind(addy);
socket_->applyOptions(
validateSocketOptions(
options, addy.getFamily(), SocketOptionKey::ApplyPos::POST_BIND),
SocketOptionKey::ApplyPos::POST_BIND);
} }
void setReusePort(bool reusePort) { void setReusePort(bool reusePort) {
......
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