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 @@
#include <folly/io/SocketOptionMap.h>
#include <folly/net/NetworkSocket.h>
#include <errno.h>
namespace folly {
const SocketOptionMap emptySocketOptionMap;
......@@ -24,4 +26,20 @@ const SocketOptionMap emptySocketOptionMap;
int SocketOptionKey::apply(NetworkSocket fd, int val) const {
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
......@@ -16,6 +16,7 @@
#pragma once
#include <folly/net/NetworkSocket.h>
#include <folly/portability/Sockets.h>
#include <map>
......@@ -50,4 +51,9 @@ using SocketOptionMap = std::map<SocketOptionKey, int>;
extern const SocketOptionMap emptySocketOptionMap;
int applySocketOptions(
NetworkSocket fd,
const SocketOptionMap& options,
SocketOptionKey::ApplyPos pos);
} // namespace folly
......@@ -1597,17 +1597,12 @@ void AsyncSocket::cachePeerAddress() const {
void AsyncSocket::applyOptions(
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) {
auto errnoCopy = errno;
throw AsyncSocketException(
AsyncSocketException::INTERNAL_ERROR,
withAddr("failed to set socket option"),
errnoCopy);
}
}
auto result = applySocketOptions(fd_, options, pos);
if (result != 0) {
throw AsyncSocketException(
AsyncSocketException::INTERNAL_ERROR,
withAddr("failed to set socket option"),
result);
}
}
......
......@@ -17,6 +17,7 @@
#include <folly/io/async/AsyncUDPSocket.h>
#include <folly/Likely.h>
#include <folly/io/SocketOptionMap.h>
#include <folly/io/async/EventBase.h>
#include <folly/portability/Fcntl.h>
#include <folly/portability/Sockets.h>
......@@ -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() {
DCHECK(eventBase_ && eventBase_->isInEventBaseThread());
registerHandler(uint16_t(NONE));
......
......@@ -21,6 +21,7 @@
#include <folly/ScopeGuard.h>
#include <folly/SocketAddress.h>
#include <folly/io/IOBuf.h>
#include <folly/io/SocketOptionMap.h>
#include <folly/io/async/AsyncSocketBase.h>
#include <folly/io/async/AsyncSocketException.h>
#include <folly/io/async/EventBase.h>
......@@ -348,6 +349,10 @@ class AsyncUDPSocket : public EventHandler {
void setTrafficClass(int tclass);
void applyOptions(
const SocketOptionMap& options,
SocketOptionKey::ApplyPos pos);
protected:
virtual ssize_t
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