]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/stream_socket_service.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / stream_socket_service.hpp
1 //
2 // stream_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #ifndef BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP
12 #define BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19
20 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
21
22 #include <cstddef>
23 #include <boost/asio/async_result.hpp>
24 #include <boost/asio/detail/type_traits.hpp>
25 #include <boost/asio/error.hpp>
26 #include <boost/asio/io_context.hpp>
27
28 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
29 # include <boost/asio/detail/winrt_ssocket_service.hpp>
30 #elif defined(BOOST_ASIO_HAS_IOCP)
31 # include <boost/asio/detail/win_iocp_socket_service.hpp>
32 #else
33 # include <boost/asio/detail/reactive_socket_service.hpp>
34 #endif
35
36 #include <boost/asio/detail/push_options.hpp>
37
38 namespace boost {
39 namespace asio {
40
41 /// Default service implementation for a stream socket.
42 template <typename Protocol>
43 class stream_socket_service
44 #if defined(GENERATING_DOCUMENTATION)
45 : public boost::asio::io_context::service
46 #else
47 : public boost::asio::detail::service_base<stream_socket_service<Protocol> >
48 #endif
49 {
50 public:
51 #if defined(GENERATING_DOCUMENTATION)
52 /// The unique service identifier.
53 static boost::asio::io_context::id id;
54 #endif
55
56 /// The protocol type.
57 typedef Protocol protocol_type;
58
59 /// The endpoint type.
60 typedef typename Protocol::endpoint endpoint_type;
61
62 private:
63 // The type of the platform-specific implementation.
64 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
65 typedef detail::winrt_ssocket_service<Protocol> service_impl_type;
66 #elif defined(BOOST_ASIO_HAS_IOCP)
67 typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
68 #else
69 typedef detail::reactive_socket_service<Protocol> service_impl_type;
70 #endif
71
72 public:
73 /// The type of a stream socket implementation.
74 #if defined(GENERATING_DOCUMENTATION)
75 typedef implementation_defined implementation_type;
76 #else
77 typedef typename service_impl_type::implementation_type implementation_type;
78 #endif
79
80 /// The native socket type.
81 #if defined(GENERATING_DOCUMENTATION)
82 typedef implementation_defined native_handle_type;
83 #else
84 typedef typename service_impl_type::native_handle_type native_handle_type;
85 #endif
86
87 /// Construct a new stream socket service for the specified io_context.
88 explicit stream_socket_service(boost::asio::io_context& io_context)
89 : boost::asio::detail::service_base<
90 stream_socket_service<Protocol> >(io_context),
91 service_impl_(io_context)
92 {
93 }
94
95 /// Construct a new stream socket implementation.
96 void construct(implementation_type& impl)
97 {
98 service_impl_.construct(impl);
99 }
100
101 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
102 /// Move-construct a new stream socket implementation.
103 void move_construct(implementation_type& impl,
104 implementation_type& other_impl)
105 {
106 service_impl_.move_construct(impl, other_impl);
107 }
108
109 /// Move-assign from another stream socket implementation.
110 void move_assign(implementation_type& impl,
111 stream_socket_service& other_service,
112 implementation_type& other_impl)
113 {
114 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
115 }
116
117 // All socket services have access to each other's implementations.
118 template <typename Protocol1> friend class stream_socket_service;
119
120 /// Move-construct a new stream socket implementation from another protocol
121 /// type.
122 template <typename Protocol1>
123 void converting_move_construct(implementation_type& impl,
124 stream_socket_service<Protocol1>& other_service,
125 typename stream_socket_service<
126 Protocol1>::implementation_type& other_impl,
127 typename enable_if<is_convertible<
128 Protocol1, Protocol>::value>::type* = 0)
129 {
130 service_impl_.template converting_move_construct<Protocol1>(
131 impl, other_service.service_impl_, other_impl);
132 }
133 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
134
135 /// Destroy a stream socket implementation.
136 void destroy(implementation_type& impl)
137 {
138 service_impl_.destroy(impl);
139 }
140
141 /// Open a stream socket.
142 BOOST_ASIO_SYNC_OP_VOID open(implementation_type& impl,
143 const protocol_type& protocol, boost::system::error_code& ec)
144 {
145 if (protocol.type() == BOOST_ASIO_OS_DEF(SOCK_STREAM))
146 service_impl_.open(impl, protocol, ec);
147 else
148 ec = boost::asio::error::invalid_argument;
149 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
150 }
151
152 /// Assign an existing native socket to a stream socket.
153 BOOST_ASIO_SYNC_OP_VOID assign(implementation_type& impl,
154 const protocol_type& protocol, const native_handle_type& native_socket,
155 boost::system::error_code& ec)
156 {
157 service_impl_.assign(impl, protocol, native_socket, ec);
158 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
159 }
160
161 /// Determine whether the socket is open.
162 bool is_open(const implementation_type& impl) const
163 {
164 return service_impl_.is_open(impl);
165 }
166
167 /// Close a stream socket implementation.
168 BOOST_ASIO_SYNC_OP_VOID close(implementation_type& impl,
169 boost::system::error_code& ec)
170 {
171 service_impl_.close(impl, ec);
172 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
173 }
174
175 /// Release ownership of the underlying socket.
176 native_handle_type release(implementation_type& impl,
177 boost::system::error_code& ec)
178 {
179 return service_impl_.release(impl, ec);
180 }
181
182 /// Get the native socket implementation.
183 native_handle_type native_handle(implementation_type& impl)
184 {
185 return service_impl_.native_handle(impl);
186 }
187
188 /// Cancel all asynchronous operations associated with the socket.
189 BOOST_ASIO_SYNC_OP_VOID cancel(implementation_type& impl,
190 boost::system::error_code& ec)
191 {
192 service_impl_.cancel(impl, ec);
193 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
194 }
195
196 /// Determine whether the socket is at the out-of-band data mark.
197 bool at_mark(const implementation_type& impl,
198 boost::system::error_code& ec) const
199 {
200 return service_impl_.at_mark(impl, ec);
201 }
202
203 /// Determine the number of bytes available for reading.
204 std::size_t available(const implementation_type& impl,
205 boost::system::error_code& ec) const
206 {
207 return service_impl_.available(impl, ec);
208 }
209
210 /// Bind the stream socket to the specified local endpoint.
211 BOOST_ASIO_SYNC_OP_VOID bind(implementation_type& impl,
212 const endpoint_type& endpoint, boost::system::error_code& ec)
213 {
214 service_impl_.bind(impl, endpoint, ec);
215 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
216 }
217
218 /// Connect the stream socket to the specified endpoint.
219 BOOST_ASIO_SYNC_OP_VOID connect(implementation_type& impl,
220 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
221 {
222 service_impl_.connect(impl, peer_endpoint, ec);
223 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
224 }
225
226 /// Start an asynchronous connect.
227 template <typename ConnectHandler>
228 BOOST_ASIO_INITFN_RESULT_TYPE(ConnectHandler,
229 void (boost::system::error_code))
230 async_connect(implementation_type& impl,
231 const endpoint_type& peer_endpoint,
232 BOOST_ASIO_MOVE_ARG(ConnectHandler) handler)
233 {
234 async_completion<ConnectHandler,
235 void (boost::system::error_code)> init(handler);
236
237 service_impl_.async_connect(impl, peer_endpoint, init.completion_handler);
238
239 return init.result.get();
240 }
241
242 /// Set a socket option.
243 template <typename SettableSocketOption>
244 BOOST_ASIO_SYNC_OP_VOID set_option(implementation_type& impl,
245 const SettableSocketOption& option, boost::system::error_code& ec)
246 {
247 service_impl_.set_option(impl, option, ec);
248 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
249 }
250
251 /// Get a socket option.
252 template <typename GettableSocketOption>
253 BOOST_ASIO_SYNC_OP_VOID get_option(const implementation_type& impl,
254 GettableSocketOption& option, boost::system::error_code& ec) const
255 {
256 service_impl_.get_option(impl, option, ec);
257 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
258 }
259
260 /// Perform an IO control command on the socket.
261 template <typename IoControlCommand>
262 BOOST_ASIO_SYNC_OP_VOID io_control(implementation_type& impl,
263 IoControlCommand& command, boost::system::error_code& ec)
264 {
265 service_impl_.io_control(impl, command, ec);
266 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
267 }
268
269 /// Gets the non-blocking mode of the socket.
270 bool non_blocking(const implementation_type& impl) const
271 {
272 return service_impl_.non_blocking(impl);
273 }
274
275 /// Sets the non-blocking mode of the socket.
276 BOOST_ASIO_SYNC_OP_VOID non_blocking(implementation_type& impl,
277 bool mode, boost::system::error_code& ec)
278 {
279 service_impl_.non_blocking(impl, mode, ec);
280 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
281 }
282
283 /// Gets the non-blocking mode of the native socket implementation.
284 bool native_non_blocking(const implementation_type& impl) const
285 {
286 return service_impl_.native_non_blocking(impl);
287 }
288
289 /// Sets the non-blocking mode of the native socket implementation.
290 BOOST_ASIO_SYNC_OP_VOID native_non_blocking(implementation_type& impl,
291 bool mode, boost::system::error_code& ec)
292 {
293 service_impl_.native_non_blocking(impl, mode, ec);
294 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
295 }
296
297 /// Get the local endpoint.
298 endpoint_type local_endpoint(const implementation_type& impl,
299 boost::system::error_code& ec) const
300 {
301 return service_impl_.local_endpoint(impl, ec);
302 }
303
304 /// Get the remote endpoint.
305 endpoint_type remote_endpoint(const implementation_type& impl,
306 boost::system::error_code& ec) const
307 {
308 return service_impl_.remote_endpoint(impl, ec);
309 }
310
311 /// Disable sends or receives on the socket.
312 BOOST_ASIO_SYNC_OP_VOID shutdown(implementation_type& impl,
313 socket_base::shutdown_type what, boost::system::error_code& ec)
314 {
315 service_impl_.shutdown(impl, what, ec);
316 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
317 }
318
319 /// Wait for the socket to become ready to read, ready to write, or to have
320 /// pending error conditions.
321 BOOST_ASIO_SYNC_OP_VOID wait(implementation_type& impl,
322 socket_base::wait_type w, boost::system::error_code& ec)
323 {
324 service_impl_.wait(impl, w, ec);
325 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
326 }
327
328 /// Asynchronously wait for the socket to become ready to read, ready to
329 /// write, or to have pending error conditions.
330 template <typename WaitHandler>
331 BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
332 void (boost::system::error_code))
333 async_wait(implementation_type& impl, socket_base::wait_type w,
334 BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
335 {
336 async_completion<WaitHandler,
337 void (boost::system::error_code)> init(handler);
338
339 service_impl_.async_wait(impl, w, init.completion_handler);
340
341 return init.result.get();
342 }
343
344 /// Send the given data to the peer.
345 template <typename ConstBufferSequence>
346 std::size_t send(implementation_type& impl,
347 const ConstBufferSequence& buffers,
348 socket_base::message_flags flags, boost::system::error_code& ec)
349 {
350 return service_impl_.send(impl, buffers, flags, ec);
351 }
352
353 /// Start an asynchronous send.
354 template <typename ConstBufferSequence, typename WriteHandler>
355 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
356 void (boost::system::error_code, std::size_t))
357 async_send(implementation_type& impl,
358 const ConstBufferSequence& buffers,
359 socket_base::message_flags flags,
360 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
361 {
362 async_completion<WriteHandler,
363 void (boost::system::error_code, std::size_t)> init(handler);
364
365 service_impl_.async_send(impl, buffers, flags, init.completion_handler);
366
367 return init.result.get();
368 }
369
370 /// Receive some data from the peer.
371 template <typename MutableBufferSequence>
372 std::size_t receive(implementation_type& impl,
373 const MutableBufferSequence& buffers,
374 socket_base::message_flags flags, boost::system::error_code& ec)
375 {
376 return service_impl_.receive(impl, buffers, flags, ec);
377 }
378
379 /// Start an asynchronous receive.
380 template <typename MutableBufferSequence, typename ReadHandler>
381 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
382 void (boost::system::error_code, std::size_t))
383 async_receive(implementation_type& impl,
384 const MutableBufferSequence& buffers,
385 socket_base::message_flags flags,
386 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
387 {
388 async_completion<ReadHandler,
389 void (boost::system::error_code, std::size_t)> init(handler);
390
391 service_impl_.async_receive(impl, buffers, flags, init.completion_handler);
392
393 return init.result.get();
394 }
395
396 private:
397 // Destroy all user-defined handler objects owned by the service.
398 void shutdown()
399 {
400 service_impl_.shutdown();
401 }
402
403 // The platform-specific implementation.
404 service_impl_type service_impl_;
405 };
406
407 } // namespace asio
408 } // namespace boost
409
410 #include <boost/asio/detail/pop_options.hpp>
411
412 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
413
414 #endif // BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP