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