Added support for TCP Fast Open in server when using Linux.
This commit is contained in:
parent
1654b3cd42
commit
7d43617993
2 changed files with 43 additions and 0 deletions
|
|
@ -351,6 +351,8 @@ namespace SimpleWeb {
|
|||
std::string address;
|
||||
/// Set to false to avoid binding the socket to an address that is already in use. Defaults to true.
|
||||
bool reuse_address = true;
|
||||
/// Makes use of RFC 7413 or TCP Fast Open (TFO)
|
||||
bool fast_open = false;
|
||||
};
|
||||
/// Set before calling start().
|
||||
Config config;
|
||||
|
|
@ -399,6 +401,14 @@ namespace SimpleWeb {
|
|||
acceptor = std::unique_ptr<asio::ip::tcp::acceptor>(new asio::ip::tcp::acceptor(*io_service));
|
||||
acceptor->open(endpoint.protocol());
|
||||
acceptor->set_option(asio::socket_base::reuse_address(config.reuse_address));
|
||||
if (config.fast_open && is_tcp_fast_open_supported(connection_mode::server))
|
||||
{
|
||||
#if defined(__linux__) && defined(TCP_FASTOPEN)
|
||||
const int qlen = 5; // This seems to be the value that is used in other examples.
|
||||
boost::system::error_code ec;
|
||||
acceptor->set_option(boost::asio::detail::socket_option::integer<IPPROTO_TCP, TCP_FASTOPEN>(qlen), ec);
|
||||
#endif // End Linux
|
||||
}
|
||||
acceptor->bind(endpoint);
|
||||
|
||||
after_bind();
|
||||
|
|
|
|||
33
utility.hpp
33
utility.hpp
|
|
@ -8,6 +8,10 @@
|
|||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#if defined(__linux__)
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#endif
|
||||
|
||||
#if __cplusplus > 201402L || _MSVC_LANG > 201402L
|
||||
#include <string_view>
|
||||
|
|
@ -26,6 +30,35 @@ namespace SimpleWeb {
|
|||
#endif
|
||||
|
||||
namespace SimpleWeb {
|
||||
enum class connection_mode { client = 1, server = 2 };
|
||||
inline bool is_tcp_fast_open_supported(const connection_mode mode)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
std::ifstream ifs("/proc/sys/net/ipv4/tcp_fastopen");
|
||||
if (!ifs.is_open()) { return false; }
|
||||
std::string line;
|
||||
if (!std::getline(ifs, line)) { return false; }
|
||||
std::istringstream iss(line);
|
||||
uint8_t value = 0;
|
||||
iss >> value;
|
||||
if (iss.fail()) { return false; } // Should never happen in theory.
|
||||
auto mode_matches_request = [](const connection_mode mode, const uint8_t value)
|
||||
{
|
||||
const auto m = static_cast<uint8_t>(mode);
|
||||
if (mode == connection_mode::client ||
|
||||
mode == connection_mode::server)
|
||||
{
|
||||
return (m & value) != 0;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
return mode_matches_request(mode, value);
|
||||
#else
|
||||
(void)mode;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline bool case_insensitive_equal(const std::string &str1, const std::string &str2) noexcept {
|
||||
return str1.size() == str2.size() &&
|
||||
std::equal(str1.begin(), str1.end(), str2.begin(), [](char a, char b) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue