]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/basic_socket.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / asio / basic_socket.hpp
1 //
2 // basic_socket.hpp
3 // ~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2022 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_BASIC_SOCKET_HPP
12 #define BOOST_ASIO_BASIC_SOCKET_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/any_io_executor.hpp>
19 #include <boost/asio/detail/config.hpp>
20 #include <boost/asio/async_result.hpp>
21 #include <boost/asio/detail/handler_type_requirements.hpp>
22 #include <boost/asio/detail/io_object_impl.hpp>
23 #include <boost/asio/detail/non_const_lvalue.hpp>
24 #include <boost/asio/detail/throw_error.hpp>
25 #include <boost/asio/detail/type_traits.hpp>
26 #include <boost/asio/error.hpp>
27 #include <boost/asio/execution_context.hpp>
28 #include <boost/asio/post.hpp>
29 #include <boost/asio/socket_base.hpp>
30
31 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
32 # include <boost/asio/detail/null_socket_service.hpp>
33 #elif defined(BOOST_ASIO_HAS_IOCP)
34 # include <boost/asio/detail/win_iocp_socket_service.hpp>
35 #elif defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
36 # include <boost/asio/detail/io_uring_socket_service.hpp>
37 #else
38 # include <boost/asio/detail/reactive_socket_service.hpp>
39 #endif
40
41 #if defined(BOOST_ASIO_HAS_MOVE)
42 # include <utility>
43 #endif // defined(BOOST_ASIO_HAS_MOVE)
44
45 #include <boost/asio/detail/push_options.hpp>
46
47 namespace boost {
48 namespace asio {
49
50 #if !defined(BOOST_ASIO_BASIC_SOCKET_FWD_DECL)
51 #define BOOST_ASIO_BASIC_SOCKET_FWD_DECL
52
53 // Forward declaration with defaulted arguments.
54 template <typename Protocol, typename Executor = any_io_executor>
55 class basic_socket;
56
57 #endif // !defined(BOOST_ASIO_BASIC_SOCKET_FWD_DECL)
58
59 /// Provides socket functionality.
60 /**
61 * The basic_socket class template provides functionality that is common to both
62 * stream-oriented and datagram-oriented sockets.
63 *
64 * @par Thread Safety
65 * @e Distinct @e objects: Safe.@n
66 * @e Shared @e objects: Unsafe.
67 */
68 template <typename Protocol, typename Executor>
69 class basic_socket
70 : public socket_base
71 {
72 public:
73 /// The type of the executor associated with the object.
74 typedef Executor executor_type;
75
76 /// Rebinds the socket type to another executor.
77 template <typename Executor1>
78 struct rebind_executor
79 {
80 /// The socket type when rebound to the specified executor.
81 typedef basic_socket<Protocol, Executor1> other;
82 };
83
84 /// The native representation of a socket.
85 #if defined(GENERATING_DOCUMENTATION)
86 typedef implementation_defined native_handle_type;
87 #elif defined(BOOST_ASIO_WINDOWS_RUNTIME)
88 typedef typename detail::null_socket_service<
89 Protocol>::native_handle_type native_handle_type;
90 #elif defined(BOOST_ASIO_HAS_IOCP)
91 typedef typename detail::win_iocp_socket_service<
92 Protocol>::native_handle_type native_handle_type;
93 #elif defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
94 typedef typename detail::io_uring_socket_service<
95 Protocol>::native_handle_type native_handle_type;
96 #else
97 typedef typename detail::reactive_socket_service<
98 Protocol>::native_handle_type native_handle_type;
99 #endif
100
101 /// The protocol type.
102 typedef Protocol protocol_type;
103
104 /// The endpoint type.
105 typedef typename Protocol::endpoint endpoint_type;
106
107 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
108 /// A basic_socket is always the lowest layer.
109 typedef basic_socket<Protocol, Executor> lowest_layer_type;
110 #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
111
112 /// Construct a basic_socket without opening it.
113 /**
114 * This constructor creates a socket without opening it.
115 *
116 * @param ex The I/O executor that the socket will use, by default, to
117 * dispatch handlers for any asynchronous operations performed on the socket.
118 */
119 explicit basic_socket(const executor_type& ex)
120 : impl_(0, ex)
121 {
122 }
123
124 /// Construct a basic_socket without opening it.
125 /**
126 * This constructor creates a socket without opening it.
127 *
128 * @param context An execution context which provides the I/O executor that
129 * the socket will use, by default, to dispatch handlers for any asynchronous
130 * operations performed on the socket.
131 */
132 template <typename ExecutionContext>
133 explicit basic_socket(ExecutionContext& context,
134 typename constraint<
135 is_convertible<ExecutionContext&, execution_context&>::value
136 >::type = 0)
137 : impl_(0, 0, context)
138 {
139 }
140
141 /// Construct and open a basic_socket.
142 /**
143 * This constructor creates and opens a socket.
144 *
145 * @param ex The I/O executor that the socket will use, by default, to
146 * dispatch handlers for any asynchronous operations performed on the socket.
147 *
148 * @param protocol An object specifying protocol parameters to be used.
149 *
150 * @throws boost::system::system_error Thrown on failure.
151 */
152 basic_socket(const executor_type& ex, const protocol_type& protocol)
153 : impl_(0, ex)
154 {
155 boost::system::error_code ec;
156 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
157 boost::asio::detail::throw_error(ec, "open");
158 }
159
160 /// Construct and open a basic_socket.
161 /**
162 * This constructor creates and opens a socket.
163 *
164 * @param context An execution context which provides the I/O executor that
165 * the socket will use, by default, to dispatch handlers for any asynchronous
166 * operations performed on the socket.
167 *
168 * @param protocol An object specifying protocol parameters to be used.
169 *
170 * @throws boost::system::system_error Thrown on failure.
171 */
172 template <typename ExecutionContext>
173 basic_socket(ExecutionContext& context, const protocol_type& protocol,
174 typename constraint<
175 is_convertible<ExecutionContext&, execution_context&>::value,
176 defaulted_constraint
177 >::type = defaulted_constraint())
178 : impl_(0, 0, context)
179 {
180 boost::system::error_code ec;
181 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
182 boost::asio::detail::throw_error(ec, "open");
183 }
184
185 /// Construct a basic_socket, opening it and binding it to the given local
186 /// endpoint.
187 /**
188 * This constructor creates a socket and automatically opens it bound to the
189 * specified endpoint on the local machine. The protocol used is the protocol
190 * associated with the given endpoint.
191 *
192 * @param ex The I/O executor that the socket will use, by default, to
193 * dispatch handlers for any asynchronous operations performed on the socket.
194 *
195 * @param endpoint An endpoint on the local machine to which the socket will
196 * be bound.
197 *
198 * @throws boost::system::system_error Thrown on failure.
199 */
200 basic_socket(const executor_type& ex, const endpoint_type& endpoint)
201 : impl_(0, ex)
202 {
203 boost::system::error_code ec;
204 const protocol_type protocol = endpoint.protocol();
205 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
206 boost::asio::detail::throw_error(ec, "open");
207 impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
208 boost::asio::detail::throw_error(ec, "bind");
209 }
210
211 /// Construct a basic_socket, opening it and binding it to the given local
212 /// endpoint.
213 /**
214 * This constructor creates a socket and automatically opens it bound to the
215 * specified endpoint on the local machine. The protocol used is the protocol
216 * associated with the given endpoint.
217 *
218 * @param context An execution context which provides the I/O executor that
219 * the socket will use, by default, to dispatch handlers for any asynchronous
220 * operations performed on the socket.
221 *
222 * @param endpoint An endpoint on the local machine to which the socket will
223 * be bound.
224 *
225 * @throws boost::system::system_error Thrown on failure.
226 */
227 template <typename ExecutionContext>
228 basic_socket(ExecutionContext& context, const endpoint_type& endpoint,
229 typename constraint<
230 is_convertible<ExecutionContext&, execution_context&>::value
231 >::type = 0)
232 : impl_(0, 0, context)
233 {
234 boost::system::error_code ec;
235 const protocol_type protocol = endpoint.protocol();
236 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
237 boost::asio::detail::throw_error(ec, "open");
238 impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
239 boost::asio::detail::throw_error(ec, "bind");
240 }
241
242 /// Construct a basic_socket on an existing native socket.
243 /**
244 * This constructor creates a socket object to hold an existing native socket.
245 *
246 * @param ex The I/O executor that the socket will use, by default, to
247 * dispatch handlers for any asynchronous operations performed on the socket.
248 *
249 * @param protocol An object specifying protocol parameters to be used.
250 *
251 * @param native_socket A native socket.
252 *
253 * @throws boost::system::system_error Thrown on failure.
254 */
255 basic_socket(const executor_type& ex, const protocol_type& protocol,
256 const native_handle_type& native_socket)
257 : impl_(0, ex)
258 {
259 boost::system::error_code ec;
260 impl_.get_service().assign(impl_.get_implementation(),
261 protocol, native_socket, ec);
262 boost::asio::detail::throw_error(ec, "assign");
263 }
264
265 /// Construct a basic_socket on an existing native socket.
266 /**
267 * This constructor creates a socket object to hold an existing native socket.
268 *
269 * @param context An execution context which provides the I/O executor that
270 * the socket will use, by default, to dispatch handlers for any asynchronous
271 * operations performed on the socket.
272 *
273 * @param protocol An object specifying protocol parameters to be used.
274 *
275 * @param native_socket A native socket.
276 *
277 * @throws boost::system::system_error Thrown on failure.
278 */
279 template <typename ExecutionContext>
280 basic_socket(ExecutionContext& context, const protocol_type& protocol,
281 const native_handle_type& native_socket,
282 typename constraint<
283 is_convertible<ExecutionContext&, execution_context&>::value
284 >::type = 0)
285 : impl_(0, 0, context)
286 {
287 boost::system::error_code ec;
288 impl_.get_service().assign(impl_.get_implementation(),
289 protocol, native_socket, ec);
290 boost::asio::detail::throw_error(ec, "assign");
291 }
292
293 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
294 /// Move-construct a basic_socket from another.
295 /**
296 * This constructor moves a socket from one object to another.
297 *
298 * @param other The other basic_socket object from which the move will
299 * occur.
300 *
301 * @note Following the move, the moved-from object is in the same state as if
302 * constructed using the @c basic_socket(const executor_type&) constructor.
303 */
304 basic_socket(basic_socket&& other) BOOST_ASIO_NOEXCEPT
305 : impl_(std::move(other.impl_))
306 {
307 }
308
309 /// Move-assign a basic_socket from another.
310 /**
311 * This assignment operator moves a socket from one object to another.
312 *
313 * @param other The other basic_socket object from which the move will
314 * occur.
315 *
316 * @note Following the move, the moved-from object is in the same state as if
317 * constructed using the @c basic_socket(const executor_type&) constructor.
318 */
319 basic_socket& operator=(basic_socket&& other)
320 {
321 impl_ = std::move(other.impl_);
322 return *this;
323 }
324
325 // All sockets have access to each other's implementations.
326 template <typename Protocol1, typename Executor1>
327 friend class basic_socket;
328
329 /// Move-construct a basic_socket from a socket of another protocol type.
330 /**
331 * This constructor moves a socket from one object to another.
332 *
333 * @param other The other basic_socket object from which the move will
334 * occur.
335 *
336 * @note Following the move, the moved-from object is in the same state as if
337 * constructed using the @c basic_socket(const executor_type&) constructor.
338 */
339 template <typename Protocol1, typename Executor1>
340 basic_socket(basic_socket<Protocol1, Executor1>&& other,
341 typename constraint<
342 is_convertible<Protocol1, Protocol>::value
343 && is_convertible<Executor1, Executor>::value
344 >::type = 0)
345 : impl_(std::move(other.impl_))
346 {
347 }
348
349 /// Move-assign a basic_socket from a socket of another protocol type.
350 /**
351 * This assignment operator moves a socket from one object to another.
352 *
353 * @param other The other basic_socket object from which the move will
354 * occur.
355 *
356 * @note Following the move, the moved-from object is in the same state as if
357 * constructed using the @c basic_socket(const executor_type&) constructor.
358 */
359 template <typename Protocol1, typename Executor1>
360 typename constraint<
361 is_convertible<Protocol1, Protocol>::value
362 && is_convertible<Executor1, Executor>::value,
363 basic_socket&
364 >::type operator=(basic_socket<Protocol1, Executor1> && other)
365 {
366 basic_socket tmp(std::move(other));
367 impl_ = std::move(tmp.impl_);
368 return *this;
369 }
370 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
371
372 /// Get the executor associated with the object.
373 executor_type get_executor() BOOST_ASIO_NOEXCEPT
374 {
375 return impl_.get_executor();
376 }
377
378 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
379 /// Get a reference to the lowest layer.
380 /**
381 * This function returns a reference to the lowest layer in a stack of
382 * layers. Since a basic_socket cannot contain any further layers, it simply
383 * returns a reference to itself.
384 *
385 * @return A reference to the lowest layer in the stack of layers. Ownership
386 * is not transferred to the caller.
387 */
388 lowest_layer_type& lowest_layer()
389 {
390 return *this;
391 }
392
393 /// Get a const reference to the lowest layer.
394 /**
395 * This function returns a const reference to the lowest layer in a stack of
396 * layers. Since a basic_socket cannot contain any further layers, it simply
397 * returns a reference to itself.
398 *
399 * @return A const reference to the lowest layer in the stack of layers.
400 * Ownership is not transferred to the caller.
401 */
402 const lowest_layer_type& lowest_layer() const
403 {
404 return *this;
405 }
406 #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
407
408 /// Open the socket using the specified protocol.
409 /**
410 * This function opens the socket so that it will use the specified protocol.
411 *
412 * @param protocol An object specifying protocol parameters to be used.
413 *
414 * @throws boost::system::system_error Thrown on failure.
415 *
416 * @par Example
417 * @code
418 * boost::asio::ip::tcp::socket socket(my_context);
419 * socket.open(boost::asio::ip::tcp::v4());
420 * @endcode
421 */
422 void open(const protocol_type& protocol = protocol_type())
423 {
424 boost::system::error_code ec;
425 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
426 boost::asio::detail::throw_error(ec, "open");
427 }
428
429 /// Open the socket using the specified protocol.
430 /**
431 * This function opens the socket so that it will use the specified protocol.
432 *
433 * @param protocol An object specifying which protocol is to be used.
434 *
435 * @param ec Set to indicate what error occurred, if any.
436 *
437 * @par Example
438 * @code
439 * boost::asio::ip::tcp::socket socket(my_context);
440 * boost::system::error_code ec;
441 * socket.open(boost::asio::ip::tcp::v4(), ec);
442 * if (ec)
443 * {
444 * // An error occurred.
445 * }
446 * @endcode
447 */
448 BOOST_ASIO_SYNC_OP_VOID open(const protocol_type& protocol,
449 boost::system::error_code& ec)
450 {
451 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
452 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
453 }
454
455 /// Assign an existing native socket to the socket.
456 /*
457 * This function opens the socket to hold an existing native socket.
458 *
459 * @param protocol An object specifying which protocol is to be used.
460 *
461 * @param native_socket A native socket.
462 *
463 * @throws boost::system::system_error Thrown on failure.
464 */
465 void assign(const protocol_type& protocol,
466 const native_handle_type& native_socket)
467 {
468 boost::system::error_code ec;
469 impl_.get_service().assign(impl_.get_implementation(),
470 protocol, native_socket, ec);
471 boost::asio::detail::throw_error(ec, "assign");
472 }
473
474 /// Assign an existing native socket to the socket.
475 /*
476 * This function opens the socket to hold an existing native socket.
477 *
478 * @param protocol An object specifying which protocol is to be used.
479 *
480 * @param native_socket A native socket.
481 *
482 * @param ec Set to indicate what error occurred, if any.
483 */
484 BOOST_ASIO_SYNC_OP_VOID assign(const protocol_type& protocol,
485 const native_handle_type& native_socket, boost::system::error_code& ec)
486 {
487 impl_.get_service().assign(impl_.get_implementation(),
488 protocol, native_socket, ec);
489 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
490 }
491
492 /// Determine whether the socket is open.
493 bool is_open() const
494 {
495 return impl_.get_service().is_open(impl_.get_implementation());
496 }
497
498 /// Close the socket.
499 /**
500 * This function is used to close the socket. Any asynchronous send, receive
501 * or connect operations will be cancelled immediately, and will complete
502 * with the boost::asio::error::operation_aborted error.
503 *
504 * @throws boost::system::system_error Thrown on failure. Note that, even if
505 * the function indicates an error, the underlying descriptor is closed.
506 *
507 * @note For portable behaviour with respect to graceful closure of a
508 * connected socket, call shutdown() before closing the socket.
509 */
510 void close()
511 {
512 boost::system::error_code ec;
513 impl_.get_service().close(impl_.get_implementation(), ec);
514 boost::asio::detail::throw_error(ec, "close");
515 }
516
517 /// Close the socket.
518 /**
519 * This function is used to close the socket. Any asynchronous send, receive
520 * or connect operations will be cancelled immediately, and will complete
521 * with the boost::asio::error::operation_aborted error.
522 *
523 * @param ec Set to indicate what error occurred, if any. Note that, even if
524 * the function indicates an error, the underlying descriptor is closed.
525 *
526 * @par Example
527 * @code
528 * boost::asio::ip::tcp::socket socket(my_context);
529 * ...
530 * boost::system::error_code ec;
531 * socket.close(ec);
532 * if (ec)
533 * {
534 * // An error occurred.
535 * }
536 * @endcode
537 *
538 * @note For portable behaviour with respect to graceful closure of a
539 * connected socket, call shutdown() before closing the socket.
540 */
541 BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
542 {
543 impl_.get_service().close(impl_.get_implementation(), ec);
544 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
545 }
546
547 /// Release ownership of the underlying native socket.
548 /**
549 * This function causes all outstanding asynchronous connect, send and receive
550 * operations to finish immediately, and the handlers for cancelled operations
551 * will be passed the boost::asio::error::operation_aborted error. Ownership
552 * of the native socket is then transferred to the caller.
553 *
554 * @throws boost::system::system_error Thrown on failure.
555 *
556 * @note This function is unsupported on Windows versions prior to Windows
557 * 8.1, and will fail with boost::asio::error::operation_not_supported on
558 * these platforms.
559 */
560 #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
561 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603)
562 __declspec(deprecated("This function always fails with "
563 "operation_not_supported when used on Windows versions "
564 "prior to Windows 8.1."))
565 #endif
566 native_handle_type release()
567 {
568 boost::system::error_code ec;
569 native_handle_type s = impl_.get_service().release(
570 impl_.get_implementation(), ec);
571 boost::asio::detail::throw_error(ec, "release");
572 return s;
573 }
574
575 /// Release ownership of the underlying native socket.
576 /**
577 * This function causes all outstanding asynchronous connect, send and receive
578 * operations to finish immediately, and the handlers for cancelled operations
579 * will be passed the boost::asio::error::operation_aborted error. Ownership
580 * of the native socket is then transferred to the caller.
581 *
582 * @param ec Set to indicate what error occurred, if any.
583 *
584 * @note This function is unsupported on Windows versions prior to Windows
585 * 8.1, and will fail with boost::asio::error::operation_not_supported on
586 * these platforms.
587 */
588 #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
589 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603)
590 __declspec(deprecated("This function always fails with "
591 "operation_not_supported when used on Windows versions "
592 "prior to Windows 8.1."))
593 #endif
594 native_handle_type release(boost::system::error_code& ec)
595 {
596 return impl_.get_service().release(impl_.get_implementation(), ec);
597 }
598
599 /// Get the native socket representation.
600 /**
601 * This function may be used to obtain the underlying representation of the
602 * socket. This is intended to allow access to native socket functionality
603 * that is not otherwise provided.
604 */
605 native_handle_type native_handle()
606 {
607 return impl_.get_service().native_handle(impl_.get_implementation());
608 }
609
610 /// Cancel all asynchronous operations associated with the socket.
611 /**
612 * This function causes all outstanding asynchronous connect, send and receive
613 * operations to finish immediately, and the handlers for cancelled operations
614 * will be passed the boost::asio::error::operation_aborted error.
615 *
616 * @throws boost::system::system_error Thrown on failure.
617 *
618 * @note Calls to cancel() will always fail with
619 * boost::asio::error::operation_not_supported when run on Windows XP, Windows
620 * Server 2003, and earlier versions of Windows, unless
621 * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
622 * two issues that should be considered before enabling its use:
623 *
624 * @li It will only cancel asynchronous operations that were initiated in the
625 * current thread.
626 *
627 * @li It can appear to complete without error, but the request to cancel the
628 * unfinished operations may be silently ignored by the operating system.
629 * Whether it works or not seems to depend on the drivers that are installed.
630 *
631 * For portable cancellation, consider using one of the following
632 * alternatives:
633 *
634 * @li Disable asio's I/O completion port backend by defining
635 * BOOST_ASIO_DISABLE_IOCP.
636 *
637 * @li Use the close() function to simultaneously cancel the outstanding
638 * operations and close the socket.
639 *
640 * When running on Windows Vista, Windows Server 2008, and later, the
641 * CancelIoEx function is always used. This function does not have the
642 * problems described above.
643 */
644 #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
645 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
646 && !defined(BOOST_ASIO_ENABLE_CANCELIO)
647 __declspec(deprecated("By default, this function always fails with "
648 "operation_not_supported when used on Windows XP, Windows Server 2003, "
649 "or earlier. Consult documentation for details."))
650 #endif
651 void cancel()
652 {
653 boost::system::error_code ec;
654 impl_.get_service().cancel(impl_.get_implementation(), ec);
655 boost::asio::detail::throw_error(ec, "cancel");
656 }
657
658 /// Cancel all asynchronous operations associated with the socket.
659 /**
660 * This function causes all outstanding asynchronous connect, send and receive
661 * operations to finish immediately, and the handlers for cancelled operations
662 * will be passed the boost::asio::error::operation_aborted error.
663 *
664 * @param ec Set to indicate what error occurred, if any.
665 *
666 * @note Calls to cancel() will always fail with
667 * boost::asio::error::operation_not_supported when run on Windows XP, Windows
668 * Server 2003, and earlier versions of Windows, unless
669 * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
670 * two issues that should be considered before enabling its use:
671 *
672 * @li It will only cancel asynchronous operations that were initiated in the
673 * current thread.
674 *
675 * @li It can appear to complete without error, but the request to cancel the
676 * unfinished operations may be silently ignored by the operating system.
677 * Whether it works or not seems to depend on the drivers that are installed.
678 *
679 * For portable cancellation, consider using one of the following
680 * alternatives:
681 *
682 * @li Disable asio's I/O completion port backend by defining
683 * BOOST_ASIO_DISABLE_IOCP.
684 *
685 * @li Use the close() function to simultaneously cancel the outstanding
686 * operations and close the socket.
687 *
688 * When running on Windows Vista, Windows Server 2008, and later, the
689 * CancelIoEx function is always used. This function does not have the
690 * problems described above.
691 */
692 #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
693 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
694 && !defined(BOOST_ASIO_ENABLE_CANCELIO)
695 __declspec(deprecated("By default, this function always fails with "
696 "operation_not_supported when used on Windows XP, Windows Server 2003, "
697 "or earlier. Consult documentation for details."))
698 #endif
699 BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
700 {
701 impl_.get_service().cancel(impl_.get_implementation(), ec);
702 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
703 }
704
705 /// Determine whether the socket is at the out-of-band data mark.
706 /**
707 * This function is used to check whether the socket input is currently
708 * positioned at the out-of-band data mark.
709 *
710 * @return A bool indicating whether the socket is at the out-of-band data
711 * mark.
712 *
713 * @throws boost::system::system_error Thrown on failure.
714 */
715 bool at_mark() const
716 {
717 boost::system::error_code ec;
718 bool b = impl_.get_service().at_mark(impl_.get_implementation(), ec);
719 boost::asio::detail::throw_error(ec, "at_mark");
720 return b;
721 }
722
723 /// Determine whether the socket is at the out-of-band data mark.
724 /**
725 * This function is used to check whether the socket input is currently
726 * positioned at the out-of-band data mark.
727 *
728 * @param ec Set to indicate what error occurred, if any.
729 *
730 * @return A bool indicating whether the socket is at the out-of-band data
731 * mark.
732 */
733 bool at_mark(boost::system::error_code& ec) const
734 {
735 return impl_.get_service().at_mark(impl_.get_implementation(), ec);
736 }
737
738 /// Determine the number of bytes available for reading.
739 /**
740 * This function is used to determine the number of bytes that may be read
741 * without blocking.
742 *
743 * @return The number of bytes that may be read without blocking, or 0 if an
744 * error occurs.
745 *
746 * @throws boost::system::system_error Thrown on failure.
747 */
748 std::size_t available() const
749 {
750 boost::system::error_code ec;
751 std::size_t s = impl_.get_service().available(
752 impl_.get_implementation(), ec);
753 boost::asio::detail::throw_error(ec, "available");
754 return s;
755 }
756
757 /// Determine the number of bytes available for reading.
758 /**
759 * This function is used to determine the number of bytes that may be read
760 * without blocking.
761 *
762 * @param ec Set to indicate what error occurred, if any.
763 *
764 * @return The number of bytes that may be read without blocking, or 0 if an
765 * error occurs.
766 */
767 std::size_t available(boost::system::error_code& ec) const
768 {
769 return impl_.get_service().available(impl_.get_implementation(), ec);
770 }
771
772 /// Bind the socket to the given local endpoint.
773 /**
774 * This function binds the socket to the specified endpoint on the local
775 * machine.
776 *
777 * @param endpoint An endpoint on the local machine to which the socket will
778 * be bound.
779 *
780 * @throws boost::system::system_error Thrown on failure.
781 *
782 * @par Example
783 * @code
784 * boost::asio::ip::tcp::socket socket(my_context);
785 * socket.open(boost::asio::ip::tcp::v4());
786 * socket.bind(boost::asio::ip::tcp::endpoint(
787 * boost::asio::ip::tcp::v4(), 12345));
788 * @endcode
789 */
790 void bind(const endpoint_type& endpoint)
791 {
792 boost::system::error_code ec;
793 impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
794 boost::asio::detail::throw_error(ec, "bind");
795 }
796
797 /// Bind the socket to the given local endpoint.
798 /**
799 * This function binds the socket to the specified endpoint on the local
800 * machine.
801 *
802 * @param endpoint An endpoint on the local machine to which the socket will
803 * be bound.
804 *
805 * @param ec Set to indicate what error occurred, if any.
806 *
807 * @par Example
808 * @code
809 * boost::asio::ip::tcp::socket socket(my_context);
810 * socket.open(boost::asio::ip::tcp::v4());
811 * boost::system::error_code ec;
812 * socket.bind(boost::asio::ip::tcp::endpoint(
813 * boost::asio::ip::tcp::v4(), 12345), ec);
814 * if (ec)
815 * {
816 * // An error occurred.
817 * }
818 * @endcode
819 */
820 BOOST_ASIO_SYNC_OP_VOID bind(const endpoint_type& endpoint,
821 boost::system::error_code& ec)
822 {
823 impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
824 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
825 }
826
827 /// Connect the socket to the specified endpoint.
828 /**
829 * This function is used to connect a socket to the specified remote endpoint.
830 * The function call will block until the connection is successfully made or
831 * an error occurs.
832 *
833 * The socket is automatically opened if it is not already open. If the
834 * connect fails, and the socket was automatically opened, the socket is
835 * not returned to the closed state.
836 *
837 * @param peer_endpoint The remote endpoint to which the socket will be
838 * connected.
839 *
840 * @throws boost::system::system_error Thrown on failure.
841 *
842 * @par Example
843 * @code
844 * boost::asio::ip::tcp::socket socket(my_context);
845 * boost::asio::ip::tcp::endpoint endpoint(
846 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
847 * socket.connect(endpoint);
848 * @endcode
849 */
850 void connect(const endpoint_type& peer_endpoint)
851 {
852 boost::system::error_code ec;
853 if (!is_open())
854 {
855 impl_.get_service().open(impl_.get_implementation(),
856 peer_endpoint.protocol(), ec);
857 boost::asio::detail::throw_error(ec, "connect");
858 }
859 impl_.get_service().connect(impl_.get_implementation(), peer_endpoint, ec);
860 boost::asio::detail::throw_error(ec, "connect");
861 }
862
863 /// Connect the socket to the specified endpoint.
864 /**
865 * This function is used to connect a socket to the specified remote endpoint.
866 * The function call will block until the connection is successfully made or
867 * an error occurs.
868 *
869 * The socket is automatically opened if it is not already open. If the
870 * connect fails, and the socket was automatically opened, the socket is
871 * not returned to the closed state.
872 *
873 * @param peer_endpoint The remote endpoint to which the socket will be
874 * connected.
875 *
876 * @param ec Set to indicate what error occurred, if any.
877 *
878 * @par Example
879 * @code
880 * boost::asio::ip::tcp::socket socket(my_context);
881 * boost::asio::ip::tcp::endpoint endpoint(
882 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
883 * boost::system::error_code ec;
884 * socket.connect(endpoint, ec);
885 * if (ec)
886 * {
887 * // An error occurred.
888 * }
889 * @endcode
890 */
891 BOOST_ASIO_SYNC_OP_VOID connect(const endpoint_type& peer_endpoint,
892 boost::system::error_code& ec)
893 {
894 if (!is_open())
895 {
896 impl_.get_service().open(impl_.get_implementation(),
897 peer_endpoint.protocol(), ec);
898 if (ec)
899 {
900 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
901 }
902 }
903
904 impl_.get_service().connect(impl_.get_implementation(), peer_endpoint, ec);
905 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
906 }
907
908 /// Start an asynchronous connect.
909 /**
910 * This function is used to asynchronously connect a socket to the specified
911 * remote endpoint. It is an initiating function for an @ref
912 * asynchronous_operation, and always returns immediately.
913 *
914 * The socket is automatically opened if it is not already open. If the
915 * connect fails, and the socket was automatically opened, the socket is
916 * not returned to the closed state.
917 *
918 * @param peer_endpoint The remote endpoint to which the socket will be
919 * connected. Copies will be made of the endpoint object as required.
920 *
921 * @param token The @ref completion_token that will be used to produce a
922 * completion handler, which will be called when the connect completes.
923 * Potential completion tokens include @ref use_future, @ref use_awaitable,
924 * @ref yield_context, or a function object with the correct completion
925 * signature. The function signature of the completion handler must be:
926 * @code void handler(
927 * const boost::system::error_code& error // Result of operation.
928 * ); @endcode
929 * Regardless of whether the asynchronous operation completes immediately or
930 * not, the completion handler will not be invoked from within this function.
931 * On immediate completion, invocation of the handler will be performed in a
932 * manner equivalent to using boost::asio::post().
933 *
934 * @par Completion Signature
935 * @code void(boost::system::error_code) @endcode
936 *
937 * @par Example
938 * @code
939 * void connect_handler(const boost::system::error_code& error)
940 * {
941 * if (!error)
942 * {
943 * // Connect succeeded.
944 * }
945 * }
946 *
947 * ...
948 *
949 * boost::asio::ip::tcp::socket socket(my_context);
950 * boost::asio::ip::tcp::endpoint endpoint(
951 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
952 * socket.async_connect(endpoint, connect_handler);
953 * @endcode
954 *
955 * @par Per-Operation Cancellation
956 * On POSIX or Windows operating systems, this asynchronous operation supports
957 * cancellation for the following boost::asio::cancellation_type values:
958 *
959 * @li @c cancellation_type::terminal
960 *
961 * @li @c cancellation_type::partial
962 *
963 * @li @c cancellation_type::total
964 */
965 template <
966 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
967 ConnectToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
968 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ConnectToken,
969 void (boost::system::error_code))
970 async_connect(const endpoint_type& peer_endpoint,
971 BOOST_ASIO_MOVE_ARG(ConnectToken) token
972 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
973 {
974 boost::system::error_code open_ec;
975 if (!is_open())
976 {
977 const protocol_type protocol = peer_endpoint.protocol();
978 impl_.get_service().open(impl_.get_implementation(), protocol, open_ec);
979 }
980
981 return async_initiate<ConnectToken, void (boost::system::error_code)>(
982 initiate_async_connect(this), token, peer_endpoint, open_ec);
983 }
984
985 /// Set an option on the socket.
986 /**
987 * This function is used to set an option on the socket.
988 *
989 * @param option The new option value to be set on the socket.
990 *
991 * @throws boost::system::system_error Thrown on failure.
992 *
993 * @sa SettableSocketOption @n
994 * boost::asio::socket_base::broadcast @n
995 * boost::asio::socket_base::do_not_route @n
996 * boost::asio::socket_base::keep_alive @n
997 * boost::asio::socket_base::linger @n
998 * boost::asio::socket_base::receive_buffer_size @n
999 * boost::asio::socket_base::receive_low_watermark @n
1000 * boost::asio::socket_base::reuse_address @n
1001 * boost::asio::socket_base::send_buffer_size @n
1002 * boost::asio::socket_base::send_low_watermark @n
1003 * boost::asio::ip::multicast::join_group @n
1004 * boost::asio::ip::multicast::leave_group @n
1005 * boost::asio::ip::multicast::enable_loopback @n
1006 * boost::asio::ip::multicast::outbound_interface @n
1007 * boost::asio::ip::multicast::hops @n
1008 * boost::asio::ip::tcp::no_delay
1009 *
1010 * @par Example
1011 * Setting the IPPROTO_TCP/TCP_NODELAY option:
1012 * @code
1013 * boost::asio::ip::tcp::socket socket(my_context);
1014 * ...
1015 * boost::asio::ip::tcp::no_delay option(true);
1016 * socket.set_option(option);
1017 * @endcode
1018 */
1019 template <typename SettableSocketOption>
1020 void set_option(const SettableSocketOption& option)
1021 {
1022 boost::system::error_code ec;
1023 impl_.get_service().set_option(impl_.get_implementation(), option, ec);
1024 boost::asio::detail::throw_error(ec, "set_option");
1025 }
1026
1027 /// Set an option on the socket.
1028 /**
1029 * This function is used to set an option on the socket.
1030 *
1031 * @param option The new option value to be set on the socket.
1032 *
1033 * @param ec Set to indicate what error occurred, if any.
1034 *
1035 * @sa SettableSocketOption @n
1036 * boost::asio::socket_base::broadcast @n
1037 * boost::asio::socket_base::do_not_route @n
1038 * boost::asio::socket_base::keep_alive @n
1039 * boost::asio::socket_base::linger @n
1040 * boost::asio::socket_base::receive_buffer_size @n
1041 * boost::asio::socket_base::receive_low_watermark @n
1042 * boost::asio::socket_base::reuse_address @n
1043 * boost::asio::socket_base::send_buffer_size @n
1044 * boost::asio::socket_base::send_low_watermark @n
1045 * boost::asio::ip::multicast::join_group @n
1046 * boost::asio::ip::multicast::leave_group @n
1047 * boost::asio::ip::multicast::enable_loopback @n
1048 * boost::asio::ip::multicast::outbound_interface @n
1049 * boost::asio::ip::multicast::hops @n
1050 * boost::asio::ip::tcp::no_delay
1051 *
1052 * @par Example
1053 * Setting the IPPROTO_TCP/TCP_NODELAY option:
1054 * @code
1055 * boost::asio::ip::tcp::socket socket(my_context);
1056 * ...
1057 * boost::asio::ip::tcp::no_delay option(true);
1058 * boost::system::error_code ec;
1059 * socket.set_option(option, ec);
1060 * if (ec)
1061 * {
1062 * // An error occurred.
1063 * }
1064 * @endcode
1065 */
1066 template <typename SettableSocketOption>
1067 BOOST_ASIO_SYNC_OP_VOID set_option(const SettableSocketOption& option,
1068 boost::system::error_code& ec)
1069 {
1070 impl_.get_service().set_option(impl_.get_implementation(), option, ec);
1071 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1072 }
1073
1074 /// Get an option from the socket.
1075 /**
1076 * This function is used to get the current value of an option on the socket.
1077 *
1078 * @param option The option value to be obtained from the socket.
1079 *
1080 * @throws boost::system::system_error Thrown on failure.
1081 *
1082 * @sa GettableSocketOption @n
1083 * boost::asio::socket_base::broadcast @n
1084 * boost::asio::socket_base::do_not_route @n
1085 * boost::asio::socket_base::keep_alive @n
1086 * boost::asio::socket_base::linger @n
1087 * boost::asio::socket_base::receive_buffer_size @n
1088 * boost::asio::socket_base::receive_low_watermark @n
1089 * boost::asio::socket_base::reuse_address @n
1090 * boost::asio::socket_base::send_buffer_size @n
1091 * boost::asio::socket_base::send_low_watermark @n
1092 * boost::asio::ip::multicast::join_group @n
1093 * boost::asio::ip::multicast::leave_group @n
1094 * boost::asio::ip::multicast::enable_loopback @n
1095 * boost::asio::ip::multicast::outbound_interface @n
1096 * boost::asio::ip::multicast::hops @n
1097 * boost::asio::ip::tcp::no_delay
1098 *
1099 * @par Example
1100 * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
1101 * @code
1102 * boost::asio::ip::tcp::socket socket(my_context);
1103 * ...
1104 * boost::asio::ip::tcp::socket::keep_alive option;
1105 * socket.get_option(option);
1106 * bool is_set = option.value();
1107 * @endcode
1108 */
1109 template <typename GettableSocketOption>
1110 void get_option(GettableSocketOption& option) const
1111 {
1112 boost::system::error_code ec;
1113 impl_.get_service().get_option(impl_.get_implementation(), option, ec);
1114 boost::asio::detail::throw_error(ec, "get_option");
1115 }
1116
1117 /// Get an option from the socket.
1118 /**
1119 * This function is used to get the current value of an option on the socket.
1120 *
1121 * @param option The option value to be obtained from the socket.
1122 *
1123 * @param ec Set to indicate what error occurred, if any.
1124 *
1125 * @sa GettableSocketOption @n
1126 * boost::asio::socket_base::broadcast @n
1127 * boost::asio::socket_base::do_not_route @n
1128 * boost::asio::socket_base::keep_alive @n
1129 * boost::asio::socket_base::linger @n
1130 * boost::asio::socket_base::receive_buffer_size @n
1131 * boost::asio::socket_base::receive_low_watermark @n
1132 * boost::asio::socket_base::reuse_address @n
1133 * boost::asio::socket_base::send_buffer_size @n
1134 * boost::asio::socket_base::send_low_watermark @n
1135 * boost::asio::ip::multicast::join_group @n
1136 * boost::asio::ip::multicast::leave_group @n
1137 * boost::asio::ip::multicast::enable_loopback @n
1138 * boost::asio::ip::multicast::outbound_interface @n
1139 * boost::asio::ip::multicast::hops @n
1140 * boost::asio::ip::tcp::no_delay
1141 *
1142 * @par Example
1143 * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
1144 * @code
1145 * boost::asio::ip::tcp::socket socket(my_context);
1146 * ...
1147 * boost::asio::ip::tcp::socket::keep_alive option;
1148 * boost::system::error_code ec;
1149 * socket.get_option(option, ec);
1150 * if (ec)
1151 * {
1152 * // An error occurred.
1153 * }
1154 * bool is_set = option.value();
1155 * @endcode
1156 */
1157 template <typename GettableSocketOption>
1158 BOOST_ASIO_SYNC_OP_VOID get_option(GettableSocketOption& option,
1159 boost::system::error_code& ec) const
1160 {
1161 impl_.get_service().get_option(impl_.get_implementation(), option, ec);
1162 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1163 }
1164
1165 /// Perform an IO control command on the socket.
1166 /**
1167 * This function is used to execute an IO control command on the socket.
1168 *
1169 * @param command The IO control command to be performed on the socket.
1170 *
1171 * @throws boost::system::system_error Thrown on failure.
1172 *
1173 * @sa IoControlCommand @n
1174 * boost::asio::socket_base::bytes_readable @n
1175 * boost::asio::socket_base::non_blocking_io
1176 *
1177 * @par Example
1178 * Getting the number of bytes ready to read:
1179 * @code
1180 * boost::asio::ip::tcp::socket socket(my_context);
1181 * ...
1182 * boost::asio::ip::tcp::socket::bytes_readable command;
1183 * socket.io_control(command);
1184 * std::size_t bytes_readable = command.get();
1185 * @endcode
1186 */
1187 template <typename IoControlCommand>
1188 void io_control(IoControlCommand& command)
1189 {
1190 boost::system::error_code ec;
1191 impl_.get_service().io_control(impl_.get_implementation(), command, ec);
1192 boost::asio::detail::throw_error(ec, "io_control");
1193 }
1194
1195 /// Perform an IO control command on the socket.
1196 /**
1197 * This function is used to execute an IO control command on the socket.
1198 *
1199 * @param command The IO control command to be performed on the socket.
1200 *
1201 * @param ec Set to indicate what error occurred, if any.
1202 *
1203 * @sa IoControlCommand @n
1204 * boost::asio::socket_base::bytes_readable @n
1205 * boost::asio::socket_base::non_blocking_io
1206 *
1207 * @par Example
1208 * Getting the number of bytes ready to read:
1209 * @code
1210 * boost::asio::ip::tcp::socket socket(my_context);
1211 * ...
1212 * boost::asio::ip::tcp::socket::bytes_readable command;
1213 * boost::system::error_code ec;
1214 * socket.io_control(command, ec);
1215 * if (ec)
1216 * {
1217 * // An error occurred.
1218 * }
1219 * std::size_t bytes_readable = command.get();
1220 * @endcode
1221 */
1222 template <typename IoControlCommand>
1223 BOOST_ASIO_SYNC_OP_VOID io_control(IoControlCommand& command,
1224 boost::system::error_code& ec)
1225 {
1226 impl_.get_service().io_control(impl_.get_implementation(), command, ec);
1227 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1228 }
1229
1230 /// Gets the non-blocking mode of the socket.
1231 /**
1232 * @returns @c true if the socket's synchronous operations will fail with
1233 * boost::asio::error::would_block if they are unable to perform the requested
1234 * operation immediately. If @c false, synchronous operations will block
1235 * until complete.
1236 *
1237 * @note The non-blocking mode has no effect on the behaviour of asynchronous
1238 * operations. Asynchronous operations will never fail with the error
1239 * boost::asio::error::would_block.
1240 */
1241 bool non_blocking() const
1242 {
1243 return impl_.get_service().non_blocking(impl_.get_implementation());
1244 }
1245
1246 /// Sets the non-blocking mode of the socket.
1247 /**
1248 * @param mode If @c true, the socket's synchronous operations will fail with
1249 * boost::asio::error::would_block if they are unable to perform the requested
1250 * operation immediately. If @c false, synchronous operations will block
1251 * until complete.
1252 *
1253 * @throws boost::system::system_error Thrown on failure.
1254 *
1255 * @note The non-blocking mode has no effect on the behaviour of asynchronous
1256 * operations. Asynchronous operations will never fail with the error
1257 * boost::asio::error::would_block.
1258 */
1259 void non_blocking(bool mode)
1260 {
1261 boost::system::error_code ec;
1262 impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
1263 boost::asio::detail::throw_error(ec, "non_blocking");
1264 }
1265
1266 /// Sets the non-blocking mode of the socket.
1267 /**
1268 * @param mode If @c true, the socket's synchronous operations will fail with
1269 * boost::asio::error::would_block if they are unable to perform the requested
1270 * operation immediately. If @c false, synchronous operations will block
1271 * until complete.
1272 *
1273 * @param ec Set to indicate what error occurred, if any.
1274 *
1275 * @note The non-blocking mode has no effect on the behaviour of asynchronous
1276 * operations. Asynchronous operations will never fail with the error
1277 * boost::asio::error::would_block.
1278 */
1279 BOOST_ASIO_SYNC_OP_VOID non_blocking(
1280 bool mode, boost::system::error_code& ec)
1281 {
1282 impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
1283 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1284 }
1285
1286 /// Gets the non-blocking mode of the native socket implementation.
1287 /**
1288 * This function is used to retrieve the non-blocking mode of the underlying
1289 * native socket. This mode has no effect on the behaviour of the socket
1290 * object's synchronous operations.
1291 *
1292 * @returns @c true if the underlying socket is in non-blocking mode and
1293 * direct system calls may fail with boost::asio::error::would_block (or the
1294 * equivalent system error).
1295 *
1296 * @note The current non-blocking mode is cached by the socket object.
1297 * Consequently, the return value may be incorrect if the non-blocking mode
1298 * was set directly on the native socket.
1299 *
1300 * @par Example
1301 * This function is intended to allow the encapsulation of arbitrary
1302 * non-blocking system calls as asynchronous operations, in a way that is
1303 * transparent to the user of the socket object. The following example
1304 * illustrates how Linux's @c sendfile system call might be encapsulated:
1305 * @code template <typename Handler>
1306 * struct sendfile_op
1307 * {
1308 * tcp::socket& sock_;
1309 * int fd_;
1310 * Handler handler_;
1311 * off_t offset_;
1312 * std::size_t total_bytes_transferred_;
1313 *
1314 * // Function call operator meeting WriteHandler requirements.
1315 * // Used as the handler for the async_write_some operation.
1316 * void operator()(boost::system::error_code ec, std::size_t)
1317 * {
1318 * // Put the underlying socket into non-blocking mode.
1319 * if (!ec)
1320 * if (!sock_.native_non_blocking())
1321 * sock_.native_non_blocking(true, ec);
1322 *
1323 * if (!ec)
1324 * {
1325 * for (;;)
1326 * {
1327 * // Try the system call.
1328 * errno = 0;
1329 * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
1330 * ec = boost::system::error_code(n < 0 ? errno : 0,
1331 * boost::asio::error::get_system_category());
1332 * total_bytes_transferred_ += ec ? 0 : n;
1333 *
1334 * // Retry operation immediately if interrupted by signal.
1335 * if (ec == boost::asio::error::interrupted)
1336 * continue;
1337 *
1338 * // Check if we need to run the operation again.
1339 * if (ec == boost::asio::error::would_block
1340 * || ec == boost::asio::error::try_again)
1341 * {
1342 * // We have to wait for the socket to become ready again.
1343 * sock_.async_wait(tcp::socket::wait_write, *this);
1344 * return;
1345 * }
1346 *
1347 * if (ec || n == 0)
1348 * {
1349 * // An error occurred, or we have reached the end of the file.
1350 * // Either way we must exit the loop so we can call the handler.
1351 * break;
1352 * }
1353 *
1354 * // Loop around to try calling sendfile again.
1355 * }
1356 * }
1357 *
1358 * // Pass result back to user's handler.
1359 * handler_(ec, total_bytes_transferred_);
1360 * }
1361 * };
1362 *
1363 * template <typename Handler>
1364 * void async_sendfile(tcp::socket& sock, int fd, Handler h)
1365 * {
1366 * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
1367 * sock.async_wait(tcp::socket::wait_write, op);
1368 * } @endcode
1369 */
1370 bool native_non_blocking() const
1371 {
1372 return impl_.get_service().native_non_blocking(impl_.get_implementation());
1373 }
1374
1375 /// Sets the non-blocking mode of the native socket implementation.
1376 /**
1377 * This function is used to modify the non-blocking mode of the underlying
1378 * native socket. It has no effect on the behaviour of the socket object's
1379 * synchronous operations.
1380 *
1381 * @param mode If @c true, the underlying socket is put into non-blocking
1382 * mode and direct system calls may fail with boost::asio::error::would_block
1383 * (or the equivalent system error).
1384 *
1385 * @throws boost::system::system_error Thrown on failure. If the @c mode is
1386 * @c false, but the current value of @c non_blocking() is @c true, this
1387 * function fails with boost::asio::error::invalid_argument, as the
1388 * combination does not make sense.
1389 *
1390 * @par Example
1391 * This function is intended to allow the encapsulation of arbitrary
1392 * non-blocking system calls as asynchronous operations, in a way that is
1393 * transparent to the user of the socket object. The following example
1394 * illustrates how Linux's @c sendfile system call might be encapsulated:
1395 * @code template <typename Handler>
1396 * struct sendfile_op
1397 * {
1398 * tcp::socket& sock_;
1399 * int fd_;
1400 * Handler handler_;
1401 * off_t offset_;
1402 * std::size_t total_bytes_transferred_;
1403 *
1404 * // Function call operator meeting WriteHandler requirements.
1405 * // Used as the handler for the async_write_some operation.
1406 * void operator()(boost::system::error_code ec, std::size_t)
1407 * {
1408 * // Put the underlying socket into non-blocking mode.
1409 * if (!ec)
1410 * if (!sock_.native_non_blocking())
1411 * sock_.native_non_blocking(true, ec);
1412 *
1413 * if (!ec)
1414 * {
1415 * for (;;)
1416 * {
1417 * // Try the system call.
1418 * errno = 0;
1419 * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
1420 * ec = boost::system::error_code(n < 0 ? errno : 0,
1421 * boost::asio::error::get_system_category());
1422 * total_bytes_transferred_ += ec ? 0 : n;
1423 *
1424 * // Retry operation immediately if interrupted by signal.
1425 * if (ec == boost::asio::error::interrupted)
1426 * continue;
1427 *
1428 * // Check if we need to run the operation again.
1429 * if (ec == boost::asio::error::would_block
1430 * || ec == boost::asio::error::try_again)
1431 * {
1432 * // We have to wait for the socket to become ready again.
1433 * sock_.async_wait(tcp::socket::wait_write, *this);
1434 * return;
1435 * }
1436 *
1437 * if (ec || n == 0)
1438 * {
1439 * // An error occurred, or we have reached the end of the file.
1440 * // Either way we must exit the loop so we can call the handler.
1441 * break;
1442 * }
1443 *
1444 * // Loop around to try calling sendfile again.
1445 * }
1446 * }
1447 *
1448 * // Pass result back to user's handler.
1449 * handler_(ec, total_bytes_transferred_);
1450 * }
1451 * };
1452 *
1453 * template <typename Handler>
1454 * void async_sendfile(tcp::socket& sock, int fd, Handler h)
1455 * {
1456 * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
1457 * sock.async_wait(tcp::socket::wait_write, op);
1458 * } @endcode
1459 */
1460 void native_non_blocking(bool mode)
1461 {
1462 boost::system::error_code ec;
1463 impl_.get_service().native_non_blocking(
1464 impl_.get_implementation(), mode, ec);
1465 boost::asio::detail::throw_error(ec, "native_non_blocking");
1466 }
1467
1468 /// Sets the non-blocking mode of the native socket implementation.
1469 /**
1470 * This function is used to modify the non-blocking mode of the underlying
1471 * native socket. It has no effect on the behaviour of the socket object's
1472 * synchronous operations.
1473 *
1474 * @param mode If @c true, the underlying socket is put into non-blocking
1475 * mode and direct system calls may fail with boost::asio::error::would_block
1476 * (or the equivalent system error).
1477 *
1478 * @param ec Set to indicate what error occurred, if any. If the @c mode is
1479 * @c false, but the current value of @c non_blocking() is @c true, this
1480 * function fails with boost::asio::error::invalid_argument, as the
1481 * combination does not make sense.
1482 *
1483 * @par Example
1484 * This function is intended to allow the encapsulation of arbitrary
1485 * non-blocking system calls as asynchronous operations, in a way that is
1486 * transparent to the user of the socket object. The following example
1487 * illustrates how Linux's @c sendfile system call might be encapsulated:
1488 * @code template <typename Handler>
1489 * struct sendfile_op
1490 * {
1491 * tcp::socket& sock_;
1492 * int fd_;
1493 * Handler handler_;
1494 * off_t offset_;
1495 * std::size_t total_bytes_transferred_;
1496 *
1497 * // Function call operator meeting WriteHandler requirements.
1498 * // Used as the handler for the async_write_some operation.
1499 * void operator()(boost::system::error_code ec, std::size_t)
1500 * {
1501 * // Put the underlying socket into non-blocking mode.
1502 * if (!ec)
1503 * if (!sock_.native_non_blocking())
1504 * sock_.native_non_blocking(true, ec);
1505 *
1506 * if (!ec)
1507 * {
1508 * for (;;)
1509 * {
1510 * // Try the system call.
1511 * errno = 0;
1512 * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
1513 * ec = boost::system::error_code(n < 0 ? errno : 0,
1514 * boost::asio::error::get_system_category());
1515 * total_bytes_transferred_ += ec ? 0 : n;
1516 *
1517 * // Retry operation immediately if interrupted by signal.
1518 * if (ec == boost::asio::error::interrupted)
1519 * continue;
1520 *
1521 * // Check if we need to run the operation again.
1522 * if (ec == boost::asio::error::would_block
1523 * || ec == boost::asio::error::try_again)
1524 * {
1525 * // We have to wait for the socket to become ready again.
1526 * sock_.async_wait(tcp::socket::wait_write, *this);
1527 * return;
1528 * }
1529 *
1530 * if (ec || n == 0)
1531 * {
1532 * // An error occurred, or we have reached the end of the file.
1533 * // Either way we must exit the loop so we can call the handler.
1534 * break;
1535 * }
1536 *
1537 * // Loop around to try calling sendfile again.
1538 * }
1539 * }
1540 *
1541 * // Pass result back to user's handler.
1542 * handler_(ec, total_bytes_transferred_);
1543 * }
1544 * };
1545 *
1546 * template <typename Handler>
1547 * void async_sendfile(tcp::socket& sock, int fd, Handler h)
1548 * {
1549 * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
1550 * sock.async_wait(tcp::socket::wait_write, op);
1551 * } @endcode
1552 */
1553 BOOST_ASIO_SYNC_OP_VOID native_non_blocking(
1554 bool mode, boost::system::error_code& ec)
1555 {
1556 impl_.get_service().native_non_blocking(
1557 impl_.get_implementation(), mode, ec);
1558 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1559 }
1560
1561 /// Get the local endpoint of the socket.
1562 /**
1563 * This function is used to obtain the locally bound endpoint of the socket.
1564 *
1565 * @returns An object that represents the local endpoint of the socket.
1566 *
1567 * @throws boost::system::system_error Thrown on failure.
1568 *
1569 * @par Example
1570 * @code
1571 * boost::asio::ip::tcp::socket socket(my_context);
1572 * ...
1573 * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint();
1574 * @endcode
1575 */
1576 endpoint_type local_endpoint() const
1577 {
1578 boost::system::error_code ec;
1579 endpoint_type ep = impl_.get_service().local_endpoint(
1580 impl_.get_implementation(), ec);
1581 boost::asio::detail::throw_error(ec, "local_endpoint");
1582 return ep;
1583 }
1584
1585 /// Get the local endpoint of the socket.
1586 /**
1587 * This function is used to obtain the locally bound endpoint of the socket.
1588 *
1589 * @param ec Set to indicate what error occurred, if any.
1590 *
1591 * @returns An object that represents the local endpoint of the socket.
1592 * Returns a default-constructed endpoint object if an error occurred.
1593 *
1594 * @par Example
1595 * @code
1596 * boost::asio::ip::tcp::socket socket(my_context);
1597 * ...
1598 * boost::system::error_code ec;
1599 * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint(ec);
1600 * if (ec)
1601 * {
1602 * // An error occurred.
1603 * }
1604 * @endcode
1605 */
1606 endpoint_type local_endpoint(boost::system::error_code& ec) const
1607 {
1608 return impl_.get_service().local_endpoint(impl_.get_implementation(), ec);
1609 }
1610
1611 /// Get the remote endpoint of the socket.
1612 /**
1613 * This function is used to obtain the remote endpoint of the socket.
1614 *
1615 * @returns An object that represents the remote endpoint of the socket.
1616 *
1617 * @throws boost::system::system_error Thrown on failure.
1618 *
1619 * @par Example
1620 * @code
1621 * boost::asio::ip::tcp::socket socket(my_context);
1622 * ...
1623 * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint();
1624 * @endcode
1625 */
1626 endpoint_type remote_endpoint() const
1627 {
1628 boost::system::error_code ec;
1629 endpoint_type ep = impl_.get_service().remote_endpoint(
1630 impl_.get_implementation(), ec);
1631 boost::asio::detail::throw_error(ec, "remote_endpoint");
1632 return ep;
1633 }
1634
1635 /// Get the remote endpoint of the socket.
1636 /**
1637 * This function is used to obtain the remote endpoint of the socket.
1638 *
1639 * @param ec Set to indicate what error occurred, if any.
1640 *
1641 * @returns An object that represents the remote endpoint of the socket.
1642 * Returns a default-constructed endpoint object if an error occurred.
1643 *
1644 * @par Example
1645 * @code
1646 * boost::asio::ip::tcp::socket socket(my_context);
1647 * ...
1648 * boost::system::error_code ec;
1649 * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(ec);
1650 * if (ec)
1651 * {
1652 * // An error occurred.
1653 * }
1654 * @endcode
1655 */
1656 endpoint_type remote_endpoint(boost::system::error_code& ec) const
1657 {
1658 return impl_.get_service().remote_endpoint(impl_.get_implementation(), ec);
1659 }
1660
1661 /// Disable sends or receives on the socket.
1662 /**
1663 * This function is used to disable send operations, receive operations, or
1664 * both.
1665 *
1666 * @param what Determines what types of operation will no longer be allowed.
1667 *
1668 * @throws boost::system::system_error Thrown on failure.
1669 *
1670 * @par Example
1671 * Shutting down the send side of the socket:
1672 * @code
1673 * boost::asio::ip::tcp::socket socket(my_context);
1674 * ...
1675 * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send);
1676 * @endcode
1677 */
1678 void shutdown(shutdown_type what)
1679 {
1680 boost::system::error_code ec;
1681 impl_.get_service().shutdown(impl_.get_implementation(), what, ec);
1682 boost::asio::detail::throw_error(ec, "shutdown");
1683 }
1684
1685 /// Disable sends or receives on the socket.
1686 /**
1687 * This function is used to disable send operations, receive operations, or
1688 * both.
1689 *
1690 * @param what Determines what types of operation will no longer be allowed.
1691 *
1692 * @param ec Set to indicate what error occurred, if any.
1693 *
1694 * @par Example
1695 * Shutting down the send side of the socket:
1696 * @code
1697 * boost::asio::ip::tcp::socket socket(my_context);
1698 * ...
1699 * boost::system::error_code ec;
1700 * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
1701 * if (ec)
1702 * {
1703 * // An error occurred.
1704 * }
1705 * @endcode
1706 */
1707 BOOST_ASIO_SYNC_OP_VOID shutdown(shutdown_type what,
1708 boost::system::error_code& ec)
1709 {
1710 impl_.get_service().shutdown(impl_.get_implementation(), what, ec);
1711 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1712 }
1713
1714 /// Wait for the socket to become ready to read, ready to write, or to have
1715 /// pending error conditions.
1716 /**
1717 * This function is used to perform a blocking wait for a socket to enter
1718 * a ready to read, write or error condition state.
1719 *
1720 * @param w Specifies the desired socket state.
1721 *
1722 * @par Example
1723 * Waiting for a socket to become readable.
1724 * @code
1725 * boost::asio::ip::tcp::socket socket(my_context);
1726 * ...
1727 * socket.wait(boost::asio::ip::tcp::socket::wait_read);
1728 * @endcode
1729 */
1730 void wait(wait_type w)
1731 {
1732 boost::system::error_code ec;
1733 impl_.get_service().wait(impl_.get_implementation(), w, ec);
1734 boost::asio::detail::throw_error(ec, "wait");
1735 }
1736
1737 /// Wait for the socket to become ready to read, ready to write, or to have
1738 /// pending error conditions.
1739 /**
1740 * This function is used to perform a blocking wait for a socket to enter
1741 * a ready to read, write or error condition state.
1742 *
1743 * @param w Specifies the desired socket state.
1744 *
1745 * @param ec Set to indicate what error occurred, if any.
1746 *
1747 * @par Example
1748 * Waiting for a socket to become readable.
1749 * @code
1750 * boost::asio::ip::tcp::socket socket(my_context);
1751 * ...
1752 * boost::system::error_code ec;
1753 * socket.wait(boost::asio::ip::tcp::socket::wait_read, ec);
1754 * @endcode
1755 */
1756 BOOST_ASIO_SYNC_OP_VOID wait(wait_type w, boost::system::error_code& ec)
1757 {
1758 impl_.get_service().wait(impl_.get_implementation(), w, ec);
1759 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
1760 }
1761
1762 /// Asynchronously wait for the socket to become ready to read, ready to
1763 /// write, or to have pending error conditions.
1764 /**
1765 * This function is used to perform an asynchronous wait for a socket to enter
1766 * a ready to read, write or error condition state. It is an initiating
1767 * function for an @ref asynchronous_operation, and always returns
1768 * immediately.
1769 *
1770 * @param w Specifies the desired socket state.
1771 *
1772 * @param token The @ref completion_token that will be used to produce a
1773 * completion handler, which will be called when the wait completes. Potential
1774 * completion tokens include @ref use_future, @ref use_awaitable, @ref
1775 * yield_context, or a function object with the correct completion signature.
1776 * The function signature of the completion handler must be:
1777 * @code void handler(
1778 * const boost::system::error_code& error // Result of operation.
1779 * ); @endcode
1780 * Regardless of whether the asynchronous operation completes immediately or
1781 * not, the completion handler will not be invoked from within this function.
1782 * On immediate completion, invocation of the handler will be performed in a
1783 * manner equivalent to using boost::asio::post().
1784 *
1785 * @par Completion Signature
1786 * @code void(boost::system::error_code) @endcode
1787 *
1788 * @par Example
1789 * @code
1790 * void wait_handler(const boost::system::error_code& error)
1791 * {
1792 * if (!error)
1793 * {
1794 * // Wait succeeded.
1795 * }
1796 * }
1797 *
1798 * ...
1799 *
1800 * boost::asio::ip::tcp::socket socket(my_context);
1801 * ...
1802 * socket.async_wait(boost::asio::ip::tcp::socket::wait_read, wait_handler);
1803 * @endcode
1804 *
1805 * @par Per-Operation Cancellation
1806 * On POSIX or Windows operating systems, this asynchronous operation supports
1807 * cancellation for the following boost::asio::cancellation_type values:
1808 *
1809 * @li @c cancellation_type::terminal
1810 *
1811 * @li @c cancellation_type::partial
1812 *
1813 * @li @c cancellation_type::total
1814 */
1815 template <
1816 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
1817 WaitToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
1818 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WaitToken,
1819 void (boost::system::error_code))
1820 async_wait(wait_type w,
1821 BOOST_ASIO_MOVE_ARG(WaitToken) token
1822 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
1823 {
1824 return async_initiate<WaitToken, void (boost::system::error_code)>(
1825 initiate_async_wait(this), token, w);
1826 }
1827
1828 protected:
1829 /// Protected destructor to prevent deletion through this type.
1830 /**
1831 * This function destroys the socket, cancelling any outstanding asynchronous
1832 * operations associated with the socket as if by calling @c cancel.
1833 */
1834 ~basic_socket()
1835 {
1836 }
1837
1838 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
1839 detail::io_object_impl<
1840 detail::null_socket_service<Protocol>, Executor> impl_;
1841 #elif defined(BOOST_ASIO_HAS_IOCP)
1842 detail::io_object_impl<
1843 detail::win_iocp_socket_service<Protocol>, Executor> impl_;
1844 #elif defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
1845 detail::io_object_impl<
1846 detail::io_uring_socket_service<Protocol>, Executor> impl_;
1847 #else
1848 detail::io_object_impl<
1849 detail::reactive_socket_service<Protocol>, Executor> impl_;
1850 #endif
1851
1852 private:
1853 // Disallow copying and assignment.
1854 basic_socket(const basic_socket&) BOOST_ASIO_DELETED;
1855 basic_socket& operator=(const basic_socket&) BOOST_ASIO_DELETED;
1856
1857 class initiate_async_connect
1858 {
1859 public:
1860 typedef Executor executor_type;
1861
1862 explicit initiate_async_connect(basic_socket* self)
1863 : self_(self)
1864 {
1865 }
1866
1867 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1868 {
1869 return self_->get_executor();
1870 }
1871
1872 template <typename ConnectHandler>
1873 void operator()(BOOST_ASIO_MOVE_ARG(ConnectHandler) handler,
1874 const endpoint_type& peer_endpoint,
1875 const boost::system::error_code& open_ec) const
1876 {
1877 // If you get an error on the following line it means that your handler
1878 // does not meet the documented type requirements for a ConnectHandler.
1879 BOOST_ASIO_CONNECT_HANDLER_CHECK(ConnectHandler, handler) type_check;
1880
1881 if (open_ec)
1882 {
1883 boost::asio::post(self_->impl_.get_executor(),
1884 boost::asio::detail::bind_handler(
1885 BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler), open_ec));
1886 }
1887 else
1888 {
1889 detail::non_const_lvalue<ConnectHandler> handler2(handler);
1890 self_->impl_.get_service().async_connect(
1891 self_->impl_.get_implementation(), peer_endpoint,
1892 handler2.value, self_->impl_.get_executor());
1893 }
1894 }
1895
1896 private:
1897 basic_socket* self_;
1898 };
1899
1900 class initiate_async_wait
1901 {
1902 public:
1903 typedef Executor executor_type;
1904
1905 explicit initiate_async_wait(basic_socket* self)
1906 : self_(self)
1907 {
1908 }
1909
1910 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
1911 {
1912 return self_->get_executor();
1913 }
1914
1915 template <typename WaitHandler>
1916 void operator()(BOOST_ASIO_MOVE_ARG(WaitHandler) handler, wait_type w) const
1917 {
1918 // If you get an error on the following line it means that your handler
1919 // does not meet the documented type requirements for a WaitHandler.
1920 BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
1921
1922 detail::non_const_lvalue<WaitHandler> handler2(handler);
1923 self_->impl_.get_service().async_wait(
1924 self_->impl_.get_implementation(), w,
1925 handler2.value, self_->impl_.get_executor());
1926 }
1927
1928 private:
1929 basic_socket* self_;
1930 };
1931 };
1932
1933 } // namespace asio
1934 } // namespace boost
1935
1936 #include <boost/asio/detail/pop_options.hpp>
1937
1938 #endif // BOOST_ASIO_BASIC_SOCKET_HPP