e034d679b2ff562cf5071c7a48a3385091129705
[platform/upstream/boost.git] / boost / asio / socket_base.hpp
1 //
2 // socket_base.hpp
3 // ~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #ifndef BOOST_ASIO_SOCKET_BASE_HPP
12 #define BOOST_ASIO_SOCKET_BASE_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/io_control.hpp>
20 #include <boost/asio/detail/socket_option.hpp>
21 #include <boost/asio/detail/socket_types.hpp>
22
23 #include <boost/asio/detail/push_options.hpp>
24
25 namespace boost {
26 namespace asio {
27
28 /// The socket_base class is used as a base for the basic_stream_socket and
29 /// basic_datagram_socket class templates so that we have a common place to
30 /// define the shutdown_type and enum.
31 class socket_base
32 {
33 public:
34   /// Different ways a socket may be shutdown.
35   enum shutdown_type
36   {
37 #if defined(GENERATING_DOCUMENTATION)
38     /// Shutdown the receive side of the socket.
39     shutdown_receive = implementation_defined,
40
41     /// Shutdown the send side of the socket.
42     shutdown_send = implementation_defined,
43
44     /// Shutdown both send and receive on the socket.
45     shutdown_both = implementation_defined
46 #else
47     shutdown_receive = BOOST_ASIO_OS_DEF(SHUT_RD),
48     shutdown_send = BOOST_ASIO_OS_DEF(SHUT_WR),
49     shutdown_both = BOOST_ASIO_OS_DEF(SHUT_RDWR)
50 #endif
51   };
52
53   /// Bitmask type for flags that can be passed to send and receive operations.
54   typedef int message_flags;
55
56 #if defined(GENERATING_DOCUMENTATION)
57   /// Peek at incoming data without removing it from the input queue.
58   static const int message_peek = implementation_defined;
59
60   /// Process out-of-band data.
61   static const int message_out_of_band = implementation_defined;
62
63   /// Specify that the data should not be subject to routing.
64   static const int message_do_not_route = implementation_defined;
65
66   /// Specifies that the data marks the end of a record.
67   static const int message_end_of_record = implementation_defined;
68 #else
69   BOOST_ASIO_STATIC_CONSTANT(int,
70       message_peek = BOOST_ASIO_OS_DEF(MSG_PEEK));
71   BOOST_ASIO_STATIC_CONSTANT(int,
72       message_out_of_band = BOOST_ASIO_OS_DEF(MSG_OOB));
73   BOOST_ASIO_STATIC_CONSTANT(int,
74       message_do_not_route = BOOST_ASIO_OS_DEF(MSG_DONTROUTE));
75   BOOST_ASIO_STATIC_CONSTANT(int,
76       message_end_of_record = BOOST_ASIO_OS_DEF(MSG_EOR));
77 #endif
78
79   /// Socket option to permit sending of broadcast messages.
80   /**
81    * Implements the SOL_SOCKET/SO_BROADCAST socket option.
82    *
83    * @par Examples
84    * Setting the option:
85    * @code
86    * boost::asio::ip::udp::socket socket(io_service); 
87    * ...
88    * boost::asio::socket_base::broadcast option(true);
89    * socket.set_option(option);
90    * @endcode
91    *
92    * @par
93    * Getting the current option value:
94    * @code
95    * boost::asio::ip::udp::socket socket(io_service); 
96    * ...
97    * boost::asio::socket_base::broadcast option;
98    * socket.get_option(option);
99    * bool is_set = option.value();
100    * @endcode
101    *
102    * @par Concepts:
103    * Socket_Option, Boolean_Socket_Option.
104    */
105 #if defined(GENERATING_DOCUMENTATION)
106   typedef implementation_defined broadcast;
107 #else
108   typedef boost::asio::detail::socket_option::boolean<
109     BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_BROADCAST)>
110       broadcast;
111 #endif
112
113   /// Socket option to enable socket-level debugging.
114   /**
115    * Implements the SOL_SOCKET/SO_DEBUG socket option.
116    *
117    * @par Examples
118    * Setting the option:
119    * @code
120    * boost::asio::ip::tcp::socket socket(io_service); 
121    * ...
122    * boost::asio::socket_base::debug option(true);
123    * socket.set_option(option);
124    * @endcode
125    *
126    * @par
127    * Getting the current option value:
128    * @code
129    * boost::asio::ip::tcp::socket socket(io_service); 
130    * ...
131    * boost::asio::socket_base::debug option;
132    * socket.get_option(option);
133    * bool is_set = option.value();
134    * @endcode
135    *
136    * @par Concepts:
137    * Socket_Option, Boolean_Socket_Option.
138    */
139 #if defined(GENERATING_DOCUMENTATION)
140   typedef implementation_defined debug;
141 #else
142   typedef boost::asio::detail::socket_option::boolean<
143     BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_DEBUG)> debug;
144 #endif
145
146   /// Socket option to prevent routing, use local interfaces only.
147   /**
148    * Implements the SOL_SOCKET/SO_DONTROUTE socket option.
149    *
150    * @par Examples
151    * Setting the option:
152    * @code
153    * boost::asio::ip::udp::socket socket(io_service); 
154    * ...
155    * boost::asio::socket_base::do_not_route option(true);
156    * socket.set_option(option);
157    * @endcode
158    *
159    * @par
160    * Getting the current option value:
161    * @code
162    * boost::asio::ip::udp::socket socket(io_service); 
163    * ...
164    * boost::asio::socket_base::do_not_route option;
165    * socket.get_option(option);
166    * bool is_set = option.value();
167    * @endcode
168    *
169    * @par Concepts:
170    * Socket_Option, Boolean_Socket_Option.
171    */
172 #if defined(GENERATING_DOCUMENTATION)
173   typedef implementation_defined do_not_route;
174 #else
175   typedef boost::asio::detail::socket_option::boolean<
176     BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_DONTROUTE)>
177       do_not_route;
178 #endif
179
180   /// Socket option to send keep-alives.
181   /**
182    * Implements the SOL_SOCKET/SO_KEEPALIVE socket option.
183    *
184    * @par Examples
185    * Setting the option:
186    * @code
187    * boost::asio::ip::tcp::socket socket(io_service); 
188    * ...
189    * boost::asio::socket_base::keep_alive option(true);
190    * socket.set_option(option);
191    * @endcode
192    *
193    * @par
194    * Getting the current option value:
195    * @code
196    * boost::asio::ip::tcp::socket socket(io_service); 
197    * ...
198    * boost::asio::socket_base::keep_alive option;
199    * socket.get_option(option);
200    * bool is_set = option.value();
201    * @endcode
202    *
203    * @par Concepts:
204    * Socket_Option, Boolean_Socket_Option.
205    */
206 #if defined(GENERATING_DOCUMENTATION)
207   typedef implementation_defined keep_alive;
208 #else
209   typedef boost::asio::detail::socket_option::boolean<
210     BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_KEEPALIVE)> keep_alive;
211 #endif
212
213   /// Socket option for the send buffer size of a socket.
214   /**
215    * Implements the SOL_SOCKET/SO_SNDBUF socket option.
216    *
217    * @par Examples
218    * Setting the option:
219    * @code
220    * boost::asio::ip::tcp::socket socket(io_service); 
221    * ...
222    * boost::asio::socket_base::send_buffer_size option(8192);
223    * socket.set_option(option);
224    * @endcode
225    *
226    * @par
227    * Getting the current option value:
228    * @code
229    * boost::asio::ip::tcp::socket socket(io_service); 
230    * ...
231    * boost::asio::socket_base::send_buffer_size option;
232    * socket.get_option(option);
233    * int size = option.value();
234    * @endcode
235    *
236    * @par Concepts:
237    * Socket_Option, Integer_Socket_Option.
238    */
239 #if defined(GENERATING_DOCUMENTATION)
240   typedef implementation_defined send_buffer_size;
241 #else
242   typedef boost::asio::detail::socket_option::integer<
243     BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_SNDBUF)>
244       send_buffer_size;
245 #endif
246
247   /// Socket option for the send low watermark.
248   /**
249    * Implements the SOL_SOCKET/SO_SNDLOWAT socket option.
250    *
251    * @par Examples
252    * Setting the option:
253    * @code
254    * boost::asio::ip::tcp::socket socket(io_service); 
255    * ...
256    * boost::asio::socket_base::send_low_watermark option(1024);
257    * socket.set_option(option);
258    * @endcode
259    *
260    * @par
261    * Getting the current option value:
262    * @code
263    * boost::asio::ip::tcp::socket socket(io_service); 
264    * ...
265    * boost::asio::socket_base::send_low_watermark option;
266    * socket.get_option(option);
267    * int size = option.value();
268    * @endcode
269    *
270    * @par Concepts:
271    * Socket_Option, Integer_Socket_Option.
272    */
273 #if defined(GENERATING_DOCUMENTATION)
274   typedef implementation_defined send_low_watermark;
275 #else
276   typedef boost::asio::detail::socket_option::integer<
277     BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_SNDLOWAT)>
278       send_low_watermark;
279 #endif
280
281   /// Socket option for the receive buffer size of a socket.
282   /**
283    * Implements the SOL_SOCKET/SO_RCVBUF socket option.
284    *
285    * @par Examples
286    * Setting the option:
287    * @code
288    * boost::asio::ip::tcp::socket socket(io_service); 
289    * ...
290    * boost::asio::socket_base::receive_buffer_size option(8192);
291    * socket.set_option(option);
292    * @endcode
293    *
294    * @par
295    * Getting the current option value:
296    * @code
297    * boost::asio::ip::tcp::socket socket(io_service); 
298    * ...
299    * boost::asio::socket_base::receive_buffer_size option;
300    * socket.get_option(option);
301    * int size = option.value();
302    * @endcode
303    *
304    * @par Concepts:
305    * Socket_Option, Integer_Socket_Option.
306    */
307 #if defined(GENERATING_DOCUMENTATION)
308   typedef implementation_defined receive_buffer_size;
309 #else
310   typedef boost::asio::detail::socket_option::integer<
311     BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_RCVBUF)>
312       receive_buffer_size;
313 #endif
314
315   /// Socket option for the receive low watermark.
316   /**
317    * Implements the SOL_SOCKET/SO_RCVLOWAT socket option.
318    *
319    * @par Examples
320    * Setting the option:
321    * @code
322    * boost::asio::ip::tcp::socket socket(io_service); 
323    * ...
324    * boost::asio::socket_base::receive_low_watermark option(1024);
325    * socket.set_option(option);
326    * @endcode
327    *
328    * @par
329    * Getting the current option value:
330    * @code
331    * boost::asio::ip::tcp::socket socket(io_service); 
332    * ...
333    * boost::asio::socket_base::receive_low_watermark option;
334    * socket.get_option(option);
335    * int size = option.value();
336    * @endcode
337    *
338    * @par Concepts:
339    * Socket_Option, Integer_Socket_Option.
340    */
341 #if defined(GENERATING_DOCUMENTATION)
342   typedef implementation_defined receive_low_watermark;
343 #else
344   typedef boost::asio::detail::socket_option::integer<
345     BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_RCVLOWAT)>
346       receive_low_watermark;
347 #endif
348
349   /// Socket option to allow the socket to be bound to an address that is
350   /// already in use.
351   /**
352    * Implements the SOL_SOCKET/SO_REUSEADDR socket option.
353    *
354    * @par Examples
355    * Setting the option:
356    * @code
357    * boost::asio::ip::tcp::acceptor acceptor(io_service); 
358    * ...
359    * boost::asio::socket_base::reuse_address option(true);
360    * acceptor.set_option(option);
361    * @endcode
362    *
363    * @par
364    * Getting the current option value:
365    * @code
366    * boost::asio::ip::tcp::acceptor acceptor(io_service); 
367    * ...
368    * boost::asio::socket_base::reuse_address option;
369    * acceptor.get_option(option);
370    * bool is_set = option.value();
371    * @endcode
372    *
373    * @par Concepts:
374    * Socket_Option, Boolean_Socket_Option.
375    */
376 #if defined(GENERATING_DOCUMENTATION)
377   typedef implementation_defined reuse_address;
378 #else
379   typedef boost::asio::detail::socket_option::boolean<
380     BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_REUSEADDR)>
381       reuse_address;
382 #endif
383
384   /// Socket option to specify whether the socket lingers on close if unsent
385   /// data is present.
386   /**
387    * Implements the SOL_SOCKET/SO_LINGER socket option.
388    *
389    * @par Examples
390    * Setting the option:
391    * @code
392    * boost::asio::ip::tcp::socket socket(io_service); 
393    * ...
394    * boost::asio::socket_base::linger option(true, 30);
395    * socket.set_option(option);
396    * @endcode
397    *
398    * @par
399    * Getting the current option value:
400    * @code
401    * boost::asio::ip::tcp::socket socket(io_service); 
402    * ...
403    * boost::asio::socket_base::linger option;
404    * socket.get_option(option);
405    * bool is_set = option.enabled();
406    * unsigned short timeout = option.timeout();
407    * @endcode
408    *
409    * @par Concepts:
410    * Socket_Option, Linger_Socket_Option.
411    */
412 #if defined(GENERATING_DOCUMENTATION)
413   typedef implementation_defined linger;
414 #else
415   typedef boost::asio::detail::socket_option::linger<
416     BOOST_ASIO_OS_DEF(SOL_SOCKET), BOOST_ASIO_OS_DEF(SO_LINGER)>
417       linger;
418 #endif
419
420   /// Socket option to report aborted connections on accept.
421   /**
422    * Implements a custom socket option that determines whether or not an accept
423    * operation is permitted to fail with boost::asio::error::connection_aborted.
424    * By default the option is false.
425    *
426    * @par Examples
427    * Setting the option:
428    * @code
429    * boost::asio::ip::tcp::acceptor acceptor(io_service); 
430    * ...
431    * boost::asio::socket_base::enable_connection_aborted option(true);
432    * acceptor.set_option(option);
433    * @endcode
434    *
435    * @par
436    * Getting the current option value:
437    * @code
438    * boost::asio::ip::tcp::acceptor acceptor(io_service); 
439    * ...
440    * boost::asio::socket_base::enable_connection_aborted option;
441    * acceptor.get_option(option);
442    * bool is_set = option.value();
443    * @endcode
444    *
445    * @par Concepts:
446    * Socket_Option, Boolean_Socket_Option.
447    */
448 #if defined(GENERATING_DOCUMENTATION)
449   typedef implementation_defined enable_connection_aborted;
450 #else
451   typedef boost::asio::detail::socket_option::boolean<
452     boost::asio::detail::custom_socket_option_level,
453     boost::asio::detail::enable_connection_aborted_option>
454     enable_connection_aborted;
455 #endif
456
457   /// (Deprecated: Use non_blocking().) IO control command to
458   /// set the blocking mode of the socket.
459   /**
460    * Implements the FIONBIO IO control command.
461    *
462    * @par Example
463    * @code
464    * boost::asio::ip::tcp::socket socket(io_service); 
465    * ...
466    * boost::asio::socket_base::non_blocking_io command(true);
467    * socket.io_control(command);
468    * @endcode
469    *
470    * @par Concepts:
471    * IO_Control_Command, Boolean_IO_Control_Command.
472    */
473 #if defined(GENERATING_DOCUMENTATION)
474   typedef implementation_defined non_blocking_io;
475 #else
476   typedef boost::asio::detail::io_control::non_blocking_io non_blocking_io;
477 #endif
478
479   /// IO control command to get the amount of data that can be read without
480   /// blocking.
481   /**
482    * Implements the FIONREAD IO control command.
483    *
484    * @par Example
485    * @code
486    * boost::asio::ip::tcp::socket socket(io_service); 
487    * ...
488    * boost::asio::socket_base::bytes_readable command(true);
489    * socket.io_control(command);
490    * std::size_t bytes_readable = command.get();
491    * @endcode
492    *
493    * @par Concepts:
494    * IO_Control_Command, Size_IO_Control_Command.
495    */
496 #if defined(GENERATING_DOCUMENTATION)
497   typedef implementation_defined bytes_readable;
498 #else
499   typedef boost::asio::detail::io_control::bytes_readable bytes_readable;
500 #endif
501
502   /// The maximum length of the queue of pending incoming connections.
503 #if defined(GENERATING_DOCUMENTATION)
504   static const int max_connections = implementation_defined;
505 #else
506   BOOST_ASIO_STATIC_CONSTANT(int, max_connections
507       = BOOST_ASIO_OS_DEF(SOMAXCONN));
508 #endif
509
510 protected:
511   /// Protected destructor to prevent deletion through this type.
512   ~socket_base()
513   {
514   }
515 };
516
517 } // namespace asio
518 } // namespace boost
519
520 #include <boost/asio/detail/pop_options.hpp>
521
522 #endif // BOOST_ASIO_SOCKET_BASE_HPP