Chris@16
|
1 //
|
Chris@16
|
2 // basic_stream_socket.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_BASIC_STREAM_SOCKET_HPP
|
Chris@16
|
12 #define BOOST_ASIO_BASIC_STREAM_SOCKET_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19 #include <cstddef>
|
Chris@16
|
20 #include <boost/asio/async_result.hpp>
|
Chris@16
|
21 #include <boost/asio/basic_socket.hpp>
|
Chris@16
|
22 #include <boost/asio/detail/handler_type_requirements.hpp>
|
Chris@16
|
23 #include <boost/asio/detail/throw_error.hpp>
|
Chris@16
|
24 #include <boost/asio/error.hpp>
|
Chris@16
|
25 #include <boost/asio/stream_socket_service.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 namespace boost {
|
Chris@16
|
30 namespace asio {
|
Chris@16
|
31
|
Chris@16
|
32 /// Provides stream-oriented socket functionality.
|
Chris@16
|
33 /**
|
Chris@16
|
34 * The basic_stream_socket class template provides asynchronous and blocking
|
Chris@16
|
35 * stream-oriented socket functionality.
|
Chris@16
|
36 *
|
Chris@16
|
37 * @par Thread Safety
|
Chris@16
|
38 * @e Distinct @e objects: Safe.@n
|
Chris@16
|
39 * @e Shared @e objects: Unsafe.
|
Chris@16
|
40 *
|
Chris@16
|
41 * @par Concepts:
|
Chris@16
|
42 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
|
Chris@16
|
43 */
|
Chris@16
|
44 template <typename Protocol,
|
Chris@16
|
45 typename StreamSocketService = stream_socket_service<Protocol> >
|
Chris@16
|
46 class basic_stream_socket
|
Chris@16
|
47 : public basic_socket<Protocol, StreamSocketService>
|
Chris@16
|
48 {
|
Chris@16
|
49 public:
|
Chris@16
|
50 /// (Deprecated: Use native_handle_type.) The native representation of a
|
Chris@16
|
51 /// socket.
|
Chris@16
|
52 typedef typename StreamSocketService::native_handle_type native_type;
|
Chris@16
|
53
|
Chris@16
|
54 /// The native representation of a socket.
|
Chris@16
|
55 typedef typename StreamSocketService::native_handle_type native_handle_type;
|
Chris@16
|
56
|
Chris@16
|
57 /// The protocol type.
|
Chris@16
|
58 typedef Protocol protocol_type;
|
Chris@16
|
59
|
Chris@16
|
60 /// The endpoint type.
|
Chris@16
|
61 typedef typename Protocol::endpoint endpoint_type;
|
Chris@16
|
62
|
Chris@16
|
63 /// Construct a basic_stream_socket without opening it.
|
Chris@16
|
64 /**
|
Chris@16
|
65 * This constructor creates a stream socket without opening it. The socket
|
Chris@16
|
66 * needs to be opened and then connected or accepted before data can be sent
|
Chris@16
|
67 * or received on it.
|
Chris@16
|
68 *
|
Chris@16
|
69 * @param io_service The io_service object that the stream socket will use to
|
Chris@16
|
70 * dispatch handlers for any asynchronous operations performed on the socket.
|
Chris@16
|
71 */
|
Chris@16
|
72 explicit basic_stream_socket(boost::asio::io_service& io_service)
|
Chris@16
|
73 : basic_socket<Protocol, StreamSocketService>(io_service)
|
Chris@16
|
74 {
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 /// Construct and open a basic_stream_socket.
|
Chris@16
|
78 /**
|
Chris@16
|
79 * This constructor creates and opens a stream socket. The socket needs to be
|
Chris@16
|
80 * connected or accepted before data can be sent or received on it.
|
Chris@16
|
81 *
|
Chris@16
|
82 * @param io_service The io_service object that the stream socket will use to
|
Chris@16
|
83 * dispatch handlers for any asynchronous operations performed on the socket.
|
Chris@16
|
84 *
|
Chris@16
|
85 * @param protocol An object specifying protocol parameters to be used.
|
Chris@16
|
86 *
|
Chris@16
|
87 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
88 */
|
Chris@16
|
89 basic_stream_socket(boost::asio::io_service& io_service,
|
Chris@16
|
90 const protocol_type& protocol)
|
Chris@16
|
91 : basic_socket<Protocol, StreamSocketService>(io_service, protocol)
|
Chris@16
|
92 {
|
Chris@16
|
93 }
|
Chris@16
|
94
|
Chris@16
|
95 /// Construct a basic_stream_socket, opening it and binding it to the given
|
Chris@16
|
96 /// local endpoint.
|
Chris@16
|
97 /**
|
Chris@16
|
98 * This constructor creates a stream socket and automatically opens it bound
|
Chris@16
|
99 * to the specified endpoint on the local machine. The protocol used is the
|
Chris@16
|
100 * protocol associated with the given endpoint.
|
Chris@16
|
101 *
|
Chris@16
|
102 * @param io_service The io_service object that the stream socket will use to
|
Chris@16
|
103 * dispatch handlers for any asynchronous operations performed on the socket.
|
Chris@16
|
104 *
|
Chris@16
|
105 * @param endpoint An endpoint on the local machine to which the stream
|
Chris@16
|
106 * socket will be bound.
|
Chris@16
|
107 *
|
Chris@16
|
108 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
109 */
|
Chris@16
|
110 basic_stream_socket(boost::asio::io_service& io_service,
|
Chris@16
|
111 const endpoint_type& endpoint)
|
Chris@16
|
112 : basic_socket<Protocol, StreamSocketService>(io_service, endpoint)
|
Chris@16
|
113 {
|
Chris@16
|
114 }
|
Chris@16
|
115
|
Chris@16
|
116 /// Construct a basic_stream_socket on an existing native socket.
|
Chris@16
|
117 /**
|
Chris@16
|
118 * This constructor creates a stream socket object to hold an existing native
|
Chris@16
|
119 * socket.
|
Chris@16
|
120 *
|
Chris@16
|
121 * @param io_service The io_service object that the stream socket will use to
|
Chris@16
|
122 * dispatch handlers for any asynchronous operations performed on the socket.
|
Chris@16
|
123 *
|
Chris@16
|
124 * @param protocol An object specifying protocol parameters to be used.
|
Chris@16
|
125 *
|
Chris@16
|
126 * @param native_socket The new underlying socket implementation.
|
Chris@16
|
127 *
|
Chris@16
|
128 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
129 */
|
Chris@16
|
130 basic_stream_socket(boost::asio::io_service& io_service,
|
Chris@16
|
131 const protocol_type& protocol, const native_handle_type& native_socket)
|
Chris@16
|
132 : basic_socket<Protocol, StreamSocketService>(
|
Chris@16
|
133 io_service, protocol, native_socket)
|
Chris@16
|
134 {
|
Chris@16
|
135 }
|
Chris@16
|
136
|
Chris@16
|
137 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
138 /// Move-construct a basic_stream_socket from another.
|
Chris@16
|
139 /**
|
Chris@16
|
140 * This constructor moves a stream socket from one object to another.
|
Chris@16
|
141 *
|
Chris@16
|
142 * @param other The other basic_stream_socket object from which the move
|
Chris@16
|
143 * will occur.
|
Chris@16
|
144 *
|
Chris@16
|
145 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
146 * constructed using the @c basic_stream_socket(io_service&) constructor.
|
Chris@16
|
147 */
|
Chris@16
|
148 basic_stream_socket(basic_stream_socket&& other)
|
Chris@16
|
149 : basic_socket<Protocol, StreamSocketService>(
|
Chris@16
|
150 BOOST_ASIO_MOVE_CAST(basic_stream_socket)(other))
|
Chris@16
|
151 {
|
Chris@16
|
152 }
|
Chris@16
|
153
|
Chris@16
|
154 /// Move-assign a basic_stream_socket from another.
|
Chris@16
|
155 /**
|
Chris@16
|
156 * This assignment operator moves a stream socket from one object to another.
|
Chris@16
|
157 *
|
Chris@16
|
158 * @param other The other basic_stream_socket object from which the move
|
Chris@16
|
159 * will occur.
|
Chris@16
|
160 *
|
Chris@16
|
161 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
162 * constructed using the @c basic_stream_socket(io_service&) constructor.
|
Chris@16
|
163 */
|
Chris@16
|
164 basic_stream_socket& operator=(basic_stream_socket&& other)
|
Chris@16
|
165 {
|
Chris@16
|
166 basic_socket<Protocol, StreamSocketService>::operator=(
|
Chris@16
|
167 BOOST_ASIO_MOVE_CAST(basic_stream_socket)(other));
|
Chris@16
|
168 return *this;
|
Chris@16
|
169 }
|
Chris@16
|
170
|
Chris@16
|
171 /// Move-construct a basic_stream_socket from a socket of another protocol
|
Chris@16
|
172 /// type.
|
Chris@16
|
173 /**
|
Chris@16
|
174 * This constructor moves a stream socket from one object to another.
|
Chris@16
|
175 *
|
Chris@16
|
176 * @param other The other basic_stream_socket object from which the move
|
Chris@16
|
177 * will occur.
|
Chris@16
|
178 *
|
Chris@16
|
179 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
180 * constructed using the @c basic_stream_socket(io_service&) constructor.
|
Chris@16
|
181 */
|
Chris@16
|
182 template <typename Protocol1, typename StreamSocketService1>
|
Chris@16
|
183 basic_stream_socket(
|
Chris@16
|
184 basic_stream_socket<Protocol1, StreamSocketService1>&& other,
|
Chris@16
|
185 typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
|
Chris@16
|
186 : basic_socket<Protocol, StreamSocketService>(
|
Chris@16
|
187 BOOST_ASIO_MOVE_CAST2(basic_stream_socket<
|
Chris@16
|
188 Protocol1, StreamSocketService1>)(other))
|
Chris@16
|
189 {
|
Chris@16
|
190 }
|
Chris@16
|
191
|
Chris@16
|
192 /// Move-assign a basic_stream_socket from a socket of another protocol type.
|
Chris@16
|
193 /**
|
Chris@16
|
194 * This assignment operator moves a stream socket from one object to another.
|
Chris@16
|
195 *
|
Chris@16
|
196 * @param other The other basic_stream_socket object from which the move
|
Chris@16
|
197 * will occur.
|
Chris@16
|
198 *
|
Chris@16
|
199 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
200 * constructed using the @c basic_stream_socket(io_service&) constructor.
|
Chris@16
|
201 */
|
Chris@16
|
202 template <typename Protocol1, typename StreamSocketService1>
|
Chris@16
|
203 typename enable_if<is_convertible<Protocol1, Protocol>::value,
|
Chris@16
|
204 basic_stream_socket>::type& operator=(
|
Chris@16
|
205 basic_stream_socket<Protocol1, StreamSocketService1>&& other)
|
Chris@16
|
206 {
|
Chris@16
|
207 basic_socket<Protocol, StreamSocketService>::operator=(
|
Chris@16
|
208 BOOST_ASIO_MOVE_CAST2(basic_stream_socket<
|
Chris@16
|
209 Protocol1, StreamSocketService1>)(other));
|
Chris@16
|
210 return *this;
|
Chris@16
|
211 }
|
Chris@16
|
212 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
213
|
Chris@16
|
214 /// Send some data on the socket.
|
Chris@16
|
215 /**
|
Chris@16
|
216 * This function is used to send data on the stream socket. The function
|
Chris@16
|
217 * call will block until one or more bytes of the data has been sent
|
Chris@16
|
218 * successfully, or an until error occurs.
|
Chris@16
|
219 *
|
Chris@16
|
220 * @param buffers One or more data buffers to be sent on the socket.
|
Chris@16
|
221 *
|
Chris@16
|
222 * @returns The number of bytes sent.
|
Chris@16
|
223 *
|
Chris@16
|
224 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
225 *
|
Chris@16
|
226 * @note The send operation may not transmit all of the data to the peer.
|
Chris@16
|
227 * Consider using the @ref write function if you need to ensure that all data
|
Chris@16
|
228 * is written before the blocking operation completes.
|
Chris@16
|
229 *
|
Chris@16
|
230 * @par Example
|
Chris@16
|
231 * To send a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
232 * @code
|
Chris@16
|
233 * socket.send(boost::asio::buffer(data, size));
|
Chris@16
|
234 * @endcode
|
Chris@16
|
235 * See the @ref buffer documentation for information on sending multiple
|
Chris@16
|
236 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
237 * std::vector.
|
Chris@16
|
238 */
|
Chris@16
|
239 template <typename ConstBufferSequence>
|
Chris@16
|
240 std::size_t send(const ConstBufferSequence& buffers)
|
Chris@16
|
241 {
|
Chris@16
|
242 boost::system::error_code ec;
|
Chris@16
|
243 std::size_t s = this->get_service().send(
|
Chris@16
|
244 this->get_implementation(), buffers, 0, ec);
|
Chris@16
|
245 boost::asio::detail::throw_error(ec, "send");
|
Chris@16
|
246 return s;
|
Chris@16
|
247 }
|
Chris@16
|
248
|
Chris@16
|
249 /// Send some data on the socket.
|
Chris@16
|
250 /**
|
Chris@16
|
251 * This function is used to send data on the stream socket. The function
|
Chris@16
|
252 * call will block until one or more bytes of the data has been sent
|
Chris@16
|
253 * successfully, or an until error occurs.
|
Chris@16
|
254 *
|
Chris@16
|
255 * @param buffers One or more data buffers to be sent on the socket.
|
Chris@16
|
256 *
|
Chris@16
|
257 * @param flags Flags specifying how the send call is to be made.
|
Chris@16
|
258 *
|
Chris@16
|
259 * @returns The number of bytes sent.
|
Chris@16
|
260 *
|
Chris@16
|
261 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
262 *
|
Chris@16
|
263 * @note The send operation may not transmit all of the data to the peer.
|
Chris@16
|
264 * Consider using the @ref write function if you need to ensure that all data
|
Chris@16
|
265 * is written before the blocking operation completes.
|
Chris@16
|
266 *
|
Chris@16
|
267 * @par Example
|
Chris@16
|
268 * To send a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
269 * @code
|
Chris@16
|
270 * socket.send(boost::asio::buffer(data, size), 0);
|
Chris@16
|
271 * @endcode
|
Chris@16
|
272 * See the @ref buffer documentation for information on sending multiple
|
Chris@16
|
273 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
274 * std::vector.
|
Chris@16
|
275 */
|
Chris@16
|
276 template <typename ConstBufferSequence>
|
Chris@16
|
277 std::size_t send(const ConstBufferSequence& buffers,
|
Chris@16
|
278 socket_base::message_flags flags)
|
Chris@16
|
279 {
|
Chris@16
|
280 boost::system::error_code ec;
|
Chris@16
|
281 std::size_t s = this->get_service().send(
|
Chris@16
|
282 this->get_implementation(), buffers, flags, ec);
|
Chris@16
|
283 boost::asio::detail::throw_error(ec, "send");
|
Chris@16
|
284 return s;
|
Chris@16
|
285 }
|
Chris@16
|
286
|
Chris@16
|
287 /// Send some data on the socket.
|
Chris@16
|
288 /**
|
Chris@16
|
289 * This function is used to send data on the stream socket. The function
|
Chris@16
|
290 * call will block until one or more bytes of the data has been sent
|
Chris@16
|
291 * successfully, or an until error occurs.
|
Chris@16
|
292 *
|
Chris@16
|
293 * @param buffers One or more data buffers to be sent on the socket.
|
Chris@16
|
294 *
|
Chris@16
|
295 * @param flags Flags specifying how the send call is to be made.
|
Chris@16
|
296 *
|
Chris@16
|
297 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
298 *
|
Chris@16
|
299 * @returns The number of bytes sent. Returns 0 if an error occurred.
|
Chris@16
|
300 *
|
Chris@16
|
301 * @note The send operation may not transmit all of the data to the peer.
|
Chris@16
|
302 * Consider using the @ref write function if you need to ensure that all data
|
Chris@16
|
303 * is written before the blocking operation completes.
|
Chris@16
|
304 */
|
Chris@16
|
305 template <typename ConstBufferSequence>
|
Chris@16
|
306 std::size_t send(const ConstBufferSequence& buffers,
|
Chris@16
|
307 socket_base::message_flags flags, boost::system::error_code& ec)
|
Chris@16
|
308 {
|
Chris@16
|
309 return this->get_service().send(
|
Chris@16
|
310 this->get_implementation(), buffers, flags, ec);
|
Chris@16
|
311 }
|
Chris@16
|
312
|
Chris@16
|
313 /// Start an asynchronous send.
|
Chris@16
|
314 /**
|
Chris@16
|
315 * This function is used to asynchronously send data on the stream socket.
|
Chris@16
|
316 * The function call always returns immediately.
|
Chris@16
|
317 *
|
Chris@16
|
318 * @param buffers One or more data buffers to be sent on the socket. Although
|
Chris@16
|
319 * the buffers object may be copied as necessary, ownership of the underlying
|
Chris@16
|
320 * memory blocks is retained by the caller, which must guarantee that they
|
Chris@16
|
321 * remain valid until the handler is called.
|
Chris@16
|
322 *
|
Chris@16
|
323 * @param handler The handler to be called when the send operation completes.
|
Chris@16
|
324 * Copies will be made of the handler as required. The function signature of
|
Chris@16
|
325 * the handler must be:
|
Chris@16
|
326 * @code void handler(
|
Chris@16
|
327 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
328 * std::size_t bytes_transferred // Number of bytes sent.
|
Chris@16
|
329 * ); @endcode
|
Chris@16
|
330 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
331 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
332 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
333 * boost::asio::io_service::post().
|
Chris@16
|
334 *
|
Chris@16
|
335 * @note The send operation may not transmit all of the data to the peer.
|
Chris@16
|
336 * Consider using the @ref async_write function if you need to ensure that all
|
Chris@16
|
337 * data is written before the asynchronous operation completes.
|
Chris@16
|
338 *
|
Chris@16
|
339 * @par Example
|
Chris@16
|
340 * To send a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
341 * @code
|
Chris@16
|
342 * socket.async_send(boost::asio::buffer(data, size), handler);
|
Chris@16
|
343 * @endcode
|
Chris@16
|
344 * See the @ref buffer documentation for information on sending multiple
|
Chris@16
|
345 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
346 * std::vector.
|
Chris@16
|
347 */
|
Chris@16
|
348 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
349 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
350 void (boost::system::error_code, std::size_t))
|
Chris@16
|
351 async_send(const ConstBufferSequence& buffers,
|
Chris@16
|
352 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
|
Chris@16
|
353 {
|
Chris@16
|
354 // If you get an error on the following line it means that your handler does
|
Chris@16
|
355 // not meet the documented type requirements for a WriteHandler.
|
Chris@16
|
356 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
Chris@16
|
357
|
Chris@16
|
358 return this->get_service().async_send(
|
Chris@16
|
359 this->get_implementation(), buffers, 0,
|
Chris@16
|
360 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
|
Chris@16
|
361 }
|
Chris@16
|
362
|
Chris@16
|
363 /// Start an asynchronous send.
|
Chris@16
|
364 /**
|
Chris@16
|
365 * This function is used to asynchronously send data on the stream socket.
|
Chris@16
|
366 * The function call always returns immediately.
|
Chris@16
|
367 *
|
Chris@16
|
368 * @param buffers One or more data buffers to be sent on the socket. Although
|
Chris@16
|
369 * the buffers object may be copied as necessary, ownership of the underlying
|
Chris@16
|
370 * memory blocks is retained by the caller, which must guarantee that they
|
Chris@16
|
371 * remain valid until the handler is called.
|
Chris@16
|
372 *
|
Chris@16
|
373 * @param flags Flags specifying how the send call is to be made.
|
Chris@16
|
374 *
|
Chris@16
|
375 * @param handler The handler to be called when the send operation completes.
|
Chris@16
|
376 * Copies will be made of the handler as required. The function signature of
|
Chris@16
|
377 * the handler must be:
|
Chris@16
|
378 * @code void handler(
|
Chris@16
|
379 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
380 * std::size_t bytes_transferred // Number of bytes sent.
|
Chris@16
|
381 * ); @endcode
|
Chris@16
|
382 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
383 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
384 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
385 * boost::asio::io_service::post().
|
Chris@16
|
386 *
|
Chris@16
|
387 * @note The send operation may not transmit all of the data to the peer.
|
Chris@16
|
388 * Consider using the @ref async_write function if you need to ensure that all
|
Chris@16
|
389 * data is written before the asynchronous operation completes.
|
Chris@16
|
390 *
|
Chris@16
|
391 * @par Example
|
Chris@16
|
392 * To send a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
393 * @code
|
Chris@16
|
394 * socket.async_send(boost::asio::buffer(data, size), 0, handler);
|
Chris@16
|
395 * @endcode
|
Chris@16
|
396 * See the @ref buffer documentation for information on sending multiple
|
Chris@16
|
397 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
398 * std::vector.
|
Chris@16
|
399 */
|
Chris@16
|
400 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
401 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
402 void (boost::system::error_code, std::size_t))
|
Chris@16
|
403 async_send(const ConstBufferSequence& buffers,
|
Chris@16
|
404 socket_base::message_flags flags,
|
Chris@16
|
405 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
|
Chris@16
|
406 {
|
Chris@16
|
407 // If you get an error on the following line it means that your handler does
|
Chris@16
|
408 // not meet the documented type requirements for a WriteHandler.
|
Chris@16
|
409 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
Chris@16
|
410
|
Chris@16
|
411 return this->get_service().async_send(
|
Chris@16
|
412 this->get_implementation(), buffers, flags,
|
Chris@16
|
413 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
|
Chris@16
|
414 }
|
Chris@16
|
415
|
Chris@16
|
416 /// Receive some data on the socket.
|
Chris@16
|
417 /**
|
Chris@16
|
418 * This function is used to receive data on the stream socket. The function
|
Chris@16
|
419 * call will block until one or more bytes of data has been received
|
Chris@16
|
420 * successfully, or until an error occurs.
|
Chris@16
|
421 *
|
Chris@16
|
422 * @param buffers One or more buffers into which the data will be received.
|
Chris@16
|
423 *
|
Chris@16
|
424 * @returns The number of bytes received.
|
Chris@16
|
425 *
|
Chris@16
|
426 * @throws boost::system::system_error Thrown on failure. An error code of
|
Chris@16
|
427 * boost::asio::error::eof indicates that the connection was closed by the
|
Chris@16
|
428 * peer.
|
Chris@16
|
429 *
|
Chris@16
|
430 * @note The receive operation may not receive all of the requested number of
|
Chris@16
|
431 * bytes. Consider using the @ref read function if you need to ensure that the
|
Chris@16
|
432 * requested amount of data is read before the blocking operation completes.
|
Chris@16
|
433 *
|
Chris@16
|
434 * @par Example
|
Chris@16
|
435 * To receive into a single data buffer use the @ref buffer function as
|
Chris@16
|
436 * follows:
|
Chris@16
|
437 * @code
|
Chris@16
|
438 * socket.receive(boost::asio::buffer(data, size));
|
Chris@16
|
439 * @endcode
|
Chris@16
|
440 * See the @ref buffer documentation for information on receiving into
|
Chris@16
|
441 * multiple buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
442 * std::vector.
|
Chris@16
|
443 */
|
Chris@16
|
444 template <typename MutableBufferSequence>
|
Chris@16
|
445 std::size_t receive(const MutableBufferSequence& buffers)
|
Chris@16
|
446 {
|
Chris@16
|
447 boost::system::error_code ec;
|
Chris@16
|
448 std::size_t s = this->get_service().receive(
|
Chris@16
|
449 this->get_implementation(), buffers, 0, ec);
|
Chris@16
|
450 boost::asio::detail::throw_error(ec, "receive");
|
Chris@16
|
451 return s;
|
Chris@16
|
452 }
|
Chris@16
|
453
|
Chris@16
|
454 /// Receive some data on the socket.
|
Chris@16
|
455 /**
|
Chris@16
|
456 * This function is used to receive data on the stream socket. The function
|
Chris@16
|
457 * call will block until one or more bytes of data has been received
|
Chris@16
|
458 * successfully, or until an error occurs.
|
Chris@16
|
459 *
|
Chris@16
|
460 * @param buffers One or more buffers into which the data will be received.
|
Chris@16
|
461 *
|
Chris@16
|
462 * @param flags Flags specifying how the receive call is to be made.
|
Chris@16
|
463 *
|
Chris@16
|
464 * @returns The number of bytes received.
|
Chris@16
|
465 *
|
Chris@16
|
466 * @throws boost::system::system_error Thrown on failure. An error code of
|
Chris@16
|
467 * boost::asio::error::eof indicates that the connection was closed by the
|
Chris@16
|
468 * peer.
|
Chris@16
|
469 *
|
Chris@16
|
470 * @note The receive operation may not receive all of the requested number of
|
Chris@16
|
471 * bytes. Consider using the @ref read function if you need to ensure that the
|
Chris@16
|
472 * requested amount of data is read before the blocking operation completes.
|
Chris@16
|
473 *
|
Chris@16
|
474 * @par Example
|
Chris@16
|
475 * To receive into a single data buffer use the @ref buffer function as
|
Chris@16
|
476 * follows:
|
Chris@16
|
477 * @code
|
Chris@16
|
478 * socket.receive(boost::asio::buffer(data, size), 0);
|
Chris@16
|
479 * @endcode
|
Chris@16
|
480 * See the @ref buffer documentation for information on receiving into
|
Chris@16
|
481 * multiple buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
482 * std::vector.
|
Chris@16
|
483 */
|
Chris@16
|
484 template <typename MutableBufferSequence>
|
Chris@16
|
485 std::size_t receive(const MutableBufferSequence& buffers,
|
Chris@16
|
486 socket_base::message_flags flags)
|
Chris@16
|
487 {
|
Chris@16
|
488 boost::system::error_code ec;
|
Chris@16
|
489 std::size_t s = this->get_service().receive(
|
Chris@16
|
490 this->get_implementation(), buffers, flags, ec);
|
Chris@16
|
491 boost::asio::detail::throw_error(ec, "receive");
|
Chris@16
|
492 return s;
|
Chris@16
|
493 }
|
Chris@16
|
494
|
Chris@16
|
495 /// Receive some data on a connected socket.
|
Chris@16
|
496 /**
|
Chris@16
|
497 * This function is used to receive data on the stream socket. The function
|
Chris@16
|
498 * call will block until one or more bytes of data has been received
|
Chris@16
|
499 * successfully, or until an error occurs.
|
Chris@16
|
500 *
|
Chris@16
|
501 * @param buffers One or more buffers into which the data will be received.
|
Chris@16
|
502 *
|
Chris@16
|
503 * @param flags Flags specifying how the receive call is to be made.
|
Chris@16
|
504 *
|
Chris@16
|
505 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
506 *
|
Chris@16
|
507 * @returns The number of bytes received. Returns 0 if an error occurred.
|
Chris@16
|
508 *
|
Chris@16
|
509 * @note The receive operation may not receive all of the requested number of
|
Chris@16
|
510 * bytes. Consider using the @ref read function if you need to ensure that the
|
Chris@16
|
511 * requested amount of data is read before the blocking operation completes.
|
Chris@16
|
512 */
|
Chris@16
|
513 template <typename MutableBufferSequence>
|
Chris@16
|
514 std::size_t receive(const MutableBufferSequence& buffers,
|
Chris@16
|
515 socket_base::message_flags flags, boost::system::error_code& ec)
|
Chris@16
|
516 {
|
Chris@16
|
517 return this->get_service().receive(
|
Chris@16
|
518 this->get_implementation(), buffers, flags, ec);
|
Chris@16
|
519 }
|
Chris@16
|
520
|
Chris@16
|
521 /// Start an asynchronous receive.
|
Chris@16
|
522 /**
|
Chris@16
|
523 * This function is used to asynchronously receive data from the stream
|
Chris@16
|
524 * socket. The function call always returns immediately.
|
Chris@16
|
525 *
|
Chris@16
|
526 * @param buffers One or more buffers into which the data will be received.
|
Chris@16
|
527 * Although the buffers object may be copied as necessary, ownership of the
|
Chris@16
|
528 * underlying memory blocks is retained by the caller, which must guarantee
|
Chris@16
|
529 * that they remain valid until the handler is called.
|
Chris@16
|
530 *
|
Chris@16
|
531 * @param handler The handler to be called when the receive operation
|
Chris@16
|
532 * completes. Copies will be made of the handler as required. The function
|
Chris@16
|
533 * signature of the handler must be:
|
Chris@16
|
534 * @code void handler(
|
Chris@16
|
535 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
536 * std::size_t bytes_transferred // Number of bytes received.
|
Chris@16
|
537 * ); @endcode
|
Chris@16
|
538 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
539 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
540 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
541 * boost::asio::io_service::post().
|
Chris@16
|
542 *
|
Chris@16
|
543 * @note The receive operation may not receive all of the requested number of
|
Chris@16
|
544 * bytes. Consider using the @ref async_read function if you need to ensure
|
Chris@16
|
545 * that the requested amount of data is received before the asynchronous
|
Chris@16
|
546 * operation completes.
|
Chris@16
|
547 *
|
Chris@16
|
548 * @par Example
|
Chris@16
|
549 * To receive into a single data buffer use the @ref buffer function as
|
Chris@16
|
550 * follows:
|
Chris@16
|
551 * @code
|
Chris@16
|
552 * socket.async_receive(boost::asio::buffer(data, size), handler);
|
Chris@16
|
553 * @endcode
|
Chris@16
|
554 * See the @ref buffer documentation for information on receiving into
|
Chris@16
|
555 * multiple buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
556 * std::vector.
|
Chris@16
|
557 */
|
Chris@16
|
558 template <typename MutableBufferSequence, typename ReadHandler>
|
Chris@16
|
559 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
560 void (boost::system::error_code, std::size_t))
|
Chris@16
|
561 async_receive(const MutableBufferSequence& buffers,
|
Chris@16
|
562 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
|
Chris@16
|
563 {
|
Chris@16
|
564 // If you get an error on the following line it means that your handler does
|
Chris@16
|
565 // not meet the documented type requirements for a ReadHandler.
|
Chris@16
|
566 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
|
Chris@16
|
567
|
Chris@16
|
568 return this->get_service().async_receive(this->get_implementation(),
|
Chris@16
|
569 buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
|
Chris@16
|
570 }
|
Chris@16
|
571
|
Chris@16
|
572 /// Start an asynchronous receive.
|
Chris@16
|
573 /**
|
Chris@16
|
574 * This function is used to asynchronously receive data from the stream
|
Chris@16
|
575 * socket. The function call always returns immediately.
|
Chris@16
|
576 *
|
Chris@16
|
577 * @param buffers One or more buffers into which the data will be received.
|
Chris@16
|
578 * Although the buffers object may be copied as necessary, ownership of the
|
Chris@16
|
579 * underlying memory blocks is retained by the caller, which must guarantee
|
Chris@16
|
580 * that they remain valid until the handler is called.
|
Chris@16
|
581 *
|
Chris@16
|
582 * @param flags Flags specifying how the receive call is to be made.
|
Chris@16
|
583 *
|
Chris@16
|
584 * @param handler The handler to be called when the receive operation
|
Chris@16
|
585 * completes. Copies will be made of the handler as required. The function
|
Chris@16
|
586 * signature of the handler must be:
|
Chris@16
|
587 * @code void handler(
|
Chris@16
|
588 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
589 * std::size_t bytes_transferred // Number of bytes received.
|
Chris@16
|
590 * ); @endcode
|
Chris@16
|
591 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
592 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
593 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
594 * boost::asio::io_service::post().
|
Chris@16
|
595 *
|
Chris@16
|
596 * @note The receive operation may not receive all of the requested number of
|
Chris@16
|
597 * bytes. Consider using the @ref async_read function if you need to ensure
|
Chris@16
|
598 * that the requested amount of data is received before the asynchronous
|
Chris@16
|
599 * operation completes.
|
Chris@16
|
600 *
|
Chris@16
|
601 * @par Example
|
Chris@16
|
602 * To receive into a single data buffer use the @ref buffer function as
|
Chris@16
|
603 * follows:
|
Chris@16
|
604 * @code
|
Chris@16
|
605 * socket.async_receive(boost::asio::buffer(data, size), 0, handler);
|
Chris@16
|
606 * @endcode
|
Chris@16
|
607 * See the @ref buffer documentation for information on receiving into
|
Chris@16
|
608 * multiple buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
609 * std::vector.
|
Chris@16
|
610 */
|
Chris@16
|
611 template <typename MutableBufferSequence, typename ReadHandler>
|
Chris@16
|
612 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
613 void (boost::system::error_code, std::size_t))
|
Chris@16
|
614 async_receive(const MutableBufferSequence& buffers,
|
Chris@16
|
615 socket_base::message_flags flags,
|
Chris@16
|
616 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
|
Chris@16
|
617 {
|
Chris@16
|
618 // If you get an error on the following line it means that your handler does
|
Chris@16
|
619 // not meet the documented type requirements for a ReadHandler.
|
Chris@16
|
620 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
|
Chris@16
|
621
|
Chris@16
|
622 return this->get_service().async_receive(this->get_implementation(),
|
Chris@16
|
623 buffers, flags, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
|
Chris@16
|
624 }
|
Chris@16
|
625
|
Chris@16
|
626 /// Write some data to the socket.
|
Chris@16
|
627 /**
|
Chris@16
|
628 * This function is used to write data to the stream socket. The function call
|
Chris@16
|
629 * will block until one or more bytes of the data has been written
|
Chris@16
|
630 * successfully, or until an error occurs.
|
Chris@16
|
631 *
|
Chris@16
|
632 * @param buffers One or more data buffers to be written to the socket.
|
Chris@16
|
633 *
|
Chris@16
|
634 * @returns The number of bytes written.
|
Chris@16
|
635 *
|
Chris@16
|
636 * @throws boost::system::system_error Thrown on failure. An error code of
|
Chris@16
|
637 * boost::asio::error::eof indicates that the connection was closed by the
|
Chris@16
|
638 * peer.
|
Chris@16
|
639 *
|
Chris@16
|
640 * @note The write_some operation may not transmit all of the data to the
|
Chris@16
|
641 * peer. Consider using the @ref write function if you need to ensure that
|
Chris@16
|
642 * all data is written before the blocking operation completes.
|
Chris@16
|
643 *
|
Chris@16
|
644 * @par Example
|
Chris@16
|
645 * To write a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
646 * @code
|
Chris@16
|
647 * socket.write_some(boost::asio::buffer(data, size));
|
Chris@16
|
648 * @endcode
|
Chris@16
|
649 * See the @ref buffer documentation for information on writing multiple
|
Chris@16
|
650 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
651 * std::vector.
|
Chris@16
|
652 */
|
Chris@16
|
653 template <typename ConstBufferSequence>
|
Chris@16
|
654 std::size_t write_some(const ConstBufferSequence& buffers)
|
Chris@16
|
655 {
|
Chris@16
|
656 boost::system::error_code ec;
|
Chris@16
|
657 std::size_t s = this->get_service().send(
|
Chris@16
|
658 this->get_implementation(), buffers, 0, ec);
|
Chris@16
|
659 boost::asio::detail::throw_error(ec, "write_some");
|
Chris@16
|
660 return s;
|
Chris@16
|
661 }
|
Chris@16
|
662
|
Chris@16
|
663 /// Write some data to the socket.
|
Chris@16
|
664 /**
|
Chris@16
|
665 * This function is used to write data to the stream socket. The function call
|
Chris@16
|
666 * will block until one or more bytes of the data has been written
|
Chris@16
|
667 * successfully, or until an error occurs.
|
Chris@16
|
668 *
|
Chris@16
|
669 * @param buffers One or more data buffers to be written to the socket.
|
Chris@16
|
670 *
|
Chris@16
|
671 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
672 *
|
Chris@16
|
673 * @returns The number of bytes written. Returns 0 if an error occurred.
|
Chris@16
|
674 *
|
Chris@16
|
675 * @note The write_some operation may not transmit all of the data to the
|
Chris@16
|
676 * peer. Consider using the @ref write function if you need to ensure that
|
Chris@16
|
677 * all data is written before the blocking operation completes.
|
Chris@16
|
678 */
|
Chris@16
|
679 template <typename ConstBufferSequence>
|
Chris@16
|
680 std::size_t write_some(const ConstBufferSequence& buffers,
|
Chris@16
|
681 boost::system::error_code& ec)
|
Chris@16
|
682 {
|
Chris@16
|
683 return this->get_service().send(this->get_implementation(), buffers, 0, ec);
|
Chris@16
|
684 }
|
Chris@16
|
685
|
Chris@16
|
686 /// Start an asynchronous write.
|
Chris@16
|
687 /**
|
Chris@16
|
688 * This function is used to asynchronously write data to the stream socket.
|
Chris@16
|
689 * The function call always returns immediately.
|
Chris@16
|
690 *
|
Chris@16
|
691 * @param buffers One or more data buffers to be written to the socket.
|
Chris@16
|
692 * Although the buffers object may be copied as necessary, ownership of the
|
Chris@16
|
693 * underlying memory blocks is retained by the caller, which must guarantee
|
Chris@16
|
694 * that they remain valid until the handler is called.
|
Chris@16
|
695 *
|
Chris@16
|
696 * @param handler The handler to be called when the write operation completes.
|
Chris@16
|
697 * Copies will be made of the handler as required. The function signature of
|
Chris@16
|
698 * the handler must be:
|
Chris@16
|
699 * @code void handler(
|
Chris@16
|
700 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
701 * std::size_t bytes_transferred // Number of bytes written.
|
Chris@16
|
702 * ); @endcode
|
Chris@16
|
703 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
704 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
705 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
706 * boost::asio::io_service::post().
|
Chris@16
|
707 *
|
Chris@16
|
708 * @note The write operation may not transmit all of the data to the peer.
|
Chris@16
|
709 * Consider using the @ref async_write function if you need to ensure that all
|
Chris@16
|
710 * data is written before the asynchronous operation completes.
|
Chris@16
|
711 *
|
Chris@16
|
712 * @par Example
|
Chris@16
|
713 * To write a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
714 * @code
|
Chris@16
|
715 * socket.async_write_some(boost::asio::buffer(data, size), handler);
|
Chris@16
|
716 * @endcode
|
Chris@16
|
717 * See the @ref buffer documentation for information on writing multiple
|
Chris@16
|
718 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
719 * std::vector.
|
Chris@16
|
720 */
|
Chris@16
|
721 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
722 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
723 void (boost::system::error_code, std::size_t))
|
Chris@16
|
724 async_write_some(const ConstBufferSequence& buffers,
|
Chris@16
|
725 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
|
Chris@16
|
726 {
|
Chris@16
|
727 // If you get an error on the following line it means that your handler does
|
Chris@16
|
728 // not meet the documented type requirements for a WriteHandler.
|
Chris@16
|
729 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
Chris@16
|
730
|
Chris@16
|
731 return this->get_service().async_send(this->get_implementation(),
|
Chris@16
|
732 buffers, 0, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
|
Chris@16
|
733 }
|
Chris@16
|
734
|
Chris@16
|
735 /// Read some data from the socket.
|
Chris@16
|
736 /**
|
Chris@16
|
737 * This function is used to read data from the stream socket. The function
|
Chris@16
|
738 * call will block until one or more bytes of data has been read successfully,
|
Chris@16
|
739 * or until an error occurs.
|
Chris@16
|
740 *
|
Chris@16
|
741 * @param buffers One or more buffers into which the data will be read.
|
Chris@16
|
742 *
|
Chris@16
|
743 * @returns The number of bytes read.
|
Chris@16
|
744 *
|
Chris@16
|
745 * @throws boost::system::system_error Thrown on failure. An error code of
|
Chris@16
|
746 * boost::asio::error::eof indicates that the connection was closed by the
|
Chris@16
|
747 * peer.
|
Chris@16
|
748 *
|
Chris@16
|
749 * @note The read_some operation may not read all of the requested number of
|
Chris@16
|
750 * bytes. Consider using the @ref read function if you need to ensure that
|
Chris@16
|
751 * the requested amount of data is read before the blocking operation
|
Chris@16
|
752 * completes.
|
Chris@16
|
753 *
|
Chris@16
|
754 * @par Example
|
Chris@16
|
755 * To read into a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
756 * @code
|
Chris@16
|
757 * socket.read_some(boost::asio::buffer(data, size));
|
Chris@16
|
758 * @endcode
|
Chris@16
|
759 * See the @ref buffer documentation for information on reading into multiple
|
Chris@16
|
760 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
761 * std::vector.
|
Chris@16
|
762 */
|
Chris@16
|
763 template <typename MutableBufferSequence>
|
Chris@16
|
764 std::size_t read_some(const MutableBufferSequence& buffers)
|
Chris@16
|
765 {
|
Chris@16
|
766 boost::system::error_code ec;
|
Chris@16
|
767 std::size_t s = this->get_service().receive(
|
Chris@16
|
768 this->get_implementation(), buffers, 0, ec);
|
Chris@16
|
769 boost::asio::detail::throw_error(ec, "read_some");
|
Chris@16
|
770 return s;
|
Chris@16
|
771 }
|
Chris@16
|
772
|
Chris@16
|
773 /// Read some data from the socket.
|
Chris@16
|
774 /**
|
Chris@16
|
775 * This function is used to read data from the stream socket. The function
|
Chris@16
|
776 * call will block until one or more bytes of data has been read successfully,
|
Chris@16
|
777 * or until an error occurs.
|
Chris@16
|
778 *
|
Chris@16
|
779 * @param buffers One or more buffers into which the data will be read.
|
Chris@16
|
780 *
|
Chris@16
|
781 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
782 *
|
Chris@16
|
783 * @returns The number of bytes read. Returns 0 if an error occurred.
|
Chris@16
|
784 *
|
Chris@16
|
785 * @note The read_some operation may not read all of the requested number of
|
Chris@16
|
786 * bytes. Consider using the @ref read function if you need to ensure that
|
Chris@16
|
787 * the requested amount of data is read before the blocking operation
|
Chris@16
|
788 * completes.
|
Chris@16
|
789 */
|
Chris@16
|
790 template <typename MutableBufferSequence>
|
Chris@16
|
791 std::size_t read_some(const MutableBufferSequence& buffers,
|
Chris@16
|
792 boost::system::error_code& ec)
|
Chris@16
|
793 {
|
Chris@16
|
794 return this->get_service().receive(
|
Chris@16
|
795 this->get_implementation(), buffers, 0, ec);
|
Chris@16
|
796 }
|
Chris@16
|
797
|
Chris@16
|
798 /// Start an asynchronous read.
|
Chris@16
|
799 /**
|
Chris@16
|
800 * This function is used to asynchronously read data from the stream socket.
|
Chris@16
|
801 * The function call always returns immediately.
|
Chris@16
|
802 *
|
Chris@16
|
803 * @param buffers One or more buffers into which the data will be read.
|
Chris@16
|
804 * Although the buffers object may be copied as necessary, ownership of the
|
Chris@16
|
805 * underlying memory blocks is retained by the caller, which must guarantee
|
Chris@16
|
806 * that they remain valid until the handler is called.
|
Chris@16
|
807 *
|
Chris@16
|
808 * @param handler The handler to be called when the read operation completes.
|
Chris@16
|
809 * Copies will be made of the handler as required. The function signature of
|
Chris@16
|
810 * the handler must be:
|
Chris@16
|
811 * @code void handler(
|
Chris@16
|
812 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
813 * std::size_t bytes_transferred // Number of bytes read.
|
Chris@16
|
814 * ); @endcode
|
Chris@16
|
815 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
816 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
817 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
818 * boost::asio::io_service::post().
|
Chris@16
|
819 *
|
Chris@16
|
820 * @note The read operation may not read all of the requested number of bytes.
|
Chris@16
|
821 * Consider using the @ref async_read function if you need to ensure that the
|
Chris@16
|
822 * requested amount of data is read before the asynchronous operation
|
Chris@16
|
823 * completes.
|
Chris@16
|
824 *
|
Chris@16
|
825 * @par Example
|
Chris@16
|
826 * To read into a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
827 * @code
|
Chris@16
|
828 * socket.async_read_some(boost::asio::buffer(data, size), handler);
|
Chris@16
|
829 * @endcode
|
Chris@16
|
830 * See the @ref buffer documentation for information on reading into multiple
|
Chris@16
|
831 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
832 * std::vector.
|
Chris@16
|
833 */
|
Chris@16
|
834 template <typename MutableBufferSequence, typename ReadHandler>
|
Chris@16
|
835 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
836 void (boost::system::error_code, std::size_t))
|
Chris@16
|
837 async_read_some(const MutableBufferSequence& buffers,
|
Chris@16
|
838 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
|
Chris@16
|
839 {
|
Chris@16
|
840 // If you get an error on the following line it means that your handler does
|
Chris@16
|
841 // not meet the documented type requirements for a ReadHandler.
|
Chris@16
|
842 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
|
Chris@16
|
843
|
Chris@16
|
844 return this->get_service().async_receive(this->get_implementation(),
|
Chris@16
|
845 buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
|
Chris@16
|
846 }
|
Chris@16
|
847 };
|
Chris@16
|
848
|
Chris@16
|
849 } // namespace asio
|
Chris@16
|
850 } // namespace boost
|
Chris@16
|
851
|
Chris@16
|
852 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
853
|
Chris@16
|
854 #endif // BOOST_ASIO_BASIC_STREAM_SOCKET_HPP
|