CppWAMP
C++11 client library for the WAMP protocol
tcphost.hpp
Go to the documentation of this file.
1 /*------------------------------------------------------------------------------
2  Copyright Butterfly Energy Systems 2014-2015, 2022.
3  Distributed under the Boost Software License, Version 1.0.
4  http://www.boost.org/LICENSE_1_0.txt
5 ------------------------------------------------------------------------------*/
6 
7 #ifndef CPPWAMP_TCPHOST_HPP
8 #define CPPWAMP_TCPHOST_HPP
9 
10 //------------------------------------------------------------------------------
14 //------------------------------------------------------------------------------
15 
16 #include <string>
17 #include "api.hpp"
18 #include "config.hpp"
19 #include "rawsockoptions.hpp"
20 #include "internal/socketoptions.hpp"
21 
22 // Forward declaration
23 namespace boost { namespace asio { namespace ip { class tcp; }}}
24 
25 namespace wamp
26 {
27 
28 namespace internal { class TcpOpener; } // Forward declaration
29 
30 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
37 class CPPWAMP_API TcpOptions
38 {
39 public:
41  TcpOptions& withBroadcast(bool enabled = true);
42 
44  TcpOptions& withDebug(bool enabled = true);
45 
47  TcpOptions& withDoNotRoute(bool enabled = true);
48 
50  TcpOptions& withKeepAlive(bool enabled = true);
51 
53  TcpOptions& withLinger(bool enabled, int timeout);
54 
56  TcpOptions& withOutOfBandInline(bool enabled);
57 
59  TcpOptions& withReceiveBufferSize(int size);
60 
62  TcpOptions& withReceiveLowWatermark(int size);
63 
65  TcpOptions& withReuseAddress(bool enabled = true);
66 
68  TcpOptions& withSendBufferSize(int size);
69 
71  TcpOptions& withSendLowWatermark(int size);
72 
74  TcpOptions& withUnicastHops(int hops);
75 
77  TcpOptions& withIpV6Only(bool enabled = true);
78 
81  TcpOptions& withNoDelay(bool enabled = true);
82 
83 private:
84  template <typename TOption, typename... TArgs>
85  TcpOptions& set(TArgs... args);
86 
87  template <typename TSocket> void applyTo(TSocket& socket) const;
88 
89  internal::SocketOptionList<boost::asio::ip::tcp> options_;
90 
91  friend class internal::TcpOpener;
92 
93  /* Implementation note: Explicit template instantiation does not seem
94  to play nice with CRTP, so it was not feasible to factor out the
95  commonality with UdsOptions as a mixin (not without giving up the
96  fluent API). */
97 };
98 
99 //------------------------------------------------------------------------------
102 //------------------------------------------------------------------------------
103 class CPPWAMP_API TcpHost
104 {
105 public:
107  static constexpr RawsockMaxLength defaultMaxRxLength =
109 
111  TcpHost(
112  std::string hostName,
113  std::string serviceName,
114  TcpOptions options = {},
115  RawsockMaxLength maxRxLength
116  = defaultMaxRxLength
117  );
118 
120  TcpHost(
121  std::string hostName,
122  unsigned short port,
123  TcpOptions options = {},
124  RawsockMaxLength maxRxLength
125  = defaultMaxRxLength
126  );
127 
129  TcpHost& withOptions(TcpOptions options);
130 
132  TcpHost& withMaxRxLength(RawsockMaxLength length);
133 
135  const std::string& hostName() const;
136 
138  const std::string& serviceName() const;
139 
141  const TcpOptions& options() const;
142 
144  RawsockMaxLength maxRxLength() const;
145 
148 
151  CPPWAMP_DEPRECATED TcpHost& withBroadcast(bool enabled = true)
152  {
153  options_.withBroadcast(enabled);
154  return *this;
155  }
156 
158  CPPWAMP_DEPRECATED TcpHost& withDebug(bool enabled = true)
159  {
160  options_.withDebug(enabled);
161  return *this;
162  }
163 
165  CPPWAMP_DEPRECATED TcpHost& withDoNotRoute(bool enabled = true)
166  {
167  options_.withDoNotRoute(enabled);
168  return *this;
169  }
170 
172  CPPWAMP_DEPRECATED TcpHost& withKeepAlive(bool enabled = true)
173  {
174  options_.withKeepAlive(enabled);
175  return *this;
176  }
177 
179  CPPWAMP_DEPRECATED TcpHost& withLinger(bool enabled, int timeout)
180  {
181  options_.withLinger(enabled, timeout);
182  return *this;
183  }
184 
186  CPPWAMP_DEPRECATED TcpHost& withOutOfBandInline(bool enabled)
187  {
188  options_.withOutOfBandInline(enabled);
189  return *this;
190  }
191 
193  CPPWAMP_DEPRECATED TcpHost& withReceiveBufferSize(int size)
194  {
195  options_.withReceiveBufferSize(size);
196  return *this;
197  }
198 
200  CPPWAMP_DEPRECATED TcpHost& withReceiveLowWatermark(int size)
201  {
202  options_.withReceiveLowWatermark(size);
203  return *this;
204  }
205 
207  CPPWAMP_DEPRECATED TcpHost& withReuseAddress(bool enabled = true)
208  {
209  options_.withReuseAddress(enabled);
210  return *this;
211  }
212 
214  CPPWAMP_DEPRECATED TcpHost& withSendBufferSize(int size)
215  {
216  options_.withSendBufferSize(size);
217  return *this;
218  }
219 
221  CPPWAMP_DEPRECATED TcpHost& withSendLowWatermark(int size)
222  {
223  options_.withSendLowWatermark(size);
224  return *this;
225  }
226 
228  CPPWAMP_DEPRECATED TcpHost& withUnicastHops(int hops)
229  {
230  options_.withUnicastHops(hops);
231  return *this;
232  }
233 
235  CPPWAMP_DEPRECATED TcpHost& withIpV6Only(bool enabled = true)
236  {
237  options_.withIpV6Only(enabled);
238  return *this;
239  }
240 
242  CPPWAMP_DEPRECATED TcpHost& withNoDelay(bool enabled = true)
243  {
244  options_.withNoDelay(enabled);
245  return *this;
246  }
248 
249 private:
250  std::string hostName_;
251  std::string serviceName_;
252  TcpOptions options_;
253  RawsockMaxLength maxRxLength_;
254 };
255 
256 } // namespace wamp
257 
258 #ifndef CPPWAMP_COMPILED_LIB
259 #include "./internal/tcphost.ipp"
260 #endif
261 
262 #endif // CPPWAMP_TCPHOST_HPP
wamp::TcpHost::withBroadcast
TcpHost & withBroadcast(bool enabled=true)
The following setters are deprecated.
Definition: tcphost.hpp:151
wamp::TcpHost::withReceiveBufferSize
TcpHost & withReceiveBufferSize(int size)
Adds the SO_RCVBUF socket option.
Definition: tcphost.hpp:193
wamp::TcpHost::withOutOfBandInline
TcpHost & withOutOfBandInline(bool enabled)
Adds the SO_OOBINLINE socket option.
Definition: tcphost.hpp:186
wamp::RawsockMaxLength::MB_16
@ MB_16
16 megabytes
wamp::TcpHost::withUnicastHops
TcpHost & withUnicastHops(int hops)
Adds the IP_UNICAST_TTL socket option.
Definition: tcphost.hpp:228
wamp::TcpHost::withIpV6Only
TcpHost & withIpV6Only(bool enabled=true)
Adds the IP_V6ONLY socket option.
Definition: tcphost.hpp:235
wamp::TcpHost::withLinger
TcpHost & withLinger(bool enabled, int timeout)
Adds the SO_LINGER socket option.
Definition: tcphost.hpp:179
wamp::TcpHost::withDoNotRoute
TcpHost & withDoNotRoute(bool enabled=true)
Adds the SO_DONTROUTE socket option.
Definition: tcphost.hpp:165
api.hpp
Defines macros related to exporting/importing APIs.
wamp::TcpHost::withKeepAlive
TcpHost & withKeepAlive(bool enabled=true)
Adds the SO_KEEPALIVE socket option.
Definition: tcphost.hpp:172
wamp::TcpHost::withSendLowWatermark
TcpHost & withSendLowWatermark(int size)
Adds the SO_SNDLOWAT socket option.
Definition: tcphost.hpp:221
wamp::TcpHost
Contains TCP host address information, as well as other socket options.
Definition: tcphost.hpp:103
wamp
Definition: anyhandler.hpp:36
rawsockoptions.hpp
Contains common option definitions for raw socket transports.
wamp::RawsockMaxLength
RawsockMaxLength
Enumerators used to specify the maximum length of messages that a raw socket transport can receive.
Definition: rawsockoptions.hpp:22
wamp::TcpHost::withReceiveLowWatermark
TcpHost & withReceiveLowWatermark(int size)
Adds the SO_RCVLOWAT socket option.
Definition: tcphost.hpp:200
wamp::TcpHost::withDebug
TcpHost & withDebug(bool enabled=true)
Adds the SO_DEBUG socket option.
Definition: tcphost.hpp:158
wamp::TcpHost::withReuseAddress
TcpHost & withReuseAddress(bool enabled=true)
Adds the SO_REUSEADDR socket option.
Definition: tcphost.hpp:207
wamp::TcpHost::withSendBufferSize
TcpHost & withSendBufferSize(int size)
Adds the SO_SNDBUF socket option.
Definition: tcphost.hpp:214
wamp::TcpOptions
Contains options for the TCP transport.
Definition: tcphost.hpp:37
wamp::TcpHost::withNoDelay
TcpHost & withNoDelay(bool enabled=true)
Adds the TCP_NODELAY socket option.
Definition: tcphost.hpp:242