Commit 54c6d875 authored by Luca Niccolini's avatar Luca Niccolini Committed by Facebook Github Bot

Async UDP Socket: socket options

Summary:
generic API for setting socket options on a UDP socket

(Note: this ignores all push blocking failures!)

Reviewed By: yangchi

Differential Revision: D19763927

fbshipit-source-id: d1a7720b04c24592e98c6e30c93c26e650ccd0b0
parent 549476f0
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
#include <folly/io/SocketOptionMap.h> #include <folly/io/SocketOptionMap.h>
#include <folly/net/NetworkSocket.h> #include <folly/net/NetworkSocket.h>
#include <errno.h>
namespace folly { namespace folly {
const SocketOptionMap emptySocketOptionMap; const SocketOptionMap emptySocketOptionMap;
...@@ -24,4 +26,20 @@ const SocketOptionMap emptySocketOptionMap; ...@@ -24,4 +26,20 @@ const SocketOptionMap emptySocketOptionMap;
int SocketOptionKey::apply(NetworkSocket fd, int val) const { int SocketOptionKey::apply(NetworkSocket fd, int val) const {
return netops::setsockopt(fd, level, optname, &val, sizeof(val)); return netops::setsockopt(fd, level, optname, &val, sizeof(val));
} }
int applySocketOptions(
NetworkSocket fd,
const SocketOptionMap& options,
SocketOptionKey::ApplyPos pos) {
for (const auto& opt : options) {
if (opt.first.applyPos_ == pos) {
auto rv = opt.first.apply(fd, opt.second);
if (rv != 0) {
return errno;
}
}
}
return 0;
}
} // namespace folly } // namespace folly
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#pragma once #pragma once
#include <folly/net/NetworkSocket.h>
#include <folly/portability/Sockets.h> #include <folly/portability/Sockets.h>
#include <map> #include <map>
...@@ -50,4 +51,9 @@ using SocketOptionMap = std::map<SocketOptionKey, int>; ...@@ -50,4 +51,9 @@ using SocketOptionMap = std::map<SocketOptionKey, int>;
extern const SocketOptionMap emptySocketOptionMap; extern const SocketOptionMap emptySocketOptionMap;
int applySocketOptions(
NetworkSocket fd,
const SocketOptionMap& options,
SocketOptionKey::ApplyPos pos);
} // namespace folly } // namespace folly
...@@ -1597,17 +1597,12 @@ void AsyncSocket::cachePeerAddress() const { ...@@ -1597,17 +1597,12 @@ void AsyncSocket::cachePeerAddress() const {
void AsyncSocket::applyOptions( void AsyncSocket::applyOptions(
const SocketOptionMap& options, const SocketOptionMap& options,
SocketOptionKey::ApplyPos pos) { SocketOptionKey::ApplyPos pos) {
for (const auto& opt : options) { auto result = applySocketOptions(fd_, options, pos);
if (opt.first.applyPos_ == pos) { if (result != 0) {
auto rv = opt.first.apply(fd_, opt.second);
if (rv != 0) {
auto errnoCopy = errno;
throw AsyncSocketException( throw AsyncSocketException(
AsyncSocketException::INTERNAL_ERROR, AsyncSocketException::INTERNAL_ERROR,
withAddr("failed to set socket option"), withAddr("failed to set socket option"),
errnoCopy); result);
}
}
} }
} }
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <folly/io/async/AsyncUDPSocket.h> #include <folly/io/async/AsyncUDPSocket.h>
#include <folly/Likely.h> #include <folly/Likely.h>
#include <folly/io/SocketOptionMap.h>
#include <folly/io/async/EventBase.h> #include <folly/io/async/EventBase.h>
#include <folly/portability/Fcntl.h> #include <folly/portability/Fcntl.h>
#include <folly/portability/Sockets.h> #include <folly/portability/Sockets.h>
...@@ -766,6 +767,18 @@ void AsyncUDPSocket::setTrafficClass(int tclass) { ...@@ -766,6 +767,18 @@ void AsyncUDPSocket::setTrafficClass(int tclass) {
} }
} }
void AsyncUDPSocket::applyOptions(
const SocketOptionMap& options,
SocketOptionKey::ApplyPos pos) {
auto result = applySocketOptions(fd_, options, pos);
if (result != 0) {
throw AsyncSocketException(
AsyncSocketException::INTERNAL_ERROR,
"failed to set socket option",
result);
}
}
void AsyncUDPSocket::detachEventBase() { void AsyncUDPSocket::detachEventBase() {
DCHECK(eventBase_ && eventBase_->isInEventBaseThread()); DCHECK(eventBase_ && eventBase_->isInEventBaseThread());
registerHandler(uint16_t(NONE)); registerHandler(uint16_t(NONE));
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include <folly/ScopeGuard.h> #include <folly/ScopeGuard.h>
#include <folly/SocketAddress.h> #include <folly/SocketAddress.h>
#include <folly/io/IOBuf.h> #include <folly/io/IOBuf.h>
#include <folly/io/SocketOptionMap.h>
#include <folly/io/async/AsyncSocketBase.h> #include <folly/io/async/AsyncSocketBase.h>
#include <folly/io/async/AsyncSocketException.h> #include <folly/io/async/AsyncSocketException.h>
#include <folly/io/async/EventBase.h> #include <folly/io/async/EventBase.h>
...@@ -348,6 +349,10 @@ class AsyncUDPSocket : public EventHandler { ...@@ -348,6 +349,10 @@ class AsyncUDPSocket : public EventHandler {
void setTrafficClass(int tclass); void setTrafficClass(int tclass);
void applyOptions(
const SocketOptionMap& options,
SocketOptionKey::ApplyPos pos);
protected: protected:
virtual ssize_t virtual ssize_t
sendmsg(NetworkSocket socket, const struct msghdr* message, int flags) { sendmsg(NetworkSocket socket, const struct msghdr* message, int flags) {
......
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