LIBWIRE
Next-generation C++17 networking library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
options.hpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2018 Max Mazurov (fox.cpp) <fox.cpp [at] disroot [dot] org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 #pragma once
24 
25 #include <chrono>
26 #include <tuple>
27 
28 /*
29  * If you had to open this file to find answer for your question - we are so
30  * sorry. Please open issue with your question so we can update documentation
31  * to answer it.
32  */
33 
41 namespace libwire::tcp {
42  class socket;
43 
47  inline namespace options {
51  struct no_delay_t {
52  static void set(socket&, bool enabled) noexcept;
53 
54  static bool get(const socket&) noexcept;
55  };
56 
66  constexpr no_delay_t no_delay{};
67 
71  struct timeout_t {
72  template<typename Duration>
73  static void set(socket& socket, Duration d) noexcept {
74  namespace ch = std::chrono;
75 
76  set_impl(socket, ch::duration_cast<ch::milliseconds>(d));
77  }
78  static std::chrono::milliseconds get(const socket&) noexcept;
79 
80  private:
81  static void set_impl(socket&, std::chrono::milliseconds) noexcept;
82  };
83 
101  constexpr timeout_t timeout{};
102 
106  struct keep_alive_t {
107  static void set(socket&, bool) noexcept;
108  static bool get(const socket&) noexcept;
109  };
110 
119 
123  struct linger_t {
124  template<typename Duration>
125  static void set(socket& sock, bool enabled, Duration timeout) noexcept {
126  namespace ch = std::chrono;
127 
128  set_impl(sock, enabled, ch::duration_cast<ch::seconds>(timeout));
129  }
130 
131  static std::tuple<bool, std::chrono::seconds> get(const socket&) noexcept;
132 
133  private:
134  static void set_impl(socket&, bool enabled, std::chrono::seconds timeout) noexcept;
135  };
136 
145  constexpr linger_t linger{};
146  } // namespace options
147 } // namespace libwire::tcp
constexpr linger_t linger
Enable linger on socket.close on TCP socket.
Definition: options.hpp:145
Dummy type for timeout option.
Definition: options.hpp:71
static void set(socket &socket, Duration d) noexcept
Definition: options.hpp:73
Dummy type for keep_alive option.
Definition: options.hpp:106
constexpr keep_alive_t keep_alive
Enable keep-alive probes on TCP socket.
Definition: options.hpp:118
Dummy type for no_delay option.
Definition: options.hpp:51
Dummy type for linger option.
Definition: options.hpp:123
static void set(socket &sock, bool enabled, Duration timeout) noexcept
Definition: options.hpp:125
constexpr no_delay_t no_delay
Disable the Nagle algorithm on TCP socket.
Definition: options.hpp:66
Descriptor wrapper for TCP socket.
Definition: socket.hpp:59
constexpr timeout_t timeout
Set transmission timeout for TCP socket.
Definition: options.hpp:101
static void set(socket &, bool enabled) noexcept