Added support for TCP Fast Open in server when using Linux.

This commit is contained in:
gavin.smith@coralbay.tv 2018-12-30 00:25:11 +00:00
commit 7d43617993
2 changed files with 43 additions and 0 deletions

View file

@ -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) {