]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/basic_raw_socket.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / basic_raw_socket.hpp
1 //
2 // basic_raw_socket.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_BASIC_RAW_SOCKET_HPP
12 #define BOOST_ASIO_BASIC_RAW_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/detail/config.hpp>
19 #include <cstddef>
20 #include <boost/asio/basic_socket.hpp>
21 #include <boost/asio/detail/handler_type_requirements.hpp>
22 #include <boost/asio/detail/throw_error.hpp>
23 #include <boost/asio/detail/type_traits.hpp>
24 #include <boost/asio/error.hpp>
25
26 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
27 # include <boost/asio/raw_socket_service.hpp>
28 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
29
30 #include <boost/asio/detail/push_options.hpp>
31
32 namespace boost {
33 namespace asio {
34
35 /// Provides raw-oriented socket functionality.
36 /**
37 * The basic_raw_socket class template provides asynchronous and blocking
38 * raw-oriented socket functionality.
39 *
40 * @par Thread Safety
41 * @e Distinct @e objects: Safe.@n
42 * @e Shared @e objects: Unsafe.
43 */
44 template <typename Protocol
45 BOOST_ASIO_SVC_TPARAM_DEF1(= raw_socket_service<Protocol>)>
46 class basic_raw_socket
47 : public basic_socket<Protocol BOOST_ASIO_SVC_TARG>
48 {
49 public:
50 /// The native representation of a socket.
51 #if defined(GENERATING_DOCUMENTATION)
52 typedef implementation_defined native_handle_type;
53 #else
54 typedef typename basic_socket<
55 Protocol BOOST_ASIO_SVC_TARG>::native_handle_type native_handle_type;
56 #endif
57
58 /// The protocol type.
59 typedef Protocol protocol_type;
60
61 /// The endpoint type.
62 typedef typename Protocol::endpoint endpoint_type;
63
64 /// Construct a basic_raw_socket without opening it.
65 /**
66 * This constructor creates a raw socket without opening it. The open()
67 * function must be called before data can be sent or received on the socket.
68 *
69 * @param io_context The io_context object that the raw socket will use
70 * to dispatch handlers for any asynchronous operations performed on the
71 * socket.
72 */
73 explicit basic_raw_socket(boost::asio::io_context& io_context)
74 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(io_context)
75 {
76 }
77
78 /// Construct and open a basic_raw_socket.
79 /**
80 * This constructor creates and opens a raw socket.
81 *
82 * @param io_context The io_context object that the raw socket will use
83 * to dispatch handlers for any asynchronous operations performed on the
84 * socket.
85 *
86 * @param protocol An object specifying protocol parameters to be used.
87 *
88 * @throws boost::system::system_error Thrown on failure.
89 */
90 basic_raw_socket(boost::asio::io_context& io_context,
91 const protocol_type& protocol)
92 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(io_context, protocol)
93 {
94 }
95
96 /// Construct a basic_raw_socket, opening it and binding it to the given
97 /// local endpoint.
98 /**
99 * This constructor creates a raw socket and automatically opens it bound
100 * to the specified endpoint on the local machine. The protocol used is the
101 * protocol associated with the given endpoint.
102 *
103 * @param io_context The io_context object that the raw socket will use
104 * to dispatch handlers for any asynchronous operations performed on the
105 * socket.
106 *
107 * @param endpoint An endpoint on the local machine to which the raw
108 * socket will be bound.
109 *
110 * @throws boost::system::system_error Thrown on failure.
111 */
112 basic_raw_socket(boost::asio::io_context& io_context,
113 const endpoint_type& endpoint)
114 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(io_context, endpoint)
115 {
116 }
117
118 /// Construct a basic_raw_socket on an existing native socket.
119 /**
120 * This constructor creates a raw socket object to hold an existing
121 * native socket.
122 *
123 * @param io_context The io_context object that the raw socket will use
124 * to dispatch handlers for any asynchronous operations performed on the
125 * socket.
126 *
127 * @param protocol An object specifying protocol parameters to be used.
128 *
129 * @param native_socket The new underlying socket implementation.
130 *
131 * @throws boost::system::system_error Thrown on failure.
132 */
133 basic_raw_socket(boost::asio::io_context& io_context,
134 const protocol_type& protocol, const native_handle_type& native_socket)
135 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(
136 io_context, protocol, native_socket)
137 {
138 }
139
140 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
141 /// Move-construct a basic_raw_socket from another.
142 /**
143 * This constructor moves a raw socket from one object to another.
144 *
145 * @param other The other basic_raw_socket object from which the move
146 * will occur.
147 *
148 * @note Following the move, the moved-from object is in the same state as if
149 * constructed using the @c basic_raw_socket(io_context&) constructor.
150 */
151 basic_raw_socket(basic_raw_socket&& other)
152 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(std::move(other))
153 {
154 }
155
156 /// Move-assign a basic_raw_socket from another.
157 /**
158 * This assignment operator moves a raw socket from one object to another.
159 *
160 * @param other The other basic_raw_socket object from which the move
161 * will occur.
162 *
163 * @note Following the move, the moved-from object is in the same state as if
164 * constructed using the @c basic_raw_socket(io_context&) constructor.
165 */
166 basic_raw_socket& operator=(basic_raw_socket&& other)
167 {
168 basic_socket<Protocol BOOST_ASIO_SVC_TARG>::operator=(std::move(other));
169 return *this;
170 }
171
172 /// Move-construct a basic_raw_socket from a socket of another protocol type.
173 /**
174 * This constructor moves a raw socket from one object to another.
175 *
176 * @param other The other basic_raw_socket object from which the move will
177 * occur.
178 *
179 * @note Following the move, the moved-from object is in the same state as if
180 * constructed using the @c basic_raw_socket(io_context&) constructor.
181 */
182 template <typename Protocol1 BOOST_ASIO_SVC_TPARAM1>
183 basic_raw_socket(basic_raw_socket<Protocol1 BOOST_ASIO_SVC_TARG1>&& other,
184 typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
185 : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(std::move(other))
186 {
187 }
188
189 /// Move-assign a basic_raw_socket from a socket of another protocol type.
190 /**
191 * This assignment operator moves a raw socket from one object to another.
192 *
193 * @param other The other basic_raw_socket object from which the move
194 * will occur.
195 *
196 * @note Following the move, the moved-from object is in the same state as if
197 * constructed using the @c basic_raw_socket(io_context&) constructor.
198 */
199 template <typename Protocol1 BOOST_ASIO_SVC_TPARAM1>
200 typename enable_if<is_convertible<Protocol1, Protocol>::value,
201 basic_raw_socket>::type& operator=(
202 basic_raw_socket<Protocol1 BOOST_ASIO_SVC_TARG1>&& other)
203 {
204 basic_socket<Protocol BOOST_ASIO_SVC_TARG>::operator=(std::move(other));
205 return *this;
206 }
207 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
208
209 /// Destroys the socket.
210 /**
211 * This function destroys the socket, cancelling any outstanding asynchronous
212 * operations associated with the socket as if by calling @c cancel.
213 */
214 ~basic_raw_socket()
215 {
216 }
217
218 /// Send some data on a connected socket.
219 /**
220 * This function is used to send data on the raw socket. The function call
221 * will block until the data has been sent successfully or an error occurs.
222 *
223 * @param buffers One ore more data buffers to be sent on the socket.
224 *
225 * @returns The number of bytes sent.
226 *
227 * @throws boost::system::system_error Thrown on failure.
228 *
229 * @note The send operation can only be used with a connected socket. Use
230 * the send_to function to send data on an unconnected raw socket.
231 *
232 * @par Example
233 * To send a single data buffer use the @ref buffer function as follows:
234 * @code socket.send(boost::asio::buffer(data, size)); @endcode
235 * See the @ref buffer documentation for information on sending multiple
236 * buffers in one go, and how to use it with arrays, boost::array or
237 * std::vector.
238 */
239 template <typename ConstBufferSequence>
240 std::size_t send(const ConstBufferSequence& buffers)
241 {
242 boost::system::error_code ec;
243 std::size_t s = this->get_service().send(
244 this->get_implementation(), buffers, 0, ec);
245 boost::asio::detail::throw_error(ec, "send");
246 return s;
247 }
248
249 /// Send some data on a connected socket.
250 /**
251 * This function is used to send data on the raw socket. The function call
252 * will block until the data has been sent successfully or an error occurs.
253 *
254 * @param buffers One ore more data buffers to be sent on the socket.
255 *
256 * @param flags Flags specifying how the send call is to be made.
257 *
258 * @returns The number of bytes sent.
259 *
260 * @throws boost::system::system_error Thrown on failure.
261 *
262 * @note The send operation can only be used with a connected socket. Use
263 * the send_to function to send data on an unconnected raw socket.
264 */
265 template <typename ConstBufferSequence>
266 std::size_t send(const ConstBufferSequence& buffers,
267 socket_base::message_flags flags)
268 {
269 boost::system::error_code ec;
270 std::size_t s = this->get_service().send(
271 this->get_implementation(), buffers, flags, ec);
272 boost::asio::detail::throw_error(ec, "send");
273 return s;
274 }
275
276 /// Send some data on a connected socket.
277 /**
278 * This function is used to send data on the raw socket. The function call
279 * will block until the data has been sent successfully or an error occurs.
280 *
281 * @param buffers One or more data buffers to be sent on the socket.
282 *
283 * @param flags Flags specifying how the send call is to be made.
284 *
285 * @param ec Set to indicate what error occurred, if any.
286 *
287 * @returns The number of bytes sent.
288 *
289 * @note The send operation can only be used with a connected socket. Use
290 * the send_to function to send data on an unconnected raw socket.
291 */
292 template <typename ConstBufferSequence>
293 std::size_t send(const ConstBufferSequence& buffers,
294 socket_base::message_flags flags, boost::system::error_code& ec)
295 {
296 return this->get_service().send(
297 this->get_implementation(), buffers, flags, ec);
298 }
299
300 /// Start an asynchronous send on a connected socket.
301 /**
302 * This function is used to send data on the raw socket. The function call
303 * will block until the data has been sent successfully or an error occurs.
304 *
305 * @param buffers One or more data buffers to be sent on the socket. Although
306 * the buffers object may be copied as necessary, ownership of the underlying
307 * memory blocks is retained by the caller, which must guarantee that they
308 * remain valid until the handler is called.
309 *
310 * @param handler The handler to be called when the send operation completes.
311 * Copies will be made of the handler as required. The function signature of
312 * the handler must be:
313 * @code void handler(
314 * const boost::system::error_code& error, // Result of operation.
315 * std::size_t bytes_transferred // Number of bytes sent.
316 * ); @endcode
317 * Regardless of whether the asynchronous operation completes immediately or
318 * not, the handler will not be invoked from within this function. Invocation
319 * of the handler will be performed in a manner equivalent to using
320 * boost::asio::io_context::post().
321 *
322 * @note The async_send operation can only be used with a connected socket.
323 * Use the async_send_to function to send data on an unconnected raw
324 * socket.
325 *
326 * @par Example
327 * To send a single data buffer use the @ref buffer function as follows:
328 * @code
329 * socket.async_send(boost::asio::buffer(data, size), handler);
330 * @endcode
331 * See the @ref buffer documentation for information on sending multiple
332 * buffers in one go, and how to use it with arrays, boost::array or
333 * std::vector.
334 */
335 template <typename ConstBufferSequence, typename WriteHandler>
336 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
337 void (boost::system::error_code, std::size_t))
338 async_send(const ConstBufferSequence& buffers,
339 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
340 {
341 // If you get an error on the following line it means that your handler does
342 // not meet the documented type requirements for a WriteHandler.
343 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
344
345 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
346 return this->get_service().async_send(this->get_implementation(),
347 buffers, 0, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
348 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
349 async_completion<WriteHandler,
350 void (boost::system::error_code, std::size_t)> init(handler);
351
352 this->get_service().async_send(this->get_implementation(),
353 buffers, 0, init.completion_handler);
354
355 return init.result.get();
356 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
357 }
358
359 /// Start an asynchronous send on a connected socket.
360 /**
361 * This function is used to send data on the raw socket. The function call
362 * will block until the data has been sent successfully or an error occurs.
363 *
364 * @param buffers One or more data buffers to be sent on the socket. Although
365 * the buffers object may be copied as necessary, ownership of the underlying
366 * memory blocks is retained by the caller, which must guarantee that they
367 * remain valid until the handler is called.
368 *
369 * @param flags Flags specifying how the send call is to be made.
370 *
371 * @param handler The handler to be called when the send operation completes.
372 * Copies will be made of the handler as required. The function signature of
373 * the handler must be:
374 * @code void handler(
375 * const boost::system::error_code& error, // Result of operation.
376 * std::size_t bytes_transferred // Number of bytes sent.
377 * ); @endcode
378 * Regardless of whether the asynchronous operation completes immediately or
379 * not, the handler will not be invoked from within this function. Invocation
380 * of the handler will be performed in a manner equivalent to using
381 * boost::asio::io_context::post().
382 *
383 * @note The async_send operation can only be used with a connected socket.
384 * Use the async_send_to function to send data on an unconnected raw
385 * socket.
386 */
387 template <typename ConstBufferSequence, typename WriteHandler>
388 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
389 void (boost::system::error_code, std::size_t))
390 async_send(const ConstBufferSequence& buffers,
391 socket_base::message_flags flags,
392 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
393 {
394 // If you get an error on the following line it means that your handler does
395 // not meet the documented type requirements for a WriteHandler.
396 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
397
398 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
399 return this->get_service().async_send(this->get_implementation(),
400 buffers, flags, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
401 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
402 async_completion<WriteHandler,
403 void (boost::system::error_code, std::size_t)> init(handler);
404
405 this->get_service().async_send(this->get_implementation(),
406 buffers, flags, init.completion_handler);
407
408 return init.result.get();
409 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
410 }
411
412 /// Send raw data to the specified endpoint.
413 /**
414 * This function is used to send raw data to the specified remote endpoint.
415 * The function call will block until the data has been sent successfully or
416 * an error occurs.
417 *
418 * @param buffers One or more data buffers to be sent to the remote endpoint.
419 *
420 * @param destination The remote endpoint to which the data will be sent.
421 *
422 * @returns The number of bytes sent.
423 *
424 * @throws boost::system::system_error Thrown on failure.
425 *
426 * @par Example
427 * To send a single data buffer use the @ref buffer function as follows:
428 * @code
429 * boost::asio::ip::udp::endpoint destination(
430 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
431 * socket.send_to(boost::asio::buffer(data, size), destination);
432 * @endcode
433 * See the @ref buffer documentation for information on sending multiple
434 * buffers in one go, and how to use it with arrays, boost::array or
435 * std::vector.
436 */
437 template <typename ConstBufferSequence>
438 std::size_t send_to(const ConstBufferSequence& buffers,
439 const endpoint_type& destination)
440 {
441 boost::system::error_code ec;
442 std::size_t s = this->get_service().send_to(
443 this->get_implementation(), buffers, destination, 0, ec);
444 boost::asio::detail::throw_error(ec, "send_to");
445 return s;
446 }
447
448 /// Send raw data to the specified endpoint.
449 /**
450 * This function is used to send raw data to the specified remote endpoint.
451 * The function call will block until the data has been sent successfully or
452 * an error occurs.
453 *
454 * @param buffers One or more data buffers to be sent to the remote endpoint.
455 *
456 * @param destination The remote endpoint to which the data will be sent.
457 *
458 * @param flags Flags specifying how the send call is to be made.
459 *
460 * @returns The number of bytes sent.
461 *
462 * @throws boost::system::system_error Thrown on failure.
463 */
464 template <typename ConstBufferSequence>
465 std::size_t send_to(const ConstBufferSequence& buffers,
466 const endpoint_type& destination, socket_base::message_flags flags)
467 {
468 boost::system::error_code ec;
469 std::size_t s = this->get_service().send_to(
470 this->get_implementation(), buffers, destination, flags, ec);
471 boost::asio::detail::throw_error(ec, "send_to");
472 return s;
473 }
474
475 /// Send raw data to the specified endpoint.
476 /**
477 * This function is used to send raw data to the specified remote endpoint.
478 * The function call will block until the data has been sent successfully or
479 * an error occurs.
480 *
481 * @param buffers One or more data buffers to be sent to the remote endpoint.
482 *
483 * @param destination The remote endpoint to which the data will be sent.
484 *
485 * @param flags Flags specifying how the send call is to be made.
486 *
487 * @param ec Set to indicate what error occurred, if any.
488 *
489 * @returns The number of bytes sent.
490 */
491 template <typename ConstBufferSequence>
492 std::size_t send_to(const ConstBufferSequence& buffers,
493 const endpoint_type& destination, socket_base::message_flags flags,
494 boost::system::error_code& ec)
495 {
496 return this->get_service().send_to(this->get_implementation(),
497 buffers, destination, flags, ec);
498 }
499
500 /// Start an asynchronous send.
501 /**
502 * This function is used to asynchronously send raw data to the specified
503 * remote endpoint. The function call always returns immediately.
504 *
505 * @param buffers One or more data buffers to be sent to the remote endpoint.
506 * Although the buffers object may be copied as necessary, ownership of the
507 * underlying memory blocks is retained by the caller, which must guarantee
508 * that they remain valid until the handler is called.
509 *
510 * @param destination The remote endpoint to which the data will be sent.
511 * Copies will be made of the endpoint as required.
512 *
513 * @param handler The handler to be called when the send operation completes.
514 * Copies will be made of the handler as required. The function signature of
515 * the handler must be:
516 * @code void handler(
517 * const boost::system::error_code& error, // Result of operation.
518 * std::size_t bytes_transferred // Number of bytes sent.
519 * ); @endcode
520 * Regardless of whether the asynchronous operation completes immediately or
521 * not, the handler will not be invoked from within this function. Invocation
522 * of the handler will be performed in a manner equivalent to using
523 * boost::asio::io_context::post().
524 *
525 * @par Example
526 * To send a single data buffer use the @ref buffer function as follows:
527 * @code
528 * boost::asio::ip::udp::endpoint destination(
529 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
530 * socket.async_send_to(
531 * boost::asio::buffer(data, size), destination, handler);
532 * @endcode
533 * See the @ref buffer documentation for information on sending multiple
534 * buffers in one go, and how to use it with arrays, boost::array or
535 * std::vector.
536 */
537 template <typename ConstBufferSequence, typename WriteHandler>
538 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
539 void (boost::system::error_code, std::size_t))
540 async_send_to(const ConstBufferSequence& buffers,
541 const endpoint_type& destination,
542 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
543 {
544 // If you get an error on the following line it means that your handler does
545 // not meet the documented type requirements for a WriteHandler.
546 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
547
548 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
549 return this->get_service().async_send_to(this->get_implementation(),
550 buffers, destination, 0, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
551 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
552 async_completion<WriteHandler,
553 void (boost::system::error_code, std::size_t)> init(handler);
554
555 this->get_service().async_send_to(this->get_implementation(),
556 buffers, destination, 0, init.completion_handler);
557
558 return init.result.get();
559 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
560 }
561
562 /// Start an asynchronous send.
563 /**
564 * This function is used to asynchronously send raw data to the specified
565 * remote endpoint. The function call always returns immediately.
566 *
567 * @param buffers One or more data buffers to be sent to the remote endpoint.
568 * Although the buffers object may be copied as necessary, ownership of the
569 * underlying memory blocks is retained by the caller, which must guarantee
570 * that they remain valid until the handler is called.
571 *
572 * @param flags Flags specifying how the send call is to be made.
573 *
574 * @param destination The remote endpoint to which the data will be sent.
575 * Copies will be made of the endpoint as required.
576 *
577 * @param handler The handler to be called when the send operation completes.
578 * Copies will be made of the handler as required. The function signature of
579 * the handler must be:
580 * @code void handler(
581 * const boost::system::error_code& error, // Result of operation.
582 * std::size_t bytes_transferred // Number of bytes sent.
583 * ); @endcode
584 * Regardless of whether the asynchronous operation completes immediately or
585 * not, the handler will not be invoked from within this function. Invocation
586 * of the handler will be performed in a manner equivalent to using
587 * boost::asio::io_context::post().
588 */
589 template <typename ConstBufferSequence, typename WriteHandler>
590 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
591 void (boost::system::error_code, std::size_t))
592 async_send_to(const ConstBufferSequence& buffers,
593 const endpoint_type& destination, socket_base::message_flags flags,
594 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
595 {
596 // If you get an error on the following line it means that your handler does
597 // not meet the documented type requirements for a WriteHandler.
598 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
599
600 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
601 return this->get_service().async_send_to(
602 this->get_implementation(), buffers, destination, flags,
603 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
604 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
605 async_completion<WriteHandler,
606 void (boost::system::error_code, std::size_t)> init(handler);
607
608 this->get_service().async_send_to(
609 this->get_implementation(), buffers, destination, flags,
610 init.completion_handler);
611
612 return init.result.get();
613 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
614 }
615
616 /// Receive some data on a connected socket.
617 /**
618 * This function is used to receive data on the raw socket. The function
619 * call will block until data has been received successfully or an error
620 * occurs.
621 *
622 * @param buffers One or more buffers into which the data will be received.
623 *
624 * @returns The number of bytes received.
625 *
626 * @throws boost::system::system_error Thrown on failure.
627 *
628 * @note The receive operation can only be used with a connected socket. Use
629 * the receive_from function to receive data on an unconnected raw
630 * socket.
631 *
632 * @par Example
633 * To receive into a single data buffer use the @ref buffer function as
634 * follows:
635 * @code socket.receive(boost::asio::buffer(data, size)); @endcode
636 * See the @ref buffer documentation for information on receiving into
637 * multiple buffers in one go, and how to use it with arrays, boost::array or
638 * std::vector.
639 */
640 template <typename MutableBufferSequence>
641 std::size_t receive(const MutableBufferSequence& buffers)
642 {
643 boost::system::error_code ec;
644 std::size_t s = this->get_service().receive(
645 this->get_implementation(), buffers, 0, ec);
646 boost::asio::detail::throw_error(ec, "receive");
647 return s;
648 }
649
650 /// Receive some data on a connected socket.
651 /**
652 * This function is used to receive data on the raw socket. The function
653 * call will block until data has been received successfully or an error
654 * occurs.
655 *
656 * @param buffers One or more buffers into which the data will be received.
657 *
658 * @param flags Flags specifying how the receive call is to be made.
659 *
660 * @returns The number of bytes received.
661 *
662 * @throws boost::system::system_error Thrown on failure.
663 *
664 * @note The receive operation can only be used with a connected socket. Use
665 * the receive_from function to receive data on an unconnected raw
666 * socket.
667 */
668 template <typename MutableBufferSequence>
669 std::size_t receive(const MutableBufferSequence& buffers,
670 socket_base::message_flags flags)
671 {
672 boost::system::error_code ec;
673 std::size_t s = this->get_service().receive(
674 this->get_implementation(), buffers, flags, ec);
675 boost::asio::detail::throw_error(ec, "receive");
676 return s;
677 }
678
679 /// Receive some data on a connected socket.
680 /**
681 * This function is used to receive data on the raw socket. The function
682 * call will block until data has been received successfully or an error
683 * occurs.
684 *
685 * @param buffers One or more buffers into which the data will be received.
686 *
687 * @param flags Flags specifying how the receive call is to be made.
688 *
689 * @param ec Set to indicate what error occurred, if any.
690 *
691 * @returns The number of bytes received.
692 *
693 * @note The receive operation can only be used with a connected socket. Use
694 * the receive_from function to receive data on an unconnected raw
695 * socket.
696 */
697 template <typename MutableBufferSequence>
698 std::size_t receive(const MutableBufferSequence& buffers,
699 socket_base::message_flags flags, boost::system::error_code& ec)
700 {
701 return this->get_service().receive(
702 this->get_implementation(), buffers, flags, ec);
703 }
704
705 /// Start an asynchronous receive on a connected socket.
706 /**
707 * This function is used to asynchronously receive data from the raw
708 * socket. The function call always returns immediately.
709 *
710 * @param buffers One or more buffers into which the data will be received.
711 * Although the buffers object may be copied as necessary, ownership of the
712 * underlying memory blocks is retained by the caller, which must guarantee
713 * that they remain valid until the handler is called.
714 *
715 * @param handler The handler to be called when the receive operation
716 * completes. Copies will be made of the handler as required. The function
717 * signature of the handler must be:
718 * @code void handler(
719 * const boost::system::error_code& error, // Result of operation.
720 * std::size_t bytes_transferred // Number of bytes received.
721 * ); @endcode
722 * Regardless of whether the asynchronous operation completes immediately or
723 * not, the handler will not be invoked from within this function. Invocation
724 * of the handler will be performed in a manner equivalent to using
725 * boost::asio::io_context::post().
726 *
727 * @note The async_receive operation can only be used with a connected socket.
728 * Use the async_receive_from function to receive data on an unconnected
729 * raw socket.
730 *
731 * @par Example
732 * To receive into a single data buffer use the @ref buffer function as
733 * follows:
734 * @code
735 * socket.async_receive(boost::asio::buffer(data, size), handler);
736 * @endcode
737 * See the @ref buffer documentation for information on receiving into
738 * multiple buffers in one go, and how to use it with arrays, boost::array or
739 * std::vector.
740 */
741 template <typename MutableBufferSequence, typename ReadHandler>
742 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
743 void (boost::system::error_code, std::size_t))
744 async_receive(const MutableBufferSequence& buffers,
745 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
746 {
747 // If you get an error on the following line it means that your handler does
748 // not meet the documented type requirements for a ReadHandler.
749 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
750
751 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
752 return this->get_service().async_receive(this->get_implementation(),
753 buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
754 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
755 async_completion<ReadHandler,
756 void (boost::system::error_code, std::size_t)> init(handler);
757
758 this->get_service().async_receive(this->get_implementation(),
759 buffers, 0, init.completion_handler);
760
761 return init.result.get();
762 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
763 }
764
765 /// Start an asynchronous receive on a connected socket.
766 /**
767 * This function is used to asynchronously receive data from the raw
768 * socket. The function call always returns immediately.
769 *
770 * @param buffers One or more buffers into which the data will be received.
771 * Although the buffers object may be copied as necessary, ownership of the
772 * underlying memory blocks is retained by the caller, which must guarantee
773 * that they remain valid until the handler is called.
774 *
775 * @param flags Flags specifying how the receive call is to be made.
776 *
777 * @param handler The handler to be called when the receive operation
778 * completes. Copies will be made of the handler as required. The function
779 * signature of the handler must be:
780 * @code void handler(
781 * const boost::system::error_code& error, // Result of operation.
782 * std::size_t bytes_transferred // Number of bytes received.
783 * ); @endcode
784 * Regardless of whether the asynchronous operation completes immediately or
785 * not, the handler will not be invoked from within this function. Invocation
786 * of the handler will be performed in a manner equivalent to using
787 * boost::asio::io_context::post().
788 *
789 * @note The async_receive operation can only be used with a connected socket.
790 * Use the async_receive_from function to receive data on an unconnected
791 * raw socket.
792 */
793 template <typename MutableBufferSequence, typename ReadHandler>
794 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
795 void (boost::system::error_code, std::size_t))
796 async_receive(const MutableBufferSequence& buffers,
797 socket_base::message_flags flags,
798 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
799 {
800 // If you get an error on the following line it means that your handler does
801 // not meet the documented type requirements for a ReadHandler.
802 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
803
804 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
805 return this->get_service().async_receive(this->get_implementation(),
806 buffers, flags, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
807 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
808 async_completion<ReadHandler,
809 void (boost::system::error_code, std::size_t)> init(handler);
810
811 this->get_service().async_receive(this->get_implementation(),
812 buffers, flags, init.completion_handler);
813
814 return init.result.get();
815 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
816 }
817
818 /// Receive raw data with the endpoint of the sender.
819 /**
820 * This function is used to receive raw data. The function call will block
821 * until data has been received successfully or an error occurs.
822 *
823 * @param buffers One or more buffers into which the data will be received.
824 *
825 * @param sender_endpoint An endpoint object that receives the endpoint of
826 * the remote sender of the data.
827 *
828 * @returns The number of bytes received.
829 *
830 * @throws boost::system::system_error Thrown on failure.
831 *
832 * @par Example
833 * To receive into a single data buffer use the @ref buffer function as
834 * follows:
835 * @code
836 * boost::asio::ip::udp::endpoint sender_endpoint;
837 * socket.receive_from(
838 * boost::asio::buffer(data, size), sender_endpoint);
839 * @endcode
840 * See the @ref buffer documentation for information on receiving into
841 * multiple buffers in one go, and how to use it with arrays, boost::array or
842 * std::vector.
843 */
844 template <typename MutableBufferSequence>
845 std::size_t receive_from(const MutableBufferSequence& buffers,
846 endpoint_type& sender_endpoint)
847 {
848 boost::system::error_code ec;
849 std::size_t s = this->get_service().receive_from(
850 this->get_implementation(), buffers, sender_endpoint, 0, ec);
851 boost::asio::detail::throw_error(ec, "receive_from");
852 return s;
853 }
854
855 /// Receive raw data with the endpoint of the sender.
856 /**
857 * This function is used to receive raw data. The function call will block
858 * until data has been received successfully or an error occurs.
859 *
860 * @param buffers One or more buffers into which the data will be received.
861 *
862 * @param sender_endpoint An endpoint object that receives the endpoint of
863 * the remote sender of the data.
864 *
865 * @param flags Flags specifying how the receive call is to be made.
866 *
867 * @returns The number of bytes received.
868 *
869 * @throws boost::system::system_error Thrown on failure.
870 */
871 template <typename MutableBufferSequence>
872 std::size_t receive_from(const MutableBufferSequence& buffers,
873 endpoint_type& sender_endpoint, socket_base::message_flags flags)
874 {
875 boost::system::error_code ec;
876 std::size_t s = this->get_service().receive_from(
877 this->get_implementation(), buffers, sender_endpoint, flags, ec);
878 boost::asio::detail::throw_error(ec, "receive_from");
879 return s;
880 }
881
882 /// Receive raw data with the endpoint of the sender.
883 /**
884 * This function is used to receive raw data. The function call will block
885 * until data has been received successfully or an error occurs.
886 *
887 * @param buffers One or more buffers into which the data will be received.
888 *
889 * @param sender_endpoint An endpoint object that receives the endpoint of
890 * the remote sender of the data.
891 *
892 * @param flags Flags specifying how the receive call is to be made.
893 *
894 * @param ec Set to indicate what error occurred, if any.
895 *
896 * @returns The number of bytes received.
897 */
898 template <typename MutableBufferSequence>
899 std::size_t receive_from(const MutableBufferSequence& buffers,
900 endpoint_type& sender_endpoint, socket_base::message_flags flags,
901 boost::system::error_code& ec)
902 {
903 return this->get_service().receive_from(this->get_implementation(),
904 buffers, sender_endpoint, flags, ec);
905 }
906
907 /// Start an asynchronous receive.
908 /**
909 * This function is used to asynchronously receive raw data. The function
910 * call always returns immediately.
911 *
912 * @param buffers One or more buffers into which the data will be received.
913 * Although the buffers object may be copied as necessary, ownership of the
914 * underlying memory blocks is retained by the caller, which must guarantee
915 * that they remain valid until the handler is called.
916 *
917 * @param sender_endpoint An endpoint object that receives the endpoint of
918 * the remote sender of the data. Ownership of the sender_endpoint object
919 * is retained by the caller, which must guarantee that it is valid until the
920 * handler is called.
921 *
922 * @param handler The handler to be called when the receive operation
923 * completes. Copies will be made of the handler as required. The function
924 * signature of the handler must be:
925 * @code void handler(
926 * const boost::system::error_code& error, // Result of operation.
927 * std::size_t bytes_transferred // Number of bytes received.
928 * ); @endcode
929 * Regardless of whether the asynchronous operation completes immediately or
930 * not, the handler will not be invoked from within this function. Invocation
931 * of the handler will be performed in a manner equivalent to using
932 * boost::asio::io_context::post().
933 *
934 * @par Example
935 * To receive into a single data buffer use the @ref buffer function as
936 * follows:
937 * @code socket.async_receive_from(
938 * boost::asio::buffer(data, size), 0, sender_endpoint, handler); @endcode
939 * See the @ref buffer documentation for information on receiving into
940 * multiple buffers in one go, and how to use it with arrays, boost::array or
941 * std::vector.
942 */
943 template <typename MutableBufferSequence, typename ReadHandler>
944 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
945 void (boost::system::error_code, std::size_t))
946 async_receive_from(const MutableBufferSequence& buffers,
947 endpoint_type& sender_endpoint,
948 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
949 {
950 // If you get an error on the following line it means that your handler does
951 // not meet the documented type requirements for a ReadHandler.
952 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
953
954 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
955 return this->get_service().async_receive_from(
956 this->get_implementation(), buffers, sender_endpoint, 0,
957 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
958 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
959 async_completion<ReadHandler,
960 void (boost::system::error_code, std::size_t)> init(handler);
961
962 this->get_service().async_receive_from(
963 this->get_implementation(), buffers, sender_endpoint, 0,
964 init.completion_handler);
965
966 return init.result.get();
967 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
968 }
969
970 /// Start an asynchronous receive.
971 /**
972 * This function is used to asynchronously receive raw data. The function
973 * call always returns immediately.
974 *
975 * @param buffers One or more buffers into which the data will be received.
976 * Although the buffers object may be copied as necessary, ownership of the
977 * underlying memory blocks is retained by the caller, which must guarantee
978 * that they remain valid until the handler is called.
979 *
980 * @param sender_endpoint An endpoint object that receives the endpoint of
981 * the remote sender of the data. Ownership of the sender_endpoint object
982 * is retained by the caller, which must guarantee that it is valid until the
983 * handler is called.
984 *
985 * @param flags Flags specifying how the receive call is to be made.
986 *
987 * @param handler The handler to be called when the receive operation
988 * completes. Copies will be made of the handler as required. The function
989 * signature of the handler must be:
990 * @code void handler(
991 * const boost::system::error_code& error, // Result of operation.
992 * std::size_t bytes_transferred // Number of bytes received.
993 * ); @endcode
994 * Regardless of whether the asynchronous operation completes immediately or
995 * not, the handler will not be invoked from within this function. Invocation
996 * of the handler will be performed in a manner equivalent to using
997 * boost::asio::io_context::post().
998 */
999 template <typename MutableBufferSequence, typename ReadHandler>
1000 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
1001 void (boost::system::error_code, std::size_t))
1002 async_receive_from(const MutableBufferSequence& buffers,
1003 endpoint_type& sender_endpoint, socket_base::message_flags flags,
1004 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
1005 {
1006 // If you get an error on the following line it means that your handler does
1007 // not meet the documented type requirements for a ReadHandler.
1008 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
1009
1010 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
1011 return this->get_service().async_receive_from(
1012 this->get_implementation(), buffers, sender_endpoint, flags,
1013 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
1014 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
1015 async_completion<ReadHandler,
1016 void (boost::system::error_code, std::size_t)> init(handler);
1017
1018 this->get_service().async_receive_from(
1019 this->get_implementation(), buffers, sender_endpoint, flags,
1020 init.completion_handler);
1021
1022 return init.result.get();
1023 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
1024 }
1025 };
1026
1027 } // namespace asio
1028 } // namespace boost
1029
1030 #include <boost/asio/detail/pop_options.hpp>
1031
1032 #endif // BOOST_ASIO_BASIC_RAW_SOCKET_HPP