5 // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_SOCKET_BASE_HPP
12 #define BOOST_ASIO_SOCKET_BASE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/detail/workaround.hpp>
20 #include <boost/asio/detail/io_control.hpp>
21 #include <boost/asio/detail/socket_option.hpp>
22 #include <boost/asio/detail/socket_types.hpp>
24 #include <boost/asio/detail/push_options.hpp>
29 /// The socket_base class is used as a base for the basic_stream_socket and
30 /// basic_datagram_socket class templates so that we have a common place to
31 /// define the shutdown_type and enum.
35 /// Different ways a socket may be shutdown.
38 #if defined(GENERATING_DOCUMENTATION)
39 /// Shutdown the receive side of the socket.
40 shutdown_receive = implementation_defined,
42 /// Shutdown the send side of the socket.
43 shutdown_send = implementation_defined,
45 /// Shutdown both send and receive on the socket.
46 shutdown_both = implementation_defined
48 shutdown_receive = boost::asio::detail::shutdown_receive,
49 shutdown_send = boost::asio::detail::shutdown_send,
50 shutdown_both = boost::asio::detail::shutdown_both
54 /// Bitmask type for flags that can be passed to send and receive operations.
55 typedef int message_flags;
57 #if defined(GENERATING_DOCUMENTATION)
58 /// Peek at incoming data without removing it from the input queue.
59 static const int message_peek = implementation_defined;
61 /// Process out-of-band data.
62 static const int message_out_of_band = implementation_defined;
64 /// Specify that the data should not be subject to routing.
65 static const int message_do_not_route = implementation_defined;
67 /// Specifies that the data marks the end of a record.
68 static const int message_end_of_record = implementation_defined;
70 BOOST_STATIC_CONSTANT(int,
71 message_peek = boost::asio::detail::message_peek);
72 BOOST_STATIC_CONSTANT(int,
73 message_out_of_band = boost::asio::detail::message_out_of_band);
74 BOOST_STATIC_CONSTANT(int,
75 message_do_not_route = boost::asio::detail::message_do_not_route);
76 BOOST_STATIC_CONSTANT(int,
77 message_end_of_record = boost::asio::detail::message_end_of_record);
80 /// Socket option to permit sending of broadcast messages.
82 * Implements the SOL_SOCKET/SO_BROADCAST socket option.
87 * boost::asio::ip::udp::socket socket(io_service);
89 * boost::asio::socket_base::broadcast option(true);
90 * socket.set_option(option);
94 * Getting the current option value:
96 * boost::asio::ip::udp::socket socket(io_service);
98 * boost::asio::socket_base::broadcast option;
99 * socket.get_option(option);
100 * bool is_set = option.value();
104 * Socket_Option, Boolean_Socket_Option.
106 #if defined(GENERATING_DOCUMENTATION)
107 typedef implementation_defined broadcast;
109 typedef boost::asio::detail::socket_option::boolean<
110 SOL_SOCKET, SO_BROADCAST> broadcast;
113 /// Socket option to enable socket-level debugging.
115 * Implements the SOL_SOCKET/SO_DEBUG socket option.
118 * Setting the option:
120 * boost::asio::ip::tcp::socket socket(io_service);
122 * boost::asio::socket_base::debug option(true);
123 * socket.set_option(option);
127 * Getting the current option value:
129 * boost::asio::ip::tcp::socket socket(io_service);
131 * boost::asio::socket_base::debug option;
132 * socket.get_option(option);
133 * bool is_set = option.value();
137 * Socket_Option, Boolean_Socket_Option.
139 #if defined(GENERATING_DOCUMENTATION)
140 typedef implementation_defined debug;
142 typedef boost::asio::detail::socket_option::boolean<
143 SOL_SOCKET, SO_DEBUG> debug;
146 /// Socket option to prevent routing, use local interfaces only.
148 * Implements the SOL_SOCKET/SO_DONTROUTE socket option.
151 * Setting the option:
153 * boost::asio::ip::udp::socket socket(io_service);
155 * boost::asio::socket_base::do_not_route option(true);
156 * socket.set_option(option);
160 * Getting the current option value:
162 * boost::asio::ip::udp::socket socket(io_service);
164 * boost::asio::socket_base::do_not_route option;
165 * socket.get_option(option);
166 * bool is_set = option.value();
170 * Socket_Option, Boolean_Socket_Option.
172 #if defined(GENERATING_DOCUMENTATION)
173 typedef implementation_defined do_not_route;
175 typedef boost::asio::detail::socket_option::boolean<
176 SOL_SOCKET, SO_DONTROUTE> do_not_route;
179 /// Socket option to send keep-alives.
181 * Implements the SOL_SOCKET/SO_KEEPALIVE socket option.
184 * Setting the option:
186 * boost::asio::ip::tcp::socket socket(io_service);
188 * boost::asio::socket_base::keep_alive option(true);
189 * socket.set_option(option);
193 * Getting the current option value:
195 * boost::asio::ip::tcp::socket socket(io_service);
197 * boost::asio::socket_base::keep_alive option;
198 * socket.get_option(option);
199 * bool is_set = option.value();
203 * Socket_Option, Boolean_Socket_Option.
205 #if defined(GENERATING_DOCUMENTATION)
206 typedef implementation_defined keep_alive;
208 typedef boost::asio::detail::socket_option::boolean<
209 SOL_SOCKET, SO_KEEPALIVE> keep_alive;
212 /// Socket option for the send buffer size of a socket.
214 * Implements the SOL_SOCKET/SO_SNDBUF socket option.
217 * Setting the option:
219 * boost::asio::ip::tcp::socket socket(io_service);
221 * boost::asio::socket_base::send_buffer_size option(8192);
222 * socket.set_option(option);
226 * Getting the current option value:
228 * boost::asio::ip::tcp::socket socket(io_service);
230 * boost::asio::socket_base::send_buffer_size option;
231 * socket.get_option(option);
232 * int size = option.value();
236 * Socket_Option, Integer_Socket_Option.
238 #if defined(GENERATING_DOCUMENTATION)
239 typedef implementation_defined send_buffer_size;
241 typedef boost::asio::detail::socket_option::integer<
242 SOL_SOCKET, SO_SNDBUF> send_buffer_size;
245 /// Socket option for the send low watermark.
247 * Implements the SOL_SOCKET/SO_SNDLOWAT socket option.
250 * Setting the option:
252 * boost::asio::ip::tcp::socket socket(io_service);
254 * boost::asio::socket_base::send_low_watermark option(1024);
255 * socket.set_option(option);
259 * Getting the current option value:
261 * boost::asio::ip::tcp::socket socket(io_service);
263 * boost::asio::socket_base::send_low_watermark option;
264 * socket.get_option(option);
265 * int size = option.value();
269 * Socket_Option, Integer_Socket_Option.
271 #if defined(GENERATING_DOCUMENTATION)
272 typedef implementation_defined send_low_watermark;
274 typedef boost::asio::detail::socket_option::integer<
275 SOL_SOCKET, SO_SNDLOWAT> send_low_watermark;
278 /// Socket option for the receive buffer size of a socket.
280 * Implements the SOL_SOCKET/SO_RCVBUF socket option.
283 * Setting the option:
285 * boost::asio::ip::tcp::socket socket(io_service);
287 * boost::asio::socket_base::receive_buffer_size option(8192);
288 * socket.set_option(option);
292 * Getting the current option value:
294 * boost::asio::ip::tcp::socket socket(io_service);
296 * boost::asio::socket_base::receive_buffer_size option;
297 * socket.get_option(option);
298 * int size = option.value();
302 * Socket_Option, Integer_Socket_Option.
304 #if defined(GENERATING_DOCUMENTATION)
305 typedef implementation_defined receive_buffer_size;
307 typedef boost::asio::detail::socket_option::integer<
308 SOL_SOCKET, SO_RCVBUF> receive_buffer_size;
311 /// Socket option for the receive low watermark.
313 * Implements the SOL_SOCKET/SO_RCVLOWAT socket option.
316 * Setting the option:
318 * boost::asio::ip::tcp::socket socket(io_service);
320 * boost::asio::socket_base::receive_low_watermark option(1024);
321 * socket.set_option(option);
325 * Getting the current option value:
327 * boost::asio::ip::tcp::socket socket(io_service);
329 * boost::asio::socket_base::receive_low_watermark option;
330 * socket.get_option(option);
331 * int size = option.value();
335 * Socket_Option, Integer_Socket_Option.
337 #if defined(GENERATING_DOCUMENTATION)
338 typedef implementation_defined receive_low_watermark;
340 typedef boost::asio::detail::socket_option::integer<
341 SOL_SOCKET, SO_RCVLOWAT> receive_low_watermark;
344 /// Socket option to allow the socket to be bound to an address that is
347 * Implements the SOL_SOCKET/SO_REUSEADDR socket option.
350 * Setting the option:
352 * boost::asio::ip::tcp::acceptor acceptor(io_service);
354 * boost::asio::socket_base::reuse_address option(true);
355 * acceptor.set_option(option);
359 * Getting the current option value:
361 * boost::asio::ip::tcp::acceptor acceptor(io_service);
363 * boost::asio::socket_base::reuse_address option;
364 * acceptor.get_option(option);
365 * bool is_set = option.value();
369 * Socket_Option, Boolean_Socket_Option.
371 #if defined(GENERATING_DOCUMENTATION)
372 typedef implementation_defined reuse_address;
374 typedef boost::asio::detail::socket_option::boolean<
375 SOL_SOCKET, SO_REUSEADDR> reuse_address;
378 /// Socket option to specify whether the socket lingers on close if unsent
381 * Implements the SOL_SOCKET/SO_LINGER socket option.
384 * Setting the option:
386 * boost::asio::ip::tcp::socket socket(io_service);
388 * boost::asio::socket_base::linger option(true, 30);
389 * socket.set_option(option);
393 * Getting the current option value:
395 * boost::asio::ip::tcp::socket socket(io_service);
397 * boost::asio::socket_base::linger option;
398 * socket.get_option(option);
399 * bool is_set = option.enabled();
400 * unsigned short timeout = option.timeout();
404 * Socket_Option, Linger_Socket_Option.
406 #if defined(GENERATING_DOCUMENTATION)
407 typedef implementation_defined linger;
409 typedef boost::asio::detail::socket_option::linger<
410 SOL_SOCKET, SO_LINGER> linger;
413 /// Socket option to report aborted connections on accept.
415 * Implements a custom socket option that determines whether or not an accept
416 * operation is permitted to fail with boost::asio::error::connection_aborted.
417 * By default the option is false.
420 * Setting the option:
422 * boost::asio::ip::tcp::acceptor acceptor(io_service);
424 * boost::asio::socket_base::enable_connection_aborted option(true);
425 * acceptor.set_option(option);
429 * Getting the current option value:
431 * boost::asio::ip::tcp::acceptor acceptor(io_service);
433 * boost::asio::socket_base::enable_connection_aborted option;
434 * acceptor.get_option(option);
435 * bool is_set = option.value();
439 * Socket_Option, Boolean_Socket_Option.
441 #if defined(GENERATING_DOCUMENTATION)
442 typedef implementation_defined enable_connection_aborted;
444 typedef boost::asio::detail::socket_option::boolean<
445 boost::asio::detail::custom_socket_option_level,
446 boost::asio::detail::enable_connection_aborted_option>
447 enable_connection_aborted;
450 /// (Deprecated: Use non_blocking().) IO control command to
451 /// set the blocking mode of the socket.
453 * Implements the FIONBIO IO control command.
457 * boost::asio::ip::tcp::socket socket(io_service);
459 * boost::asio::socket_base::non_blocking_io command(true);
460 * socket.io_control(command);
464 * IO_Control_Command, Boolean_IO_Control_Command.
466 #if defined(GENERATING_DOCUMENTATION)
467 typedef implementation_defined non_blocking_io;
469 typedef boost::asio::detail::io_control::non_blocking_io non_blocking_io;
472 /// IO control command to get the amount of data that can be read without
475 * Implements the FIONREAD IO control command.
479 * boost::asio::ip::tcp::socket socket(io_service);
481 * boost::asio::socket_base::bytes_readable command(true);
482 * socket.io_control(command);
483 * std::size_t bytes_readable = command.get();
487 * IO_Control_Command, Size_IO_Control_Command.
489 #if defined(GENERATING_DOCUMENTATION)
490 typedef implementation_defined bytes_readable;
492 typedef boost::asio::detail::io_control::bytes_readable bytes_readable;
495 /// The maximum length of the queue of pending incoming connections.
496 #if defined(GENERATING_DOCUMENTATION)
497 static const int max_connections = implementation_defined;
499 BOOST_STATIC_CONSTANT(int, max_connections = SOMAXCONN);
503 /// Protected destructor to prevent deletion through this type.
508 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
510 // Workaround to enable the empty base optimisation with Borland C++.
518 #include <boost/asio/detail/pop_options.hpp>
520 #endif // BOOST_ASIO_SOCKET_BASE_HPP