mqtt_cpp
tcp_endpoint.hpp
Go to the documentation of this file.
1 // Copyright Takatoshi Kondo 2017
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #if !defined(MQTT_TCP_ENDPOINT_HPP)
8 #define MQTT_TCP_ENDPOINT_HPP
9 
10 #include <boost/asio.hpp>
11 #include <boost/asio/bind_executor.hpp>
12 
13 #include <mqtt/namespace.hpp>
15 #include <mqtt/move.hpp>
16 #include <mqtt/attributes.hpp>
17 
18 namespace MQTT_NS {
19 
20 namespace as = boost::asio;
21 
22 template <typename Socket, typename Strand>
23 class tcp_endpoint : public socket {
24 public:
25  template <typename... Args>
26  tcp_endpoint(as::io_context& ioc, Args&&... args)
27  :tcp_(ioc, std::forward<Args>(args)...),
28  strand_(ioc) {
29  }
30 
32  as::mutable_buffer buffers,
33  std::function<void(error_code, std::size_t)> handler
34  ) override final {
35  as::async_read(
36  tcp_,
37  force_move(buffers),
38  as::bind_executor(
39  strand_,
40  force_move(handler)
41  )
42  );
43  }
44 
46  std::vector<as::const_buffer> buffers,
47  std::function<void(error_code, std::size_t)> handler
48  ) override final {
49  as::async_write(
50  tcp_,
51  force_move(buffers),
52  as::bind_executor(
53  strand_,
54  force_move(handler)
55  )
56  );
57  }
58 
60  std::vector<as::const_buffer> buffers,
62  ) override final {
63  return as::write(tcp_,force_move(buffers), ec);
64  }
65 
66  MQTT_ALWAYS_INLINE void post(std::function<void()> handler) override final {
67  as::post(
68  strand_,
69  force_move(handler)
70  );
71  }
72 
73  MQTT_ALWAYS_INLINE as::ip::tcp::socket::lowest_layer_type& lowest_layer() override final {
74  return tcp_.lowest_layer();
75  }
76 
77  MQTT_ALWAYS_INLINE any native_handle() override final {
78  return tcp_.native_handle();
79  }
80 
82  tcp_.lowest_layer().close(ec);
83  }
84 
85 #if BOOST_VERSION < 107400 || defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
86  MQTT_ALWAYS_INLINE as::executor get_executor() override final {
87  return lowest_layer().get_executor();
88  }
89 #else // BOOST_VERSION < 107400 || defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
90  MQTT_ALWAYS_INLINE as::any_io_executor get_executor() override final {
91  return lowest_layer().get_executor();
92  }
93 #endif // BOOST_VERSION < 107400 || defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
94 
95  auto& socket() { return tcp_; }
96  auto const& socket() const { return tcp_; }
97 
98  template <typename... Args>
99  void set_option(Args&& ... args) {
100  tcp_.set_option(std::forward<Args>(args)...);
101  }
102 
103  template <typename... Args>
104  void async_accept(Args&& ... args) {
105  tcp_.async_accept(std::forward<Args>(args)...);
106  }
107 
108 #if defined(MQTT_USE_TLS)
109 
110  template <typename... Args>
111  void handshake(Args&& ... args) {
112  tcp_.handshake(std::forward<Args>(args)...);
113  }
114 
115  template <typename... Args>
116  void async_handshake(Args&& ... args) {
117  tcp_.async_handshake(std::forward<Args>(args)...);
118  }
119 
120 #endif // defined(MQTT_USE_TLS)
121 
122 private:
123  Socket tcp_;
124  Strand strand_;
125 };
126 
127 } // namespace MQTT_NS
128 
129 #endif // MQTT_TCP_ENDPOINT_HPP
#define MQTT_ALWAYS_INLINE
Definition: attributes.hpp:18
Definition: type_erased_socket.hpp:22
Definition: tcp_endpoint.hpp:23
MQTT_ALWAYS_INLINE as::executor get_executor() override final
Definition: tcp_endpoint.hpp:86
MQTT_ALWAYS_INLINE void close(boost::system::error_code &ec) override final
Definition: tcp_endpoint.hpp:81
MQTT_ALWAYS_INLINE std::size_t write(std::vector< as::const_buffer > buffers, boost::system::error_code &ec) override final
Definition: tcp_endpoint.hpp:59
MQTT_ALWAYS_INLINE any native_handle() override final
Definition: tcp_endpoint.hpp:77
void async_accept(Args &&... args)
Definition: tcp_endpoint.hpp:104
MQTT_ALWAYS_INLINE void async_read(as::mutable_buffer buffers, std::function< void(error_code, std::size_t)> handler) override final
Definition: tcp_endpoint.hpp:31
MQTT_ALWAYS_INLINE as::ip::tcp::socket::lowest_layer_type & lowest_layer() override final
Definition: tcp_endpoint.hpp:73
void set_option(Args &&... args)
Definition: tcp_endpoint.hpp:99
auto const & socket() const
Definition: tcp_endpoint.hpp:96
MQTT_ALWAYS_INLINE void post(std::function< void()> handler) override final
Definition: tcp_endpoint.hpp:66
tcp_endpoint(as::io_context &ioc, Args &&... args)
Definition: tcp_endpoint.hpp:26
MQTT_ALWAYS_INLINE void async_write(std::vector< as::const_buffer > buffers, std::function< void(error_code, std::size_t)> handler) override final
Definition: tcp_endpoint.hpp:45
auto & socket()
Definition: tcp_endpoint.hpp:95
Definition: any.hpp:27
boost::system::error_code error_code
Definition: error_code.hpp:16
constexpr std::remove_reference_t< T > && force_move(T &&t)
Definition: move.hpp:20
Definition: buffer.hpp:242