Chris@16
|
1 //
|
Chris@16
|
2 // basic_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_SOCKET_HPP
|
Chris@16
|
12 #define BOOST_ASIO_BASIC_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 <boost/asio/async_result.hpp>
|
Chris@16
|
20 #include <boost/asio/basic_io_object.hpp>
|
Chris@16
|
21 #include <boost/asio/detail/handler_type_requirements.hpp>
|
Chris@16
|
22 #include <boost/asio/detail/throw_error.hpp>
|
Chris@16
|
23 #include <boost/asio/detail/type_traits.hpp>
|
Chris@16
|
24 #include <boost/asio/error.hpp>
|
Chris@16
|
25 #include <boost/asio/socket_base.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 socket functionality.
|
Chris@16
|
33 /**
|
Chris@16
|
34 * The basic_socket class template provides functionality that is common to both
|
Chris@16
|
35 * stream-oriented and datagram-oriented sockets.
|
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 template <typename Protocol, typename SocketService>
|
Chris@16
|
42 class basic_socket
|
Chris@16
|
43 : public basic_io_object<SocketService>,
|
Chris@16
|
44 public socket_base
|
Chris@16
|
45 {
|
Chris@16
|
46 public:
|
Chris@16
|
47 /// (Deprecated: Use native_handle_type.) The native representation of a
|
Chris@16
|
48 /// socket.
|
Chris@16
|
49 typedef typename SocketService::native_handle_type native_type;
|
Chris@16
|
50
|
Chris@16
|
51 /// The native representation of a socket.
|
Chris@16
|
52 typedef typename SocketService::native_handle_type native_handle_type;
|
Chris@16
|
53
|
Chris@16
|
54 /// The protocol type.
|
Chris@16
|
55 typedef Protocol protocol_type;
|
Chris@16
|
56
|
Chris@16
|
57 /// The endpoint type.
|
Chris@16
|
58 typedef typename Protocol::endpoint endpoint_type;
|
Chris@16
|
59
|
Chris@16
|
60 /// A basic_socket is always the lowest layer.
|
Chris@16
|
61 typedef basic_socket<Protocol, SocketService> lowest_layer_type;
|
Chris@16
|
62
|
Chris@16
|
63 /// Construct a basic_socket without opening it.
|
Chris@16
|
64 /**
|
Chris@16
|
65 * This constructor creates a socket without opening it.
|
Chris@16
|
66 *
|
Chris@16
|
67 * @param io_service The io_service object that the socket will use to
|
Chris@16
|
68 * dispatch handlers for any asynchronous operations performed on the socket.
|
Chris@16
|
69 */
|
Chris@16
|
70 explicit basic_socket(boost::asio::io_service& io_service)
|
Chris@16
|
71 : basic_io_object<SocketService>(io_service)
|
Chris@16
|
72 {
|
Chris@16
|
73 }
|
Chris@16
|
74
|
Chris@16
|
75 /// Construct and open a basic_socket.
|
Chris@16
|
76 /**
|
Chris@16
|
77 * This constructor creates and opens a socket.
|
Chris@16
|
78 *
|
Chris@16
|
79 * @param io_service The io_service object that the socket will use to
|
Chris@16
|
80 * dispatch handlers for any asynchronous operations performed on the socket.
|
Chris@16
|
81 *
|
Chris@16
|
82 * @param protocol An object specifying protocol parameters to be used.
|
Chris@16
|
83 *
|
Chris@16
|
84 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
85 */
|
Chris@16
|
86 basic_socket(boost::asio::io_service& io_service,
|
Chris@16
|
87 const protocol_type& protocol)
|
Chris@16
|
88 : basic_io_object<SocketService>(io_service)
|
Chris@16
|
89 {
|
Chris@16
|
90 boost::system::error_code ec;
|
Chris@16
|
91 this->get_service().open(this->get_implementation(), protocol, ec);
|
Chris@16
|
92 boost::asio::detail::throw_error(ec, "open");
|
Chris@16
|
93 }
|
Chris@16
|
94
|
Chris@16
|
95 /// Construct a basic_socket, opening it and binding it to the given local
|
Chris@16
|
96 /// endpoint.
|
Chris@16
|
97 /**
|
Chris@16
|
98 * This constructor creates a socket and automatically opens it bound to the
|
Chris@16
|
99 * specified endpoint on the local machine. The protocol used is the protocol
|
Chris@16
|
100 * associated with the given endpoint.
|
Chris@16
|
101 *
|
Chris@16
|
102 * @param io_service The io_service object that the 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 socket will
|
Chris@16
|
106 * be bound.
|
Chris@16
|
107 *
|
Chris@16
|
108 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
109 */
|
Chris@16
|
110 basic_socket(boost::asio::io_service& io_service,
|
Chris@16
|
111 const endpoint_type& endpoint)
|
Chris@16
|
112 : basic_io_object<SocketService>(io_service)
|
Chris@16
|
113 {
|
Chris@16
|
114 boost::system::error_code ec;
|
Chris@16
|
115 const protocol_type protocol = endpoint.protocol();
|
Chris@16
|
116 this->get_service().open(this->get_implementation(), protocol, ec);
|
Chris@16
|
117 boost::asio::detail::throw_error(ec, "open");
|
Chris@16
|
118 this->get_service().bind(this->get_implementation(), endpoint, ec);
|
Chris@16
|
119 boost::asio::detail::throw_error(ec, "bind");
|
Chris@16
|
120 }
|
Chris@16
|
121
|
Chris@16
|
122 /// Construct a basic_socket on an existing native socket.
|
Chris@16
|
123 /**
|
Chris@16
|
124 * This constructor creates a socket object to hold an existing native socket.
|
Chris@16
|
125 *
|
Chris@16
|
126 * @param io_service The io_service object that the socket will use to
|
Chris@16
|
127 * dispatch handlers for any asynchronous operations performed on the socket.
|
Chris@16
|
128 *
|
Chris@16
|
129 * @param protocol An object specifying protocol parameters to be used.
|
Chris@16
|
130 *
|
Chris@16
|
131 * @param native_socket A native socket.
|
Chris@16
|
132 *
|
Chris@16
|
133 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
134 */
|
Chris@16
|
135 basic_socket(boost::asio::io_service& io_service,
|
Chris@16
|
136 const protocol_type& protocol, const native_handle_type& native_socket)
|
Chris@16
|
137 : basic_io_object<SocketService>(io_service)
|
Chris@16
|
138 {
|
Chris@16
|
139 boost::system::error_code ec;
|
Chris@16
|
140 this->get_service().assign(this->get_implementation(),
|
Chris@16
|
141 protocol, native_socket, ec);
|
Chris@16
|
142 boost::asio::detail::throw_error(ec, "assign");
|
Chris@16
|
143 }
|
Chris@16
|
144
|
Chris@16
|
145 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
146 /// Move-construct a basic_socket from another.
|
Chris@16
|
147 /**
|
Chris@16
|
148 * This constructor moves a socket from one object to another.
|
Chris@16
|
149 *
|
Chris@16
|
150 * @param other The other basic_socket object from which the move will
|
Chris@16
|
151 * occur.
|
Chris@16
|
152 *
|
Chris@16
|
153 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
154 * constructed using the @c basic_socket(io_service&) constructor.
|
Chris@16
|
155 */
|
Chris@16
|
156 basic_socket(basic_socket&& other)
|
Chris@16
|
157 : basic_io_object<SocketService>(
|
Chris@16
|
158 BOOST_ASIO_MOVE_CAST(basic_socket)(other))
|
Chris@16
|
159 {
|
Chris@16
|
160 }
|
Chris@16
|
161
|
Chris@16
|
162 /// Move-assign a basic_socket from another.
|
Chris@16
|
163 /**
|
Chris@16
|
164 * This assignment operator moves a socket from one object to another.
|
Chris@16
|
165 *
|
Chris@16
|
166 * @param other The other basic_socket object from which the move will
|
Chris@16
|
167 * occur.
|
Chris@16
|
168 *
|
Chris@16
|
169 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
170 * constructed using the @c basic_socket(io_service&) constructor.
|
Chris@16
|
171 */
|
Chris@16
|
172 basic_socket& operator=(basic_socket&& other)
|
Chris@16
|
173 {
|
Chris@16
|
174 basic_io_object<SocketService>::operator=(
|
Chris@16
|
175 BOOST_ASIO_MOVE_CAST(basic_socket)(other));
|
Chris@16
|
176 return *this;
|
Chris@16
|
177 }
|
Chris@16
|
178
|
Chris@16
|
179 // All sockets have access to each other's implementations.
|
Chris@16
|
180 template <typename Protocol1, typename SocketService1>
|
Chris@16
|
181 friend class basic_socket;
|
Chris@16
|
182
|
Chris@16
|
183 /// Move-construct a basic_socket from a socket of another protocol type.
|
Chris@16
|
184 /**
|
Chris@16
|
185 * This constructor moves a socket from one object to another.
|
Chris@16
|
186 *
|
Chris@16
|
187 * @param other The other basic_socket object from which the move will
|
Chris@16
|
188 * occur.
|
Chris@16
|
189 *
|
Chris@16
|
190 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
191 * constructed using the @c basic_socket(io_service&) constructor.
|
Chris@16
|
192 */
|
Chris@16
|
193 template <typename Protocol1, typename SocketService1>
|
Chris@16
|
194 basic_socket(basic_socket<Protocol1, SocketService1>&& other,
|
Chris@16
|
195 typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
|
Chris@16
|
196 : basic_io_object<SocketService>(other.get_io_service())
|
Chris@16
|
197 {
|
Chris@16
|
198 this->get_service().template converting_move_construct<Protocol1>(
|
Chris@16
|
199 this->get_implementation(), other.get_implementation());
|
Chris@16
|
200 }
|
Chris@16
|
201
|
Chris@16
|
202 /// Move-assign a basic_socket from a socket of another protocol type.
|
Chris@16
|
203 /**
|
Chris@16
|
204 * This assignment operator moves a socket from one object to another.
|
Chris@16
|
205 *
|
Chris@16
|
206 * @param other The other basic_socket object from which the move will
|
Chris@16
|
207 * occur.
|
Chris@16
|
208 *
|
Chris@16
|
209 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
210 * constructed using the @c basic_socket(io_service&) constructor.
|
Chris@16
|
211 */
|
Chris@16
|
212 template <typename Protocol1, typename SocketService1>
|
Chris@16
|
213 typename enable_if<is_convertible<Protocol1, Protocol>::value,
|
Chris@16
|
214 basic_socket>::type& operator=(
|
Chris@16
|
215 basic_socket<Protocol1, SocketService1>&& other)
|
Chris@16
|
216 {
|
Chris@16
|
217 basic_socket tmp(BOOST_ASIO_MOVE_CAST2(basic_socket<
|
Chris@16
|
218 Protocol1, SocketService1>)(other));
|
Chris@16
|
219 basic_io_object<SocketService>::operator=(
|
Chris@16
|
220 BOOST_ASIO_MOVE_CAST(basic_socket)(tmp));
|
Chris@16
|
221 return *this;
|
Chris@16
|
222 }
|
Chris@16
|
223 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
224
|
Chris@16
|
225 /// Get a reference to the lowest layer.
|
Chris@16
|
226 /**
|
Chris@16
|
227 * This function returns a reference to the lowest layer in a stack of
|
Chris@16
|
228 * layers. Since a basic_socket cannot contain any further layers, it simply
|
Chris@16
|
229 * returns a reference to itself.
|
Chris@16
|
230 *
|
Chris@16
|
231 * @return A reference to the lowest layer in the stack of layers. Ownership
|
Chris@16
|
232 * is not transferred to the caller.
|
Chris@16
|
233 */
|
Chris@16
|
234 lowest_layer_type& lowest_layer()
|
Chris@16
|
235 {
|
Chris@16
|
236 return *this;
|
Chris@16
|
237 }
|
Chris@16
|
238
|
Chris@16
|
239 /// Get a const reference to the lowest layer.
|
Chris@16
|
240 /**
|
Chris@16
|
241 * This function returns a const reference to the lowest layer in a stack of
|
Chris@16
|
242 * layers. Since a basic_socket cannot contain any further layers, it simply
|
Chris@16
|
243 * returns a reference to itself.
|
Chris@16
|
244 *
|
Chris@16
|
245 * @return A const reference to the lowest layer in the stack of layers.
|
Chris@16
|
246 * Ownership is not transferred to the caller.
|
Chris@16
|
247 */
|
Chris@16
|
248 const lowest_layer_type& lowest_layer() const
|
Chris@16
|
249 {
|
Chris@16
|
250 return *this;
|
Chris@16
|
251 }
|
Chris@16
|
252
|
Chris@16
|
253 /// Open the socket using the specified protocol.
|
Chris@16
|
254 /**
|
Chris@16
|
255 * This function opens the socket so that it will use the specified protocol.
|
Chris@16
|
256 *
|
Chris@16
|
257 * @param protocol An object specifying protocol parameters to be used.
|
Chris@16
|
258 *
|
Chris@16
|
259 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
260 *
|
Chris@16
|
261 * @par Example
|
Chris@16
|
262 * @code
|
Chris@16
|
263 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
264 * socket.open(boost::asio::ip::tcp::v4());
|
Chris@16
|
265 * @endcode
|
Chris@16
|
266 */
|
Chris@16
|
267 void open(const protocol_type& protocol = protocol_type())
|
Chris@16
|
268 {
|
Chris@16
|
269 boost::system::error_code ec;
|
Chris@16
|
270 this->get_service().open(this->get_implementation(), protocol, ec);
|
Chris@16
|
271 boost::asio::detail::throw_error(ec, "open");
|
Chris@16
|
272 }
|
Chris@16
|
273
|
Chris@16
|
274 /// Open the socket using the specified protocol.
|
Chris@16
|
275 /**
|
Chris@16
|
276 * This function opens the socket so that it will use the specified protocol.
|
Chris@16
|
277 *
|
Chris@16
|
278 * @param protocol An object specifying which protocol is to be used.
|
Chris@16
|
279 *
|
Chris@16
|
280 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
281 *
|
Chris@16
|
282 * @par Example
|
Chris@16
|
283 * @code
|
Chris@16
|
284 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
285 * boost::system::error_code ec;
|
Chris@16
|
286 * socket.open(boost::asio::ip::tcp::v4(), ec);
|
Chris@16
|
287 * if (ec)
|
Chris@16
|
288 * {
|
Chris@16
|
289 * // An error occurred.
|
Chris@16
|
290 * }
|
Chris@16
|
291 * @endcode
|
Chris@16
|
292 */
|
Chris@16
|
293 boost::system::error_code open(const protocol_type& protocol,
|
Chris@16
|
294 boost::system::error_code& ec)
|
Chris@16
|
295 {
|
Chris@16
|
296 return this->get_service().open(this->get_implementation(), protocol, ec);
|
Chris@16
|
297 }
|
Chris@16
|
298
|
Chris@16
|
299 /// Assign an existing native socket to the socket.
|
Chris@16
|
300 /*
|
Chris@16
|
301 * This function opens the socket to hold an existing native socket.
|
Chris@16
|
302 *
|
Chris@16
|
303 * @param protocol An object specifying which protocol is to be used.
|
Chris@16
|
304 *
|
Chris@16
|
305 * @param native_socket A native socket.
|
Chris@16
|
306 *
|
Chris@16
|
307 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
308 */
|
Chris@16
|
309 void assign(const protocol_type& protocol,
|
Chris@16
|
310 const native_handle_type& native_socket)
|
Chris@16
|
311 {
|
Chris@16
|
312 boost::system::error_code ec;
|
Chris@16
|
313 this->get_service().assign(this->get_implementation(),
|
Chris@16
|
314 protocol, native_socket, ec);
|
Chris@16
|
315 boost::asio::detail::throw_error(ec, "assign");
|
Chris@16
|
316 }
|
Chris@16
|
317
|
Chris@16
|
318 /// Assign an existing native socket to the socket.
|
Chris@16
|
319 /*
|
Chris@16
|
320 * This function opens the socket to hold an existing native socket.
|
Chris@16
|
321 *
|
Chris@16
|
322 * @param protocol An object specifying which protocol is to be used.
|
Chris@16
|
323 *
|
Chris@16
|
324 * @param native_socket A native socket.
|
Chris@16
|
325 *
|
Chris@16
|
326 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
327 */
|
Chris@16
|
328 boost::system::error_code assign(const protocol_type& protocol,
|
Chris@16
|
329 const native_handle_type& native_socket, boost::system::error_code& ec)
|
Chris@16
|
330 {
|
Chris@16
|
331 return this->get_service().assign(this->get_implementation(),
|
Chris@16
|
332 protocol, native_socket, ec);
|
Chris@16
|
333 }
|
Chris@16
|
334
|
Chris@16
|
335 /// Determine whether the socket is open.
|
Chris@16
|
336 bool is_open() const
|
Chris@16
|
337 {
|
Chris@16
|
338 return this->get_service().is_open(this->get_implementation());
|
Chris@16
|
339 }
|
Chris@16
|
340
|
Chris@16
|
341 /// Close the socket.
|
Chris@16
|
342 /**
|
Chris@16
|
343 * This function is used to close the socket. Any asynchronous send, receive
|
Chris@16
|
344 * or connect operations will be cancelled immediately, and will complete
|
Chris@16
|
345 * with the boost::asio::error::operation_aborted error.
|
Chris@16
|
346 *
|
Chris@16
|
347 * @throws boost::system::system_error Thrown on failure. Note that, even if
|
Chris@16
|
348 * the function indicates an error, the underlying descriptor is closed.
|
Chris@16
|
349 *
|
Chris@16
|
350 * @note For portable behaviour with respect to graceful closure of a
|
Chris@16
|
351 * connected socket, call shutdown() before closing the socket.
|
Chris@16
|
352 */
|
Chris@16
|
353 void close()
|
Chris@16
|
354 {
|
Chris@16
|
355 boost::system::error_code ec;
|
Chris@16
|
356 this->get_service().close(this->get_implementation(), ec);
|
Chris@16
|
357 boost::asio::detail::throw_error(ec, "close");
|
Chris@16
|
358 }
|
Chris@16
|
359
|
Chris@16
|
360 /// Close the socket.
|
Chris@16
|
361 /**
|
Chris@16
|
362 * This function is used to close the socket. Any asynchronous send, receive
|
Chris@16
|
363 * or connect operations will be cancelled immediately, and will complete
|
Chris@16
|
364 * with the boost::asio::error::operation_aborted error.
|
Chris@16
|
365 *
|
Chris@16
|
366 * @param ec Set to indicate what error occurred, if any. Note that, even if
|
Chris@16
|
367 * the function indicates an error, the underlying descriptor is closed.
|
Chris@16
|
368 *
|
Chris@16
|
369 * @par Example
|
Chris@16
|
370 * @code
|
Chris@16
|
371 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
372 * ...
|
Chris@16
|
373 * boost::system::error_code ec;
|
Chris@16
|
374 * socket.close(ec);
|
Chris@16
|
375 * if (ec)
|
Chris@16
|
376 * {
|
Chris@16
|
377 * // An error occurred.
|
Chris@16
|
378 * }
|
Chris@16
|
379 * @endcode
|
Chris@16
|
380 *
|
Chris@16
|
381 * @note For portable behaviour with respect to graceful closure of a
|
Chris@16
|
382 * connected socket, call shutdown() before closing the socket.
|
Chris@16
|
383 */
|
Chris@16
|
384 boost::system::error_code close(boost::system::error_code& ec)
|
Chris@16
|
385 {
|
Chris@16
|
386 return this->get_service().close(this->get_implementation(), ec);
|
Chris@16
|
387 }
|
Chris@16
|
388
|
Chris@16
|
389 /// (Deprecated: Use native_handle().) Get the native socket representation.
|
Chris@16
|
390 /**
|
Chris@16
|
391 * This function may be used to obtain the underlying representation of the
|
Chris@16
|
392 * socket. This is intended to allow access to native socket functionality
|
Chris@16
|
393 * that is not otherwise provided.
|
Chris@16
|
394 */
|
Chris@16
|
395 native_type native()
|
Chris@16
|
396 {
|
Chris@16
|
397 return this->get_service().native_handle(this->get_implementation());
|
Chris@16
|
398 }
|
Chris@16
|
399
|
Chris@16
|
400 /// Get the native socket representation.
|
Chris@16
|
401 /**
|
Chris@16
|
402 * This function may be used to obtain the underlying representation of the
|
Chris@16
|
403 * socket. This is intended to allow access to native socket functionality
|
Chris@16
|
404 * that is not otherwise provided.
|
Chris@16
|
405 */
|
Chris@16
|
406 native_handle_type native_handle()
|
Chris@16
|
407 {
|
Chris@16
|
408 return this->get_service().native_handle(this->get_implementation());
|
Chris@16
|
409 }
|
Chris@16
|
410
|
Chris@16
|
411 /// Cancel all asynchronous operations associated with the socket.
|
Chris@16
|
412 /**
|
Chris@16
|
413 * This function causes all outstanding asynchronous connect, send and receive
|
Chris@16
|
414 * operations to finish immediately, and the handlers for cancelled operations
|
Chris@16
|
415 * will be passed the boost::asio::error::operation_aborted error.
|
Chris@16
|
416 *
|
Chris@16
|
417 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
418 *
|
Chris@16
|
419 * @note Calls to cancel() will always fail with
|
Chris@16
|
420 * boost::asio::error::operation_not_supported when run on Windows XP, Windows
|
Chris@16
|
421 * Server 2003, and earlier versions of Windows, unless
|
Chris@16
|
422 * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
|
Chris@16
|
423 * two issues that should be considered before enabling its use:
|
Chris@16
|
424 *
|
Chris@16
|
425 * @li It will only cancel asynchronous operations that were initiated in the
|
Chris@16
|
426 * current thread.
|
Chris@16
|
427 *
|
Chris@16
|
428 * @li It can appear to complete without error, but the request to cancel the
|
Chris@16
|
429 * unfinished operations may be silently ignored by the operating system.
|
Chris@16
|
430 * Whether it works or not seems to depend on the drivers that are installed.
|
Chris@16
|
431 *
|
Chris@16
|
432 * For portable cancellation, consider using one of the following
|
Chris@16
|
433 * alternatives:
|
Chris@16
|
434 *
|
Chris@16
|
435 * @li Disable asio's I/O completion port backend by defining
|
Chris@16
|
436 * BOOST_ASIO_DISABLE_IOCP.
|
Chris@16
|
437 *
|
Chris@16
|
438 * @li Use the close() function to simultaneously cancel the outstanding
|
Chris@16
|
439 * operations and close the socket.
|
Chris@16
|
440 *
|
Chris@16
|
441 * When running on Windows Vista, Windows Server 2008, and later, the
|
Chris@16
|
442 * CancelIoEx function is always used. This function does not have the
|
Chris@16
|
443 * problems described above.
|
Chris@16
|
444 */
|
Chris@16
|
445 #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
|
Chris@16
|
446 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
|
Chris@16
|
447 && !defined(BOOST_ASIO_ENABLE_CANCELIO)
|
Chris@16
|
448 __declspec(deprecated("By default, this function always fails with "
|
Chris@16
|
449 "operation_not_supported when used on Windows XP, Windows Server 2003, "
|
Chris@16
|
450 "or earlier. Consult documentation for details."))
|
Chris@16
|
451 #endif
|
Chris@16
|
452 void cancel()
|
Chris@16
|
453 {
|
Chris@16
|
454 boost::system::error_code ec;
|
Chris@16
|
455 this->get_service().cancel(this->get_implementation(), ec);
|
Chris@16
|
456 boost::asio::detail::throw_error(ec, "cancel");
|
Chris@16
|
457 }
|
Chris@16
|
458
|
Chris@16
|
459 /// Cancel all asynchronous operations associated with the socket.
|
Chris@16
|
460 /**
|
Chris@16
|
461 * This function causes all outstanding asynchronous connect, send and receive
|
Chris@16
|
462 * operations to finish immediately, and the handlers for cancelled operations
|
Chris@16
|
463 * will be passed the boost::asio::error::operation_aborted error.
|
Chris@16
|
464 *
|
Chris@16
|
465 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
466 *
|
Chris@16
|
467 * @note Calls to cancel() will always fail with
|
Chris@16
|
468 * boost::asio::error::operation_not_supported when run on Windows XP, Windows
|
Chris@16
|
469 * Server 2003, and earlier versions of Windows, unless
|
Chris@16
|
470 * BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
|
Chris@16
|
471 * two issues that should be considered before enabling its use:
|
Chris@16
|
472 *
|
Chris@16
|
473 * @li It will only cancel asynchronous operations that were initiated in the
|
Chris@16
|
474 * current thread.
|
Chris@16
|
475 *
|
Chris@16
|
476 * @li It can appear to complete without error, but the request to cancel the
|
Chris@16
|
477 * unfinished operations may be silently ignored by the operating system.
|
Chris@16
|
478 * Whether it works or not seems to depend on the drivers that are installed.
|
Chris@16
|
479 *
|
Chris@16
|
480 * For portable cancellation, consider using one of the following
|
Chris@16
|
481 * alternatives:
|
Chris@16
|
482 *
|
Chris@16
|
483 * @li Disable asio's I/O completion port backend by defining
|
Chris@16
|
484 * BOOST_ASIO_DISABLE_IOCP.
|
Chris@16
|
485 *
|
Chris@16
|
486 * @li Use the close() function to simultaneously cancel the outstanding
|
Chris@16
|
487 * operations and close the socket.
|
Chris@16
|
488 *
|
Chris@16
|
489 * When running on Windows Vista, Windows Server 2008, and later, the
|
Chris@16
|
490 * CancelIoEx function is always used. This function does not have the
|
Chris@16
|
491 * problems described above.
|
Chris@16
|
492 */
|
Chris@16
|
493 #if defined(BOOST_ASIO_MSVC) && (BOOST_ASIO_MSVC >= 1400) \
|
Chris@16
|
494 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
|
Chris@16
|
495 && !defined(BOOST_ASIO_ENABLE_CANCELIO)
|
Chris@16
|
496 __declspec(deprecated("By default, this function always fails with "
|
Chris@16
|
497 "operation_not_supported when used on Windows XP, Windows Server 2003, "
|
Chris@16
|
498 "or earlier. Consult documentation for details."))
|
Chris@16
|
499 #endif
|
Chris@16
|
500 boost::system::error_code cancel(boost::system::error_code& ec)
|
Chris@16
|
501 {
|
Chris@16
|
502 return this->get_service().cancel(this->get_implementation(), ec);
|
Chris@16
|
503 }
|
Chris@16
|
504
|
Chris@16
|
505 /// Determine whether the socket is at the out-of-band data mark.
|
Chris@16
|
506 /**
|
Chris@16
|
507 * This function is used to check whether the socket input is currently
|
Chris@16
|
508 * positioned at the out-of-band data mark.
|
Chris@16
|
509 *
|
Chris@16
|
510 * @return A bool indicating whether the socket is at the out-of-band data
|
Chris@16
|
511 * mark.
|
Chris@16
|
512 *
|
Chris@16
|
513 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
514 */
|
Chris@16
|
515 bool at_mark() const
|
Chris@16
|
516 {
|
Chris@16
|
517 boost::system::error_code ec;
|
Chris@16
|
518 bool b = this->get_service().at_mark(this->get_implementation(), ec);
|
Chris@16
|
519 boost::asio::detail::throw_error(ec, "at_mark");
|
Chris@16
|
520 return b;
|
Chris@16
|
521 }
|
Chris@16
|
522
|
Chris@16
|
523 /// Determine whether the socket is at the out-of-band data mark.
|
Chris@16
|
524 /**
|
Chris@16
|
525 * This function is used to check whether the socket input is currently
|
Chris@16
|
526 * positioned at the out-of-band data mark.
|
Chris@16
|
527 *
|
Chris@16
|
528 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
529 *
|
Chris@16
|
530 * @return A bool indicating whether the socket is at the out-of-band data
|
Chris@16
|
531 * mark.
|
Chris@16
|
532 */
|
Chris@16
|
533 bool at_mark(boost::system::error_code& ec) const
|
Chris@16
|
534 {
|
Chris@16
|
535 return this->get_service().at_mark(this->get_implementation(), ec);
|
Chris@16
|
536 }
|
Chris@16
|
537
|
Chris@16
|
538 /// Determine the number of bytes available for reading.
|
Chris@16
|
539 /**
|
Chris@16
|
540 * This function is used to determine the number of bytes that may be read
|
Chris@16
|
541 * without blocking.
|
Chris@16
|
542 *
|
Chris@16
|
543 * @return The number of bytes that may be read without blocking, or 0 if an
|
Chris@16
|
544 * error occurs.
|
Chris@16
|
545 *
|
Chris@16
|
546 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
547 */
|
Chris@16
|
548 std::size_t available() const
|
Chris@16
|
549 {
|
Chris@16
|
550 boost::system::error_code ec;
|
Chris@16
|
551 std::size_t s = this->get_service().available(
|
Chris@16
|
552 this->get_implementation(), ec);
|
Chris@16
|
553 boost::asio::detail::throw_error(ec, "available");
|
Chris@16
|
554 return s;
|
Chris@16
|
555 }
|
Chris@16
|
556
|
Chris@16
|
557 /// Determine the number of bytes available for reading.
|
Chris@16
|
558 /**
|
Chris@16
|
559 * This function is used to determine the number of bytes that may be read
|
Chris@16
|
560 * without blocking.
|
Chris@16
|
561 *
|
Chris@16
|
562 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
563 *
|
Chris@16
|
564 * @return The number of bytes that may be read without blocking, or 0 if an
|
Chris@16
|
565 * error occurs.
|
Chris@16
|
566 */
|
Chris@16
|
567 std::size_t available(boost::system::error_code& ec) const
|
Chris@16
|
568 {
|
Chris@16
|
569 return this->get_service().available(this->get_implementation(), ec);
|
Chris@16
|
570 }
|
Chris@16
|
571
|
Chris@16
|
572 /// Bind the socket to the given local endpoint.
|
Chris@16
|
573 /**
|
Chris@16
|
574 * This function binds the socket to the specified endpoint on the local
|
Chris@16
|
575 * machine.
|
Chris@16
|
576 *
|
Chris@16
|
577 * @param endpoint An endpoint on the local machine to which the socket will
|
Chris@16
|
578 * be bound.
|
Chris@16
|
579 *
|
Chris@16
|
580 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
581 *
|
Chris@16
|
582 * @par Example
|
Chris@16
|
583 * @code
|
Chris@16
|
584 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
585 * socket.open(boost::asio::ip::tcp::v4());
|
Chris@16
|
586 * socket.bind(boost::asio::ip::tcp::endpoint(
|
Chris@16
|
587 * boost::asio::ip::tcp::v4(), 12345));
|
Chris@16
|
588 * @endcode
|
Chris@16
|
589 */
|
Chris@16
|
590 void bind(const endpoint_type& endpoint)
|
Chris@16
|
591 {
|
Chris@16
|
592 boost::system::error_code ec;
|
Chris@16
|
593 this->get_service().bind(this->get_implementation(), endpoint, ec);
|
Chris@16
|
594 boost::asio::detail::throw_error(ec, "bind");
|
Chris@16
|
595 }
|
Chris@16
|
596
|
Chris@16
|
597 /// Bind the socket to the given local endpoint.
|
Chris@16
|
598 /**
|
Chris@16
|
599 * This function binds the socket to the specified endpoint on the local
|
Chris@16
|
600 * machine.
|
Chris@16
|
601 *
|
Chris@16
|
602 * @param endpoint An endpoint on the local machine to which the socket will
|
Chris@16
|
603 * be bound.
|
Chris@16
|
604 *
|
Chris@16
|
605 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
606 *
|
Chris@16
|
607 * @par Example
|
Chris@16
|
608 * @code
|
Chris@16
|
609 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
610 * socket.open(boost::asio::ip::tcp::v4());
|
Chris@16
|
611 * boost::system::error_code ec;
|
Chris@16
|
612 * socket.bind(boost::asio::ip::tcp::endpoint(
|
Chris@16
|
613 * boost::asio::ip::tcp::v4(), 12345), ec);
|
Chris@16
|
614 * if (ec)
|
Chris@16
|
615 * {
|
Chris@16
|
616 * // An error occurred.
|
Chris@16
|
617 * }
|
Chris@16
|
618 * @endcode
|
Chris@16
|
619 */
|
Chris@16
|
620 boost::system::error_code bind(const endpoint_type& endpoint,
|
Chris@16
|
621 boost::system::error_code& ec)
|
Chris@16
|
622 {
|
Chris@16
|
623 return this->get_service().bind(this->get_implementation(), endpoint, ec);
|
Chris@16
|
624 }
|
Chris@16
|
625
|
Chris@16
|
626 /// Connect the socket to the specified endpoint.
|
Chris@16
|
627 /**
|
Chris@16
|
628 * This function is used to connect a socket to the specified remote endpoint.
|
Chris@16
|
629 * The function call will block until the connection is successfully made or
|
Chris@16
|
630 * an error occurs.
|
Chris@16
|
631 *
|
Chris@16
|
632 * The socket is automatically opened if it is not already open. If the
|
Chris@16
|
633 * connect fails, and the socket was automatically opened, the socket is
|
Chris@16
|
634 * not returned to the closed state.
|
Chris@16
|
635 *
|
Chris@16
|
636 * @param peer_endpoint The remote endpoint to which the socket will be
|
Chris@16
|
637 * connected.
|
Chris@16
|
638 *
|
Chris@16
|
639 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
640 *
|
Chris@16
|
641 * @par Example
|
Chris@16
|
642 * @code
|
Chris@16
|
643 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
644 * boost::asio::ip::tcp::endpoint endpoint(
|
Chris@16
|
645 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
|
Chris@16
|
646 * socket.connect(endpoint);
|
Chris@16
|
647 * @endcode
|
Chris@16
|
648 */
|
Chris@16
|
649 void connect(const endpoint_type& peer_endpoint)
|
Chris@16
|
650 {
|
Chris@16
|
651 boost::system::error_code ec;
|
Chris@16
|
652 if (!is_open())
|
Chris@16
|
653 {
|
Chris@16
|
654 this->get_service().open(this->get_implementation(),
|
Chris@16
|
655 peer_endpoint.protocol(), ec);
|
Chris@16
|
656 boost::asio::detail::throw_error(ec, "connect");
|
Chris@16
|
657 }
|
Chris@16
|
658 this->get_service().connect(this->get_implementation(), peer_endpoint, ec);
|
Chris@16
|
659 boost::asio::detail::throw_error(ec, "connect");
|
Chris@16
|
660 }
|
Chris@16
|
661
|
Chris@16
|
662 /// Connect the socket to the specified endpoint.
|
Chris@16
|
663 /**
|
Chris@16
|
664 * This function is used to connect a socket to the specified remote endpoint.
|
Chris@16
|
665 * The function call will block until the connection is successfully made or
|
Chris@16
|
666 * an error occurs.
|
Chris@16
|
667 *
|
Chris@16
|
668 * The socket is automatically opened if it is not already open. If the
|
Chris@16
|
669 * connect fails, and the socket was automatically opened, the socket is
|
Chris@16
|
670 * not returned to the closed state.
|
Chris@16
|
671 *
|
Chris@16
|
672 * @param peer_endpoint The remote endpoint to which the socket will be
|
Chris@16
|
673 * connected.
|
Chris@16
|
674 *
|
Chris@16
|
675 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
676 *
|
Chris@16
|
677 * @par Example
|
Chris@16
|
678 * @code
|
Chris@16
|
679 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
680 * boost::asio::ip::tcp::endpoint endpoint(
|
Chris@16
|
681 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
|
Chris@16
|
682 * boost::system::error_code ec;
|
Chris@16
|
683 * socket.connect(endpoint, ec);
|
Chris@16
|
684 * if (ec)
|
Chris@16
|
685 * {
|
Chris@16
|
686 * // An error occurred.
|
Chris@16
|
687 * }
|
Chris@16
|
688 * @endcode
|
Chris@16
|
689 */
|
Chris@16
|
690 boost::system::error_code connect(const endpoint_type& peer_endpoint,
|
Chris@16
|
691 boost::system::error_code& ec)
|
Chris@16
|
692 {
|
Chris@16
|
693 if (!is_open())
|
Chris@16
|
694 {
|
Chris@16
|
695 if (this->get_service().open(this->get_implementation(),
|
Chris@16
|
696 peer_endpoint.protocol(), ec))
|
Chris@16
|
697 {
|
Chris@16
|
698 return ec;
|
Chris@16
|
699 }
|
Chris@16
|
700 }
|
Chris@16
|
701
|
Chris@16
|
702 return this->get_service().connect(
|
Chris@16
|
703 this->get_implementation(), peer_endpoint, ec);
|
Chris@16
|
704 }
|
Chris@16
|
705
|
Chris@16
|
706 /// Start an asynchronous connect.
|
Chris@16
|
707 /**
|
Chris@16
|
708 * This function is used to asynchronously connect a socket to the specified
|
Chris@16
|
709 * remote endpoint. The function call always returns immediately.
|
Chris@16
|
710 *
|
Chris@16
|
711 * The socket is automatically opened if it is not already open. If the
|
Chris@16
|
712 * connect fails, and the socket was automatically opened, the socket is
|
Chris@16
|
713 * not returned to the closed state.
|
Chris@16
|
714 *
|
Chris@16
|
715 * @param peer_endpoint The remote endpoint to which the socket will be
|
Chris@16
|
716 * connected. Copies will be made of the endpoint object as required.
|
Chris@16
|
717 *
|
Chris@16
|
718 * @param handler The handler to be called when the connection operation
|
Chris@16
|
719 * completes. Copies will be made of the handler as required. The function
|
Chris@16
|
720 * signature of the handler must be:
|
Chris@16
|
721 * @code void handler(
|
Chris@16
|
722 * const boost::system::error_code& error // Result of operation
|
Chris@16
|
723 * ); @endcode
|
Chris@16
|
724 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
725 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
726 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
727 * boost::asio::io_service::post().
|
Chris@16
|
728 *
|
Chris@16
|
729 * @par Example
|
Chris@16
|
730 * @code
|
Chris@16
|
731 * void connect_handler(const boost::system::error_code& error)
|
Chris@16
|
732 * {
|
Chris@16
|
733 * if (!error)
|
Chris@16
|
734 * {
|
Chris@16
|
735 * // Connect succeeded.
|
Chris@16
|
736 * }
|
Chris@16
|
737 * }
|
Chris@16
|
738 *
|
Chris@16
|
739 * ...
|
Chris@16
|
740 *
|
Chris@16
|
741 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
742 * boost::asio::ip::tcp::endpoint endpoint(
|
Chris@16
|
743 * boost::asio::ip::address::from_string("1.2.3.4"), 12345);
|
Chris@16
|
744 * socket.async_connect(endpoint, connect_handler);
|
Chris@16
|
745 * @endcode
|
Chris@16
|
746 */
|
Chris@16
|
747 template <typename ConnectHandler>
|
Chris@16
|
748 BOOST_ASIO_INITFN_RESULT_TYPE(ConnectHandler,
|
Chris@16
|
749 void (boost::system::error_code))
|
Chris@16
|
750 async_connect(const endpoint_type& peer_endpoint,
|
Chris@16
|
751 BOOST_ASIO_MOVE_ARG(ConnectHandler) handler)
|
Chris@16
|
752 {
|
Chris@16
|
753 // If you get an error on the following line it means that your handler does
|
Chris@16
|
754 // not meet the documented type requirements for a ConnectHandler.
|
Chris@16
|
755 BOOST_ASIO_CONNECT_HANDLER_CHECK(ConnectHandler, handler) type_check;
|
Chris@16
|
756
|
Chris@16
|
757 if (!is_open())
|
Chris@16
|
758 {
|
Chris@16
|
759 boost::system::error_code ec;
|
Chris@16
|
760 const protocol_type protocol = peer_endpoint.protocol();
|
Chris@16
|
761 if (this->get_service().open(this->get_implementation(), protocol, ec))
|
Chris@16
|
762 {
|
Chris@16
|
763 detail::async_result_init<
|
Chris@16
|
764 ConnectHandler, void (boost::system::error_code)> init(
|
Chris@16
|
765 BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler));
|
Chris@16
|
766
|
Chris@16
|
767 this->get_io_service().post(
|
Chris@16
|
768 boost::asio::detail::bind_handler(
|
Chris@16
|
769 BOOST_ASIO_MOVE_CAST(BOOST_ASIO_HANDLER_TYPE(
|
Chris@16
|
770 ConnectHandler, void (boost::system::error_code)))(
|
Chris@16
|
771 init.handler), ec));
|
Chris@16
|
772
|
Chris@16
|
773 return init.result.get();
|
Chris@16
|
774 }
|
Chris@16
|
775 }
|
Chris@16
|
776
|
Chris@16
|
777 return this->get_service().async_connect(this->get_implementation(),
|
Chris@16
|
778 peer_endpoint, BOOST_ASIO_MOVE_CAST(ConnectHandler)(handler));
|
Chris@16
|
779 }
|
Chris@16
|
780
|
Chris@16
|
781 /// Set an option on the socket.
|
Chris@16
|
782 /**
|
Chris@16
|
783 * This function is used to set an option on the socket.
|
Chris@16
|
784 *
|
Chris@16
|
785 * @param option The new option value to be set on the socket.
|
Chris@16
|
786 *
|
Chris@16
|
787 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
788 *
|
Chris@16
|
789 * @sa SettableSocketOption @n
|
Chris@16
|
790 * boost::asio::socket_base::broadcast @n
|
Chris@16
|
791 * boost::asio::socket_base::do_not_route @n
|
Chris@16
|
792 * boost::asio::socket_base::keep_alive @n
|
Chris@16
|
793 * boost::asio::socket_base::linger @n
|
Chris@16
|
794 * boost::asio::socket_base::receive_buffer_size @n
|
Chris@16
|
795 * boost::asio::socket_base::receive_low_watermark @n
|
Chris@16
|
796 * boost::asio::socket_base::reuse_address @n
|
Chris@16
|
797 * boost::asio::socket_base::send_buffer_size @n
|
Chris@16
|
798 * boost::asio::socket_base::send_low_watermark @n
|
Chris@16
|
799 * boost::asio::ip::multicast::join_group @n
|
Chris@16
|
800 * boost::asio::ip::multicast::leave_group @n
|
Chris@16
|
801 * boost::asio::ip::multicast::enable_loopback @n
|
Chris@16
|
802 * boost::asio::ip::multicast::outbound_interface @n
|
Chris@16
|
803 * boost::asio::ip::multicast::hops @n
|
Chris@16
|
804 * boost::asio::ip::tcp::no_delay
|
Chris@16
|
805 *
|
Chris@16
|
806 * @par Example
|
Chris@16
|
807 * Setting the IPPROTO_TCP/TCP_NODELAY option:
|
Chris@16
|
808 * @code
|
Chris@16
|
809 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
810 * ...
|
Chris@16
|
811 * boost::asio::ip::tcp::no_delay option(true);
|
Chris@16
|
812 * socket.set_option(option);
|
Chris@16
|
813 * @endcode
|
Chris@16
|
814 */
|
Chris@16
|
815 template <typename SettableSocketOption>
|
Chris@16
|
816 void set_option(const SettableSocketOption& option)
|
Chris@16
|
817 {
|
Chris@16
|
818 boost::system::error_code ec;
|
Chris@16
|
819 this->get_service().set_option(this->get_implementation(), option, ec);
|
Chris@16
|
820 boost::asio::detail::throw_error(ec, "set_option");
|
Chris@16
|
821 }
|
Chris@16
|
822
|
Chris@16
|
823 /// Set an option on the socket.
|
Chris@16
|
824 /**
|
Chris@16
|
825 * This function is used to set an option on the socket.
|
Chris@16
|
826 *
|
Chris@16
|
827 * @param option The new option value to be set on the socket.
|
Chris@16
|
828 *
|
Chris@16
|
829 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
830 *
|
Chris@16
|
831 * @sa SettableSocketOption @n
|
Chris@16
|
832 * boost::asio::socket_base::broadcast @n
|
Chris@16
|
833 * boost::asio::socket_base::do_not_route @n
|
Chris@16
|
834 * boost::asio::socket_base::keep_alive @n
|
Chris@16
|
835 * boost::asio::socket_base::linger @n
|
Chris@16
|
836 * boost::asio::socket_base::receive_buffer_size @n
|
Chris@16
|
837 * boost::asio::socket_base::receive_low_watermark @n
|
Chris@16
|
838 * boost::asio::socket_base::reuse_address @n
|
Chris@16
|
839 * boost::asio::socket_base::send_buffer_size @n
|
Chris@16
|
840 * boost::asio::socket_base::send_low_watermark @n
|
Chris@16
|
841 * boost::asio::ip::multicast::join_group @n
|
Chris@16
|
842 * boost::asio::ip::multicast::leave_group @n
|
Chris@16
|
843 * boost::asio::ip::multicast::enable_loopback @n
|
Chris@16
|
844 * boost::asio::ip::multicast::outbound_interface @n
|
Chris@16
|
845 * boost::asio::ip::multicast::hops @n
|
Chris@16
|
846 * boost::asio::ip::tcp::no_delay
|
Chris@16
|
847 *
|
Chris@16
|
848 * @par Example
|
Chris@16
|
849 * Setting the IPPROTO_TCP/TCP_NODELAY option:
|
Chris@16
|
850 * @code
|
Chris@16
|
851 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
852 * ...
|
Chris@16
|
853 * boost::asio::ip::tcp::no_delay option(true);
|
Chris@16
|
854 * boost::system::error_code ec;
|
Chris@16
|
855 * socket.set_option(option, ec);
|
Chris@16
|
856 * if (ec)
|
Chris@16
|
857 * {
|
Chris@16
|
858 * // An error occurred.
|
Chris@16
|
859 * }
|
Chris@16
|
860 * @endcode
|
Chris@16
|
861 */
|
Chris@16
|
862 template <typename SettableSocketOption>
|
Chris@16
|
863 boost::system::error_code set_option(const SettableSocketOption& option,
|
Chris@16
|
864 boost::system::error_code& ec)
|
Chris@16
|
865 {
|
Chris@16
|
866 return this->get_service().set_option(
|
Chris@16
|
867 this->get_implementation(), option, ec);
|
Chris@16
|
868 }
|
Chris@16
|
869
|
Chris@16
|
870 /// Get an option from the socket.
|
Chris@16
|
871 /**
|
Chris@16
|
872 * This function is used to get the current value of an option on the socket.
|
Chris@16
|
873 *
|
Chris@16
|
874 * @param option The option value to be obtained from the socket.
|
Chris@16
|
875 *
|
Chris@16
|
876 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
877 *
|
Chris@16
|
878 * @sa GettableSocketOption @n
|
Chris@16
|
879 * boost::asio::socket_base::broadcast @n
|
Chris@16
|
880 * boost::asio::socket_base::do_not_route @n
|
Chris@16
|
881 * boost::asio::socket_base::keep_alive @n
|
Chris@16
|
882 * boost::asio::socket_base::linger @n
|
Chris@16
|
883 * boost::asio::socket_base::receive_buffer_size @n
|
Chris@16
|
884 * boost::asio::socket_base::receive_low_watermark @n
|
Chris@16
|
885 * boost::asio::socket_base::reuse_address @n
|
Chris@16
|
886 * boost::asio::socket_base::send_buffer_size @n
|
Chris@16
|
887 * boost::asio::socket_base::send_low_watermark @n
|
Chris@16
|
888 * boost::asio::ip::multicast::join_group @n
|
Chris@16
|
889 * boost::asio::ip::multicast::leave_group @n
|
Chris@16
|
890 * boost::asio::ip::multicast::enable_loopback @n
|
Chris@16
|
891 * boost::asio::ip::multicast::outbound_interface @n
|
Chris@16
|
892 * boost::asio::ip::multicast::hops @n
|
Chris@16
|
893 * boost::asio::ip::tcp::no_delay
|
Chris@16
|
894 *
|
Chris@16
|
895 * @par Example
|
Chris@16
|
896 * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
|
Chris@16
|
897 * @code
|
Chris@16
|
898 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
899 * ...
|
Chris@16
|
900 * boost::asio::ip::tcp::socket::keep_alive option;
|
Chris@16
|
901 * socket.get_option(option);
|
Chris@16
|
902 * bool is_set = option.value();
|
Chris@16
|
903 * @endcode
|
Chris@16
|
904 */
|
Chris@16
|
905 template <typename GettableSocketOption>
|
Chris@16
|
906 void get_option(GettableSocketOption& option) const
|
Chris@16
|
907 {
|
Chris@16
|
908 boost::system::error_code ec;
|
Chris@16
|
909 this->get_service().get_option(this->get_implementation(), option, ec);
|
Chris@16
|
910 boost::asio::detail::throw_error(ec, "get_option");
|
Chris@16
|
911 }
|
Chris@16
|
912
|
Chris@16
|
913 /// Get an option from the socket.
|
Chris@16
|
914 /**
|
Chris@16
|
915 * This function is used to get the current value of an option on the socket.
|
Chris@16
|
916 *
|
Chris@16
|
917 * @param option The option value to be obtained from the socket.
|
Chris@16
|
918 *
|
Chris@16
|
919 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
920 *
|
Chris@16
|
921 * @sa GettableSocketOption @n
|
Chris@16
|
922 * boost::asio::socket_base::broadcast @n
|
Chris@16
|
923 * boost::asio::socket_base::do_not_route @n
|
Chris@16
|
924 * boost::asio::socket_base::keep_alive @n
|
Chris@16
|
925 * boost::asio::socket_base::linger @n
|
Chris@16
|
926 * boost::asio::socket_base::receive_buffer_size @n
|
Chris@16
|
927 * boost::asio::socket_base::receive_low_watermark @n
|
Chris@16
|
928 * boost::asio::socket_base::reuse_address @n
|
Chris@16
|
929 * boost::asio::socket_base::send_buffer_size @n
|
Chris@16
|
930 * boost::asio::socket_base::send_low_watermark @n
|
Chris@16
|
931 * boost::asio::ip::multicast::join_group @n
|
Chris@16
|
932 * boost::asio::ip::multicast::leave_group @n
|
Chris@16
|
933 * boost::asio::ip::multicast::enable_loopback @n
|
Chris@16
|
934 * boost::asio::ip::multicast::outbound_interface @n
|
Chris@16
|
935 * boost::asio::ip::multicast::hops @n
|
Chris@16
|
936 * boost::asio::ip::tcp::no_delay
|
Chris@16
|
937 *
|
Chris@16
|
938 * @par Example
|
Chris@16
|
939 * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
|
Chris@16
|
940 * @code
|
Chris@16
|
941 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
942 * ...
|
Chris@16
|
943 * boost::asio::ip::tcp::socket::keep_alive option;
|
Chris@16
|
944 * boost::system::error_code ec;
|
Chris@16
|
945 * socket.get_option(option, ec);
|
Chris@16
|
946 * if (ec)
|
Chris@16
|
947 * {
|
Chris@16
|
948 * // An error occurred.
|
Chris@16
|
949 * }
|
Chris@16
|
950 * bool is_set = option.value();
|
Chris@16
|
951 * @endcode
|
Chris@16
|
952 */
|
Chris@16
|
953 template <typename GettableSocketOption>
|
Chris@16
|
954 boost::system::error_code get_option(GettableSocketOption& option,
|
Chris@16
|
955 boost::system::error_code& ec) const
|
Chris@16
|
956 {
|
Chris@16
|
957 return this->get_service().get_option(
|
Chris@16
|
958 this->get_implementation(), option, ec);
|
Chris@16
|
959 }
|
Chris@16
|
960
|
Chris@16
|
961 /// Perform an IO control command on the socket.
|
Chris@16
|
962 /**
|
Chris@16
|
963 * This function is used to execute an IO control command on the socket.
|
Chris@16
|
964 *
|
Chris@16
|
965 * @param command The IO control command to be performed on the socket.
|
Chris@16
|
966 *
|
Chris@16
|
967 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
968 *
|
Chris@16
|
969 * @sa IoControlCommand @n
|
Chris@16
|
970 * boost::asio::socket_base::bytes_readable @n
|
Chris@16
|
971 * boost::asio::socket_base::non_blocking_io
|
Chris@16
|
972 *
|
Chris@16
|
973 * @par Example
|
Chris@16
|
974 * Getting the number of bytes ready to read:
|
Chris@16
|
975 * @code
|
Chris@16
|
976 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
977 * ...
|
Chris@16
|
978 * boost::asio::ip::tcp::socket::bytes_readable command;
|
Chris@16
|
979 * socket.io_control(command);
|
Chris@16
|
980 * std::size_t bytes_readable = command.get();
|
Chris@16
|
981 * @endcode
|
Chris@16
|
982 */
|
Chris@16
|
983 template <typename IoControlCommand>
|
Chris@16
|
984 void io_control(IoControlCommand& command)
|
Chris@16
|
985 {
|
Chris@16
|
986 boost::system::error_code ec;
|
Chris@16
|
987 this->get_service().io_control(this->get_implementation(), command, ec);
|
Chris@16
|
988 boost::asio::detail::throw_error(ec, "io_control");
|
Chris@16
|
989 }
|
Chris@16
|
990
|
Chris@16
|
991 /// Perform an IO control command on the socket.
|
Chris@16
|
992 /**
|
Chris@16
|
993 * This function is used to execute an IO control command on the socket.
|
Chris@16
|
994 *
|
Chris@16
|
995 * @param command The IO control command to be performed on the socket.
|
Chris@16
|
996 *
|
Chris@16
|
997 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
998 *
|
Chris@16
|
999 * @sa IoControlCommand @n
|
Chris@16
|
1000 * boost::asio::socket_base::bytes_readable @n
|
Chris@16
|
1001 * boost::asio::socket_base::non_blocking_io
|
Chris@16
|
1002 *
|
Chris@16
|
1003 * @par Example
|
Chris@16
|
1004 * Getting the number of bytes ready to read:
|
Chris@16
|
1005 * @code
|
Chris@16
|
1006 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
1007 * ...
|
Chris@16
|
1008 * boost::asio::ip::tcp::socket::bytes_readable command;
|
Chris@16
|
1009 * boost::system::error_code ec;
|
Chris@16
|
1010 * socket.io_control(command, ec);
|
Chris@16
|
1011 * if (ec)
|
Chris@16
|
1012 * {
|
Chris@16
|
1013 * // An error occurred.
|
Chris@16
|
1014 * }
|
Chris@16
|
1015 * std::size_t bytes_readable = command.get();
|
Chris@16
|
1016 * @endcode
|
Chris@16
|
1017 */
|
Chris@16
|
1018 template <typename IoControlCommand>
|
Chris@16
|
1019 boost::system::error_code io_control(IoControlCommand& command,
|
Chris@16
|
1020 boost::system::error_code& ec)
|
Chris@16
|
1021 {
|
Chris@16
|
1022 return this->get_service().io_control(
|
Chris@16
|
1023 this->get_implementation(), command, ec);
|
Chris@16
|
1024 }
|
Chris@16
|
1025
|
Chris@16
|
1026 /// Gets the non-blocking mode of the socket.
|
Chris@16
|
1027 /**
|
Chris@16
|
1028 * @returns @c true if the socket's synchronous operations will fail with
|
Chris@16
|
1029 * boost::asio::error::would_block if they are unable to perform the requested
|
Chris@16
|
1030 * operation immediately. If @c false, synchronous operations will block
|
Chris@16
|
1031 * until complete.
|
Chris@16
|
1032 *
|
Chris@16
|
1033 * @note The non-blocking mode has no effect on the behaviour of asynchronous
|
Chris@16
|
1034 * operations. Asynchronous operations will never fail with the error
|
Chris@16
|
1035 * boost::asio::error::would_block.
|
Chris@16
|
1036 */
|
Chris@16
|
1037 bool non_blocking() const
|
Chris@16
|
1038 {
|
Chris@16
|
1039 return this->get_service().non_blocking(this->get_implementation());
|
Chris@16
|
1040 }
|
Chris@16
|
1041
|
Chris@16
|
1042 /// Sets the non-blocking mode of the socket.
|
Chris@16
|
1043 /**
|
Chris@16
|
1044 * @param mode If @c true, the socket's synchronous operations will fail with
|
Chris@16
|
1045 * boost::asio::error::would_block if they are unable to perform the requested
|
Chris@16
|
1046 * operation immediately. If @c false, synchronous operations will block
|
Chris@16
|
1047 * until complete.
|
Chris@16
|
1048 *
|
Chris@16
|
1049 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
1050 *
|
Chris@16
|
1051 * @note The non-blocking mode has no effect on the behaviour of asynchronous
|
Chris@16
|
1052 * operations. Asynchronous operations will never fail with the error
|
Chris@16
|
1053 * boost::asio::error::would_block.
|
Chris@16
|
1054 */
|
Chris@16
|
1055 void non_blocking(bool mode)
|
Chris@16
|
1056 {
|
Chris@16
|
1057 boost::system::error_code ec;
|
Chris@16
|
1058 this->get_service().non_blocking(this->get_implementation(), mode, ec);
|
Chris@16
|
1059 boost::asio::detail::throw_error(ec, "non_blocking");
|
Chris@16
|
1060 }
|
Chris@16
|
1061
|
Chris@16
|
1062 /// Sets the non-blocking mode of the socket.
|
Chris@16
|
1063 /**
|
Chris@16
|
1064 * @param mode If @c true, the socket's synchronous operations will fail with
|
Chris@16
|
1065 * boost::asio::error::would_block if they are unable to perform the requested
|
Chris@16
|
1066 * operation immediately. If @c false, synchronous operations will block
|
Chris@16
|
1067 * until complete.
|
Chris@16
|
1068 *
|
Chris@16
|
1069 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
1070 *
|
Chris@16
|
1071 * @note The non-blocking mode has no effect on the behaviour of asynchronous
|
Chris@16
|
1072 * operations. Asynchronous operations will never fail with the error
|
Chris@16
|
1073 * boost::asio::error::would_block.
|
Chris@16
|
1074 */
|
Chris@16
|
1075 boost::system::error_code non_blocking(
|
Chris@16
|
1076 bool mode, boost::system::error_code& ec)
|
Chris@16
|
1077 {
|
Chris@16
|
1078 return this->get_service().non_blocking(
|
Chris@16
|
1079 this->get_implementation(), mode, ec);
|
Chris@16
|
1080 }
|
Chris@16
|
1081
|
Chris@16
|
1082 /// Gets the non-blocking mode of the native socket implementation.
|
Chris@16
|
1083 /**
|
Chris@16
|
1084 * This function is used to retrieve the non-blocking mode of the underlying
|
Chris@16
|
1085 * native socket. This mode has no effect on the behaviour of the socket
|
Chris@16
|
1086 * object's synchronous operations.
|
Chris@16
|
1087 *
|
Chris@16
|
1088 * @returns @c true if the underlying socket is in non-blocking mode and
|
Chris@16
|
1089 * direct system calls may fail with boost::asio::error::would_block (or the
|
Chris@16
|
1090 * equivalent system error).
|
Chris@16
|
1091 *
|
Chris@16
|
1092 * @note The current non-blocking mode is cached by the socket object.
|
Chris@16
|
1093 * Consequently, the return value may be incorrect if the non-blocking mode
|
Chris@16
|
1094 * was set directly on the native socket.
|
Chris@16
|
1095 *
|
Chris@16
|
1096 * @par Example
|
Chris@16
|
1097 * This function is intended to allow the encapsulation of arbitrary
|
Chris@16
|
1098 * non-blocking system calls as asynchronous operations, in a way that is
|
Chris@16
|
1099 * transparent to the user of the socket object. The following example
|
Chris@16
|
1100 * illustrates how Linux's @c sendfile system call might be encapsulated:
|
Chris@16
|
1101 * @code template <typename Handler>
|
Chris@16
|
1102 * struct sendfile_op
|
Chris@16
|
1103 * {
|
Chris@16
|
1104 * tcp::socket& sock_;
|
Chris@16
|
1105 * int fd_;
|
Chris@16
|
1106 * Handler handler_;
|
Chris@16
|
1107 * off_t offset_;
|
Chris@16
|
1108 * std::size_t total_bytes_transferred_;
|
Chris@16
|
1109 *
|
Chris@16
|
1110 * // Function call operator meeting WriteHandler requirements.
|
Chris@16
|
1111 * // Used as the handler for the async_write_some operation.
|
Chris@16
|
1112 * void operator()(boost::system::error_code ec, std::size_t)
|
Chris@16
|
1113 * {
|
Chris@16
|
1114 * // Put the underlying socket into non-blocking mode.
|
Chris@16
|
1115 * if (!ec)
|
Chris@16
|
1116 * if (!sock_.native_non_blocking())
|
Chris@16
|
1117 * sock_.native_non_blocking(true, ec);
|
Chris@16
|
1118 *
|
Chris@16
|
1119 * if (!ec)
|
Chris@16
|
1120 * {
|
Chris@16
|
1121 * for (;;)
|
Chris@16
|
1122 * {
|
Chris@16
|
1123 * // Try the system call.
|
Chris@16
|
1124 * errno = 0;
|
Chris@16
|
1125 * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
|
Chris@16
|
1126 * ec = boost::system::error_code(n < 0 ? errno : 0,
|
Chris@16
|
1127 * boost::asio::error::get_system_category());
|
Chris@16
|
1128 * total_bytes_transferred_ += ec ? 0 : n;
|
Chris@16
|
1129 *
|
Chris@16
|
1130 * // Retry operation immediately if interrupted by signal.
|
Chris@16
|
1131 * if (ec == boost::asio::error::interrupted)
|
Chris@16
|
1132 * continue;
|
Chris@16
|
1133 *
|
Chris@16
|
1134 * // Check if we need to run the operation again.
|
Chris@16
|
1135 * if (ec == boost::asio::error::would_block
|
Chris@16
|
1136 * || ec == boost::asio::error::try_again)
|
Chris@16
|
1137 * {
|
Chris@16
|
1138 * // We have to wait for the socket to become ready again.
|
Chris@16
|
1139 * sock_.async_write_some(boost::asio::null_buffers(), *this);
|
Chris@16
|
1140 * return;
|
Chris@16
|
1141 * }
|
Chris@16
|
1142 *
|
Chris@16
|
1143 * if (ec || n == 0)
|
Chris@16
|
1144 * {
|
Chris@16
|
1145 * // An error occurred, or we have reached the end of the file.
|
Chris@16
|
1146 * // Either way we must exit the loop so we can call the handler.
|
Chris@16
|
1147 * break;
|
Chris@16
|
1148 * }
|
Chris@16
|
1149 *
|
Chris@16
|
1150 * // Loop around to try calling sendfile again.
|
Chris@16
|
1151 * }
|
Chris@16
|
1152 * }
|
Chris@16
|
1153 *
|
Chris@16
|
1154 * // Pass result back to user's handler.
|
Chris@16
|
1155 * handler_(ec, total_bytes_transferred_);
|
Chris@16
|
1156 * }
|
Chris@16
|
1157 * };
|
Chris@16
|
1158 *
|
Chris@16
|
1159 * template <typename Handler>
|
Chris@16
|
1160 * void async_sendfile(tcp::socket& sock, int fd, Handler h)
|
Chris@16
|
1161 * {
|
Chris@16
|
1162 * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
|
Chris@16
|
1163 * sock.async_write_some(boost::asio::null_buffers(), op);
|
Chris@16
|
1164 * } @endcode
|
Chris@16
|
1165 */
|
Chris@16
|
1166 bool native_non_blocking() const
|
Chris@16
|
1167 {
|
Chris@16
|
1168 return this->get_service().native_non_blocking(this->get_implementation());
|
Chris@16
|
1169 }
|
Chris@16
|
1170
|
Chris@16
|
1171 /// Sets the non-blocking mode of the native socket implementation.
|
Chris@16
|
1172 /**
|
Chris@16
|
1173 * This function is used to modify the non-blocking mode of the underlying
|
Chris@16
|
1174 * native socket. It has no effect on the behaviour of the socket object's
|
Chris@16
|
1175 * synchronous operations.
|
Chris@16
|
1176 *
|
Chris@16
|
1177 * @param mode If @c true, the underlying socket is put into non-blocking
|
Chris@16
|
1178 * mode and direct system calls may fail with boost::asio::error::would_block
|
Chris@16
|
1179 * (or the equivalent system error).
|
Chris@16
|
1180 *
|
Chris@16
|
1181 * @throws boost::system::system_error Thrown on failure. If the @c mode is
|
Chris@16
|
1182 * @c false, but the current value of @c non_blocking() is @c true, this
|
Chris@16
|
1183 * function fails with boost::asio::error::invalid_argument, as the
|
Chris@16
|
1184 * combination does not make sense.
|
Chris@16
|
1185 *
|
Chris@16
|
1186 * @par Example
|
Chris@16
|
1187 * This function is intended to allow the encapsulation of arbitrary
|
Chris@16
|
1188 * non-blocking system calls as asynchronous operations, in a way that is
|
Chris@16
|
1189 * transparent to the user of the socket object. The following example
|
Chris@16
|
1190 * illustrates how Linux's @c sendfile system call might be encapsulated:
|
Chris@16
|
1191 * @code template <typename Handler>
|
Chris@16
|
1192 * struct sendfile_op
|
Chris@16
|
1193 * {
|
Chris@16
|
1194 * tcp::socket& sock_;
|
Chris@16
|
1195 * int fd_;
|
Chris@16
|
1196 * Handler handler_;
|
Chris@16
|
1197 * off_t offset_;
|
Chris@16
|
1198 * std::size_t total_bytes_transferred_;
|
Chris@16
|
1199 *
|
Chris@16
|
1200 * // Function call operator meeting WriteHandler requirements.
|
Chris@16
|
1201 * // Used as the handler for the async_write_some operation.
|
Chris@16
|
1202 * void operator()(boost::system::error_code ec, std::size_t)
|
Chris@16
|
1203 * {
|
Chris@16
|
1204 * // Put the underlying socket into non-blocking mode.
|
Chris@16
|
1205 * if (!ec)
|
Chris@16
|
1206 * if (!sock_.native_non_blocking())
|
Chris@16
|
1207 * sock_.native_non_blocking(true, ec);
|
Chris@16
|
1208 *
|
Chris@16
|
1209 * if (!ec)
|
Chris@16
|
1210 * {
|
Chris@16
|
1211 * for (;;)
|
Chris@16
|
1212 * {
|
Chris@16
|
1213 * // Try the system call.
|
Chris@16
|
1214 * errno = 0;
|
Chris@16
|
1215 * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
|
Chris@16
|
1216 * ec = boost::system::error_code(n < 0 ? errno : 0,
|
Chris@16
|
1217 * boost::asio::error::get_system_category());
|
Chris@16
|
1218 * total_bytes_transferred_ += ec ? 0 : n;
|
Chris@16
|
1219 *
|
Chris@16
|
1220 * // Retry operation immediately if interrupted by signal.
|
Chris@16
|
1221 * if (ec == boost::asio::error::interrupted)
|
Chris@16
|
1222 * continue;
|
Chris@16
|
1223 *
|
Chris@16
|
1224 * // Check if we need to run the operation again.
|
Chris@16
|
1225 * if (ec == boost::asio::error::would_block
|
Chris@16
|
1226 * || ec == boost::asio::error::try_again)
|
Chris@16
|
1227 * {
|
Chris@16
|
1228 * // We have to wait for the socket to become ready again.
|
Chris@16
|
1229 * sock_.async_write_some(boost::asio::null_buffers(), *this);
|
Chris@16
|
1230 * return;
|
Chris@16
|
1231 * }
|
Chris@16
|
1232 *
|
Chris@16
|
1233 * if (ec || n == 0)
|
Chris@16
|
1234 * {
|
Chris@16
|
1235 * // An error occurred, or we have reached the end of the file.
|
Chris@16
|
1236 * // Either way we must exit the loop so we can call the handler.
|
Chris@16
|
1237 * break;
|
Chris@16
|
1238 * }
|
Chris@16
|
1239 *
|
Chris@16
|
1240 * // Loop around to try calling sendfile again.
|
Chris@16
|
1241 * }
|
Chris@16
|
1242 * }
|
Chris@16
|
1243 *
|
Chris@16
|
1244 * // Pass result back to user's handler.
|
Chris@16
|
1245 * handler_(ec, total_bytes_transferred_);
|
Chris@16
|
1246 * }
|
Chris@16
|
1247 * };
|
Chris@16
|
1248 *
|
Chris@16
|
1249 * template <typename Handler>
|
Chris@16
|
1250 * void async_sendfile(tcp::socket& sock, int fd, Handler h)
|
Chris@16
|
1251 * {
|
Chris@16
|
1252 * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
|
Chris@16
|
1253 * sock.async_write_some(boost::asio::null_buffers(), op);
|
Chris@16
|
1254 * } @endcode
|
Chris@16
|
1255 */
|
Chris@16
|
1256 void native_non_blocking(bool mode)
|
Chris@16
|
1257 {
|
Chris@16
|
1258 boost::system::error_code ec;
|
Chris@16
|
1259 this->get_service().native_non_blocking(
|
Chris@16
|
1260 this->get_implementation(), mode, ec);
|
Chris@16
|
1261 boost::asio::detail::throw_error(ec, "native_non_blocking");
|
Chris@16
|
1262 }
|
Chris@16
|
1263
|
Chris@16
|
1264 /// Sets the non-blocking mode of the native socket implementation.
|
Chris@16
|
1265 /**
|
Chris@16
|
1266 * This function is used to modify the non-blocking mode of the underlying
|
Chris@16
|
1267 * native socket. It has no effect on the behaviour of the socket object's
|
Chris@16
|
1268 * synchronous operations.
|
Chris@16
|
1269 *
|
Chris@16
|
1270 * @param mode If @c true, the underlying socket is put into non-blocking
|
Chris@16
|
1271 * mode and direct system calls may fail with boost::asio::error::would_block
|
Chris@16
|
1272 * (or the equivalent system error).
|
Chris@16
|
1273 *
|
Chris@16
|
1274 * @param ec Set to indicate what error occurred, if any. If the @c mode is
|
Chris@16
|
1275 * @c false, but the current value of @c non_blocking() is @c true, this
|
Chris@16
|
1276 * function fails with boost::asio::error::invalid_argument, as the
|
Chris@16
|
1277 * combination does not make sense.
|
Chris@16
|
1278 *
|
Chris@16
|
1279 * @par Example
|
Chris@16
|
1280 * This function is intended to allow the encapsulation of arbitrary
|
Chris@16
|
1281 * non-blocking system calls as asynchronous operations, in a way that is
|
Chris@16
|
1282 * transparent to the user of the socket object. The following example
|
Chris@16
|
1283 * illustrates how Linux's @c sendfile system call might be encapsulated:
|
Chris@16
|
1284 * @code template <typename Handler>
|
Chris@16
|
1285 * struct sendfile_op
|
Chris@16
|
1286 * {
|
Chris@16
|
1287 * tcp::socket& sock_;
|
Chris@16
|
1288 * int fd_;
|
Chris@16
|
1289 * Handler handler_;
|
Chris@16
|
1290 * off_t offset_;
|
Chris@16
|
1291 * std::size_t total_bytes_transferred_;
|
Chris@16
|
1292 *
|
Chris@16
|
1293 * // Function call operator meeting WriteHandler requirements.
|
Chris@16
|
1294 * // Used as the handler for the async_write_some operation.
|
Chris@16
|
1295 * void operator()(boost::system::error_code ec, std::size_t)
|
Chris@16
|
1296 * {
|
Chris@16
|
1297 * // Put the underlying socket into non-blocking mode.
|
Chris@16
|
1298 * if (!ec)
|
Chris@16
|
1299 * if (!sock_.native_non_blocking())
|
Chris@16
|
1300 * sock_.native_non_blocking(true, ec);
|
Chris@16
|
1301 *
|
Chris@16
|
1302 * if (!ec)
|
Chris@16
|
1303 * {
|
Chris@16
|
1304 * for (;;)
|
Chris@16
|
1305 * {
|
Chris@16
|
1306 * // Try the system call.
|
Chris@16
|
1307 * errno = 0;
|
Chris@16
|
1308 * int n = ::sendfile(sock_.native_handle(), fd_, &offset_, 65536);
|
Chris@16
|
1309 * ec = boost::system::error_code(n < 0 ? errno : 0,
|
Chris@16
|
1310 * boost::asio::error::get_system_category());
|
Chris@16
|
1311 * total_bytes_transferred_ += ec ? 0 : n;
|
Chris@16
|
1312 *
|
Chris@16
|
1313 * // Retry operation immediately if interrupted by signal.
|
Chris@16
|
1314 * if (ec == boost::asio::error::interrupted)
|
Chris@16
|
1315 * continue;
|
Chris@16
|
1316 *
|
Chris@16
|
1317 * // Check if we need to run the operation again.
|
Chris@16
|
1318 * if (ec == boost::asio::error::would_block
|
Chris@16
|
1319 * || ec == boost::asio::error::try_again)
|
Chris@16
|
1320 * {
|
Chris@16
|
1321 * // We have to wait for the socket to become ready again.
|
Chris@16
|
1322 * sock_.async_write_some(boost::asio::null_buffers(), *this);
|
Chris@16
|
1323 * return;
|
Chris@16
|
1324 * }
|
Chris@16
|
1325 *
|
Chris@16
|
1326 * if (ec || n == 0)
|
Chris@16
|
1327 * {
|
Chris@16
|
1328 * // An error occurred, or we have reached the end of the file.
|
Chris@16
|
1329 * // Either way we must exit the loop so we can call the handler.
|
Chris@16
|
1330 * break;
|
Chris@16
|
1331 * }
|
Chris@16
|
1332 *
|
Chris@16
|
1333 * // Loop around to try calling sendfile again.
|
Chris@16
|
1334 * }
|
Chris@16
|
1335 * }
|
Chris@16
|
1336 *
|
Chris@16
|
1337 * // Pass result back to user's handler.
|
Chris@16
|
1338 * handler_(ec, total_bytes_transferred_);
|
Chris@16
|
1339 * }
|
Chris@16
|
1340 * };
|
Chris@16
|
1341 *
|
Chris@16
|
1342 * template <typename Handler>
|
Chris@16
|
1343 * void async_sendfile(tcp::socket& sock, int fd, Handler h)
|
Chris@16
|
1344 * {
|
Chris@16
|
1345 * sendfile_op<Handler> op = { sock, fd, h, 0, 0 };
|
Chris@16
|
1346 * sock.async_write_some(boost::asio::null_buffers(), op);
|
Chris@16
|
1347 * } @endcode
|
Chris@16
|
1348 */
|
Chris@16
|
1349 boost::system::error_code native_non_blocking(
|
Chris@16
|
1350 bool mode, boost::system::error_code& ec)
|
Chris@16
|
1351 {
|
Chris@16
|
1352 return this->get_service().native_non_blocking(
|
Chris@16
|
1353 this->get_implementation(), mode, ec);
|
Chris@16
|
1354 }
|
Chris@16
|
1355
|
Chris@16
|
1356 /// Get the local endpoint of the socket.
|
Chris@16
|
1357 /**
|
Chris@16
|
1358 * This function is used to obtain the locally bound endpoint of the socket.
|
Chris@16
|
1359 *
|
Chris@16
|
1360 * @returns An object that represents the local endpoint of the socket.
|
Chris@16
|
1361 *
|
Chris@16
|
1362 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
1363 *
|
Chris@16
|
1364 * @par Example
|
Chris@16
|
1365 * @code
|
Chris@16
|
1366 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
1367 * ...
|
Chris@16
|
1368 * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint();
|
Chris@16
|
1369 * @endcode
|
Chris@16
|
1370 */
|
Chris@16
|
1371 endpoint_type local_endpoint() const
|
Chris@16
|
1372 {
|
Chris@16
|
1373 boost::system::error_code ec;
|
Chris@16
|
1374 endpoint_type ep = this->get_service().local_endpoint(
|
Chris@16
|
1375 this->get_implementation(), ec);
|
Chris@16
|
1376 boost::asio::detail::throw_error(ec, "local_endpoint");
|
Chris@16
|
1377 return ep;
|
Chris@16
|
1378 }
|
Chris@16
|
1379
|
Chris@16
|
1380 /// Get the local endpoint of the socket.
|
Chris@16
|
1381 /**
|
Chris@16
|
1382 * This function is used to obtain the locally bound endpoint of the socket.
|
Chris@16
|
1383 *
|
Chris@16
|
1384 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
1385 *
|
Chris@16
|
1386 * @returns An object that represents the local endpoint of the socket.
|
Chris@16
|
1387 * Returns a default-constructed endpoint object if an error occurred.
|
Chris@16
|
1388 *
|
Chris@16
|
1389 * @par Example
|
Chris@16
|
1390 * @code
|
Chris@16
|
1391 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
1392 * ...
|
Chris@16
|
1393 * boost::system::error_code ec;
|
Chris@16
|
1394 * boost::asio::ip::tcp::endpoint endpoint = socket.local_endpoint(ec);
|
Chris@16
|
1395 * if (ec)
|
Chris@16
|
1396 * {
|
Chris@16
|
1397 * // An error occurred.
|
Chris@16
|
1398 * }
|
Chris@16
|
1399 * @endcode
|
Chris@16
|
1400 */
|
Chris@16
|
1401 endpoint_type local_endpoint(boost::system::error_code& ec) const
|
Chris@16
|
1402 {
|
Chris@16
|
1403 return this->get_service().local_endpoint(this->get_implementation(), ec);
|
Chris@16
|
1404 }
|
Chris@16
|
1405
|
Chris@16
|
1406 /// Get the remote endpoint of the socket.
|
Chris@16
|
1407 /**
|
Chris@16
|
1408 * This function is used to obtain the remote endpoint of the socket.
|
Chris@16
|
1409 *
|
Chris@16
|
1410 * @returns An object that represents the remote endpoint of the socket.
|
Chris@16
|
1411 *
|
Chris@16
|
1412 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
1413 *
|
Chris@16
|
1414 * @par Example
|
Chris@16
|
1415 * @code
|
Chris@16
|
1416 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
1417 * ...
|
Chris@16
|
1418 * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint();
|
Chris@16
|
1419 * @endcode
|
Chris@16
|
1420 */
|
Chris@16
|
1421 endpoint_type remote_endpoint() const
|
Chris@16
|
1422 {
|
Chris@16
|
1423 boost::system::error_code ec;
|
Chris@16
|
1424 endpoint_type ep = this->get_service().remote_endpoint(
|
Chris@16
|
1425 this->get_implementation(), ec);
|
Chris@16
|
1426 boost::asio::detail::throw_error(ec, "remote_endpoint");
|
Chris@16
|
1427 return ep;
|
Chris@16
|
1428 }
|
Chris@16
|
1429
|
Chris@16
|
1430 /// Get the remote endpoint of the socket.
|
Chris@16
|
1431 /**
|
Chris@16
|
1432 * This function is used to obtain the remote endpoint of the socket.
|
Chris@16
|
1433 *
|
Chris@16
|
1434 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
1435 *
|
Chris@16
|
1436 * @returns An object that represents the remote endpoint of the socket.
|
Chris@16
|
1437 * Returns a default-constructed endpoint object if an error occurred.
|
Chris@16
|
1438 *
|
Chris@16
|
1439 * @par Example
|
Chris@16
|
1440 * @code
|
Chris@16
|
1441 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
1442 * ...
|
Chris@16
|
1443 * boost::system::error_code ec;
|
Chris@16
|
1444 * boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(ec);
|
Chris@16
|
1445 * if (ec)
|
Chris@16
|
1446 * {
|
Chris@16
|
1447 * // An error occurred.
|
Chris@16
|
1448 * }
|
Chris@16
|
1449 * @endcode
|
Chris@16
|
1450 */
|
Chris@16
|
1451 endpoint_type remote_endpoint(boost::system::error_code& ec) const
|
Chris@16
|
1452 {
|
Chris@16
|
1453 return this->get_service().remote_endpoint(this->get_implementation(), ec);
|
Chris@16
|
1454 }
|
Chris@16
|
1455
|
Chris@16
|
1456 /// Disable sends or receives on the socket.
|
Chris@16
|
1457 /**
|
Chris@16
|
1458 * This function is used to disable send operations, receive operations, or
|
Chris@16
|
1459 * both.
|
Chris@16
|
1460 *
|
Chris@16
|
1461 * @param what Determines what types of operation will no longer be allowed.
|
Chris@16
|
1462 *
|
Chris@16
|
1463 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
1464 *
|
Chris@16
|
1465 * @par Example
|
Chris@16
|
1466 * Shutting down the send side of the socket:
|
Chris@16
|
1467 * @code
|
Chris@16
|
1468 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
1469 * ...
|
Chris@16
|
1470 * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send);
|
Chris@16
|
1471 * @endcode
|
Chris@16
|
1472 */
|
Chris@16
|
1473 void shutdown(shutdown_type what)
|
Chris@16
|
1474 {
|
Chris@16
|
1475 boost::system::error_code ec;
|
Chris@16
|
1476 this->get_service().shutdown(this->get_implementation(), what, ec);
|
Chris@16
|
1477 boost::asio::detail::throw_error(ec, "shutdown");
|
Chris@16
|
1478 }
|
Chris@16
|
1479
|
Chris@16
|
1480 /// Disable sends or receives on the socket.
|
Chris@16
|
1481 /**
|
Chris@16
|
1482 * This function is used to disable send operations, receive operations, or
|
Chris@16
|
1483 * both.
|
Chris@16
|
1484 *
|
Chris@16
|
1485 * @param what Determines what types of operation will no longer be allowed.
|
Chris@16
|
1486 *
|
Chris@16
|
1487 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
1488 *
|
Chris@16
|
1489 * @par Example
|
Chris@16
|
1490 * Shutting down the send side of the socket:
|
Chris@16
|
1491 * @code
|
Chris@16
|
1492 * boost::asio::ip::tcp::socket socket(io_service);
|
Chris@16
|
1493 * ...
|
Chris@16
|
1494 * boost::system::error_code ec;
|
Chris@16
|
1495 * socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
|
Chris@16
|
1496 * if (ec)
|
Chris@16
|
1497 * {
|
Chris@16
|
1498 * // An error occurred.
|
Chris@16
|
1499 * }
|
Chris@16
|
1500 * @endcode
|
Chris@16
|
1501 */
|
Chris@16
|
1502 boost::system::error_code shutdown(shutdown_type what,
|
Chris@16
|
1503 boost::system::error_code& ec)
|
Chris@16
|
1504 {
|
Chris@16
|
1505 return this->get_service().shutdown(this->get_implementation(), what, ec);
|
Chris@16
|
1506 }
|
Chris@16
|
1507
|
Chris@16
|
1508 protected:
|
Chris@16
|
1509 /// Protected destructor to prevent deletion through this type.
|
Chris@16
|
1510 ~basic_socket()
|
Chris@16
|
1511 {
|
Chris@16
|
1512 }
|
Chris@16
|
1513 };
|
Chris@16
|
1514
|
Chris@16
|
1515 } // namespace asio
|
Chris@16
|
1516 } // namespace boost
|
Chris@16
|
1517
|
Chris@16
|
1518 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
1519
|
Chris@16
|
1520 #endif // BOOST_ASIO_BASIC_SOCKET_HPP
|