Commit 44a7106b authored by Luca Niccolini's avatar Luca Niccolini Committed by Facebook Github Bot

move AsyncSocket::OptionMap into SocketOptionMap (base)

Summary: base diff to move AsyncSocket::OptionMap into SocketOptionMap. folly only changes and an alias for previous usages of AsyncSocket::OptionMap

Reviewed By: yangchi

Differential Revision: D19961899

fbshipit-source-id: fab060cc73409c77ea07ced663fbd0ee15a35be7
parent 15e365c9
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/io/SocketOptionMap.h>
#include <folly/net/NetworkSocket.h>
namespace folly {
const SocketOptionMap emptySocketOptionMap;
int SocketOptionKey::apply(NetworkSocket fd, int val) const {
return netops::setsockopt(fd, level, optname, &val, sizeof(val));
}
} // namespace folly
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/portability/Sockets.h>
#include <map>
namespace folly {
/**
* Uniquely identifies a handle to a socket option value. Each
* combination of level and option name corresponds to one socket
* option value.
*/
class SocketOptionKey {
public:
enum class ApplyPos { POST_BIND = 0, PRE_BIND = 1 };
bool operator<(const SocketOptionKey& other) const {
if (level == other.level) {
return optname < other.optname;
}
return level < other.level;
}
int apply(NetworkSocket fd, int val) const;
int level;
int optname;
ApplyPos applyPos_{ApplyPos::POST_BIND};
};
// Maps from a socket option key to its value
using SocketOptionMap = std::map<SocketOptionKey, int>;
extern const SocketOptionMap emptySocketOptionMap;
} // namespace folly
......@@ -32,6 +32,7 @@
#include <folly/SpinLock.h>
#include <folly/io/Cursor.h>
#include <folly/io/IOBuf.h>
#include <folly/io/SocketOptionMap.h>
#include <folly/io/async/ssl/BasicTransportCertificate.h>
#include <folly/lang/Bits.h>
#include <folly/portability/OpenSSL.h>
......@@ -673,7 +674,7 @@ void AsyncSSLSocket::connect(
ConnectCallback* callback,
const folly::SocketAddress& address,
int timeout,
const OptionMap& options,
const SocketOptionMap& options,
const folly::SocketAddress& bindAddr) noexcept {
auto timeoutChrono = std::chrono::milliseconds(timeout);
connect(callback, address, timeoutChrono, timeoutChrono, options, bindAddr);
......@@ -684,7 +685,7 @@ void AsyncSSLSocket::connect(
const folly::SocketAddress& address,
std::chrono::milliseconds connectTimeout,
std::chrono::milliseconds totalConnectTimeout,
const OptionMap& options,
const SocketOptionMap& options,
const folly::SocketAddress& bindAddr) noexcept {
assert(!server_);
assert(state_ == StateEnum::UNINIT);
......
......@@ -22,6 +22,7 @@
#include <folly/String.h>
#include <folly/io/Cursor.h>
#include <folly/io/IOBuf.h>
#include <folly/io/SocketOptionMap.h>
#include <folly/io/async/AsyncPipe.h>
#include <folly/io/async/AsyncSocket.h>
#include <folly/io/async/AsyncTimeout.h>
......@@ -376,7 +377,7 @@ class AsyncSSLSocket : public virtual AsyncSocket {
ConnectCallback* callback,
const folly::SocketAddress& address,
int timeout = 0,
const OptionMap& options = emptyOptionMap,
const SocketOptionMap& options = emptySocketOptionMap,
const folly::SocketAddress& bindAddr = anyAddress()) noexcept override;
/**
......@@ -399,7 +400,7 @@ class AsyncSSLSocket : public virtual AsyncSocket {
const folly::SocketAddress& address,
std::chrono::milliseconds connectTimeout,
std::chrono::milliseconds totalConnectTimeout,
const OptionMap& options = emptyOptionMap,
const SocketOptionMap& options = emptySocketOptionMap,
const folly::SocketAddress& bindAddr = anyAddress()) noexcept;
using AsyncSocket::connect;
......
......@@ -24,6 +24,7 @@
#include <folly/io/Cursor.h>
#include <folly/io/IOBuf.h>
#include <folly/io/IOBufQueue.h>
#include <folly/io/SocketOptionMap.h>
#include <folly/portability/Fcntl.h>
#include <folly/portability/Sockets.h>
#include <folly/portability/SysUio.h>
......@@ -450,7 +451,7 @@ void AsyncSocket::connect(
ConnectCallback* callback,
const folly::SocketAddress& address,
int timeout,
const OptionMap& options,
const SocketOptionMap& options,
const folly::SocketAddress& bindAddr) noexcept {
DestructorGuard dg(this);
eventBase_->dcheckIsInEventBaseThread();
......@@ -529,7 +530,7 @@ void AsyncSocket::connect(
}
// Apply the additional PRE_BIND options if any.
applyOptions(options, OptionKey::ApplyPos::PRE_BIND);
applyOptions(options, SocketOptionKey::ApplyPos::PRE_BIND);
VLOG(5) << "AsyncSocket::connect(this=" << this << ", evb=" << eventBase_
<< ", fd=" << fd_ << ", host=" << address.describe().c_str();
......@@ -560,7 +561,7 @@ void AsyncSocket::connect(
}
// Apply the additional POST_BIND options if any.
applyOptions(options, OptionKey::ApplyPos::POST_BIND);
applyOptions(options, SocketOptionKey::ApplyPos::POST_BIND);
// Call preConnect hook if any.
if (connectCallback_) {
......@@ -655,7 +656,7 @@ void AsyncSocket::connect(
const string& ip,
uint16_t port,
int timeout,
const OptionMap& options) noexcept {
const SocketOptionMap& options) noexcept {
DestructorGuard dg(this);
try {
connectCallback_ = callback;
......@@ -1597,8 +1598,8 @@ void AsyncSocket::cachePeerAddress() const {
}
void AsyncSocket::applyOptions(
const OptionMap& options,
OptionKey::ApplyPos pos) {
const SocketOptionMap& options,
SocketOptionKey::ApplyPos pos) {
for (const auto& opt : options) {
if (opt.first.applyPos_ == pos) {
auto rv = opt.first.apply(fd_, opt.second);
......
......@@ -21,6 +21,7 @@
#include <folly/detail/SocketFastOpen.h>
#include <folly/io/IOBuf.h>
#include <folly/io/ShutdownSocketSet.h>
#include <folly/io/SocketOptionMap.h>
#include <folly/io/async/AsyncSocketException.h>
#include <folly/io/async/AsyncTimeout.h>
#include <folly/io/async/AsyncTransport.h>
......@@ -379,32 +380,10 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
*/
virtual NetworkSocket detachNetworkSocket();
/**
* Uniquely identifies a handle to a socket option value. Each
* combination of level and option name corresponds to one socket
* option value.
*/
class OptionKey {
public:
enum class ApplyPos { POST_BIND = 0, PRE_BIND = 1 };
bool operator<(const OptionKey& other) const {
if (level == other.level) {
return optname < other.optname;
}
return level < other.level;
}
int apply(NetworkSocket fd, int val) const {
return netops::setsockopt(fd, level, optname, &val, sizeof(val));
}
int level;
int optname;
ApplyPos applyPos_{ApplyPos::POST_BIND};
};
// Maps from a socket option key to its value
using OptionMap = std::map<OptionKey, int>;
using OptionKey = SocketOptionKey;
using OptionMap = SocketOptionMap;
static const OptionMap emptyOptionMap;
static const folly::SocketAddress& anyAddress();
/**
......@@ -421,7 +400,7 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
ConnectCallback* callback,
const folly::SocketAddress& address,
int timeout = 0,
const OptionMap& options = emptyOptionMap,
const SocketOptionMap& options = emptySocketOptionMap,
const folly::SocketAddress& bindAddr = anyAddress()) noexcept;
void connect(
......@@ -429,7 +408,7 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
const std::string& ip,
uint16_t port,
int timeout = 0,
const OptionMap& options = emptyOptionMap) noexcept;
const SocketOptionMap& options = emptySocketOptionMap) noexcept;
/**
* If a connect request is in-flight, cancels it and closes the socket
......@@ -1262,7 +1241,9 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
void cacheLocalAddress() const;
void cachePeerAddress() const;
void applyOptions(const OptionMap& options, OptionKey::ApplyPos pos);
void applyOptions(
const SocketOptionMap& options,
SocketOptionKey::ApplyPos pos);
bool isZeroCopyRequest(WriteFlags flags);
......
......@@ -22,6 +22,7 @@
#include <folly/SocketAddress.h>
#include <folly/experimental/TestUtil.h>
#include <folly/fibers/FiberManagerMap.h>
#include <folly/io/SocketOptionMap.h>
#include <folly/io/async/AsyncSSLSocket.h>
#include <folly/io/async/AsyncServerSocket.h>
#include <folly/io/async/AsyncSocket.h>
......@@ -236,7 +237,7 @@ class WriteCheckTimestampCallback : public WriteCallbackBase {
EXPECT_NE(socket_->getNetworkSocket(), NetworkSocket());
int flags = SOF_TIMESTAMPING_OPT_ID | SOF_TIMESTAMPING_OPT_TSONLY |
SOF_TIMESTAMPING_SOFTWARE;
AsyncSocket::OptionKey tstampingOpt = {SOL_SOCKET, SO_TIMESTAMPING};
SocketOptionKey tstampingOpt = {SOL_SOCKET, SO_TIMESTAMPING};
int ret = tstampingOpt.apply(socket_->getNetworkSocket(), flags);
EXPECT_EQ(ret, 0);
}
......
......@@ -19,6 +19,7 @@
#include <folly/ExceptionWrapper.h>
#include <folly/Random.h>
#include <folly/SocketAddress.h>
#include <folly/io/SocketOptionMap.h>
#include <folly/io/async/AsyncTimeout.h>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/ScopedEventBaseThread.h>
......@@ -1148,7 +1149,7 @@ TEST(AsyncSocketTest, WriteErrorCallbackBytesWritten) {
TestServer server(false, kSockBufSize);
AsyncSocket::OptionMap options{
SocketOptionMap options{
{{SOL_SOCKET, SO_SNDBUF}, int(kSockBufSize)},
{{SOL_SOCKET, SO_RCVBUF}, int(kSockBufSize)},
{{IPPROTO_TCP, TCP_NODELAY}, 1},
......@@ -2330,7 +2331,7 @@ TEST(AsyncSocketTest, BufferTest) {
TestServer server;
EventBase evb;
AsyncSocket::OptionMap option{{{SOL_SOCKET, SO_SNDBUF}, 128}};
SocketOptionMap option{{{SOL_SOCKET, SO_SNDBUF}, 128}};
std::shared_ptr<AsyncSocket> socket = AsyncSocket::newSocket(&evb);
ConnCallback ccb;
socket->connect(&ccb, server.getAddress(), 30, option);
......@@ -2360,7 +2361,7 @@ TEST(AsyncSocketTest, BufferTestChain) {
TestServer server;
EventBase evb;
AsyncSocket::OptionMap option{{{SOL_SOCKET, SO_SNDBUF}, 128}};
SocketOptionMap option{{{SOL_SOCKET, SO_SNDBUF}, 128}};
std::shared_ptr<AsyncSocket> socket = AsyncSocket::newSocket(&evb);
ConnCallback ccb;
socket->connect(&ccb, server.getAddress(), 30, option);
......@@ -2399,7 +2400,7 @@ TEST(AsyncSocketTest, BufferTestChain) {
TEST(AsyncSocketTest, BufferCallbackKill) {
TestServer server;
EventBase evb;
AsyncSocket::OptionMap option{{{SOL_SOCKET, SO_SNDBUF}, 128}};
SocketOptionMap option{{{SOL_SOCKET, SO_SNDBUF}, 128}};
std::shared_ptr<AsyncSocket> socket = AsyncSocket::newSocket(&evb);
ConnCallback ccb;
socket->connect(&ccb, server.getAddress(), 30, option);
......@@ -3211,7 +3212,7 @@ TEST_P(AsyncSocketErrMessageCallbackTest, ErrMessageCallback) {
int flags = SOF_TIMESTAMPING_OPT_ID | SOF_TIMESTAMPING_OPT_TSONLY |
SOF_TIMESTAMPING_SOFTWARE | SOF_TIMESTAMPING_OPT_CMSG |
SOF_TIMESTAMPING_TX_SCHED;
AsyncSocket::OptionKey tstampingOpt = {SOL_SOCKET, SO_TIMESTAMPING};
SocketOptionKey tstampingOpt = {SOL_SOCKET, SO_TIMESTAMPING};
EXPECT_EQ(tstampingOpt.apply(socket->getNetworkSocket(), flags), 0);
// write()
......@@ -3640,8 +3641,8 @@ TEST(AsyncSocketTest, V6TosReflectTest) {
EventBase* evb,
folly::SocketAddress sAddr) {
clientSock = AsyncSocket::newSocket(evb);
AsyncSocket::OptionKey v6Opts = {IPPROTO_IPV6, IPV6_TCLASS};
AsyncSocket::OptionMap optionMap;
SocketOptionKey v6Opts = {IPPROTO_IPV6, IPV6_TCLASS};
SocketOptionMap optionMap;
optionMap.insert({v6Opts, 0x2c});
SocketAddress bindAddr("0.0.0.0", 0);
clientSock->connect(ccb, sAddr, 30, optionMap, bindAddr);
......@@ -3724,8 +3725,8 @@ TEST(AsyncSocketTest, V4TosReflectTest) {
EventBase* evb,
folly::SocketAddress sAddr) {
clientSock = AsyncSocket::newSocket(evb);
AsyncSocket::OptionKey v4Opts = {IPPROTO_IP, IP_TOS};
AsyncSocket::OptionMap optionMap;
SocketOptionKey v4Opts = {IPPROTO_IP, IP_TOS};
SocketOptionMap optionMap;
optionMap.insert({v4Opts, 0x2c});
SocketAddress bindAddr("0.0.0.0", 0);
clientSock->connect(ccb, sAddr, 30, optionMap, bindAddr);
......
......@@ -37,13 +37,13 @@ class MockAsyncSSLSocket : public AsyncSSLSocket {
AsyncSocket::ConnectCallback*,
const folly::SocketAddress&,
int,
const OptionMap&,
const folly::SocketOptionMap&,
const folly::SocketAddress&));
void connect(
AsyncSocket::ConnectCallback* callback,
const folly::SocketAddress& address,
int timeout,
const OptionMap& options,
const folly::SocketOptionMap& options,
const folly::SocketAddress& bindAddr) noexcept override {
connect_(callback, address, timeout, options, bindAddr);
}
......
......@@ -36,13 +36,13 @@ class MockAsyncSocket : public AsyncSocket {
AsyncSocket::ConnectCallback*,
const folly::SocketAddress&,
int,
const OptionMap&,
const folly::SocketOptionMap&,
const folly::SocketAddress&));
void connect(
AsyncSocket::ConnectCallback* callback,
const folly::SocketAddress& address,
int timeout,
const OptionMap& options,
const folly::SocketOptionMap& options,
const folly::SocketAddress& bindAddr) noexcept override {
connect_(callback, address, timeout, options, bindAddr);
}
......
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