1 // 2 // ip/tcp.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_IP_TCP_HPP 12 #define ASIO_IP_TCP_HPP 13 14 15 #include "asio/detail/config.hpp" 16 #include "asio/basic_socket_acceptor.hpp" 17 #include "asio/basic_socket_iostream.hpp" 18 #include "asio/basic_stream_socket.hpp" 19 #include "asio/detail/socket_option.hpp" 20 #include "asio/detail/socket_types.hpp" 21 #include "asio/ip/basic_endpoint.hpp" 22 #include "asio/ip/basic_resolver.hpp" 23 #include "asio/ip/basic_resolver_iterator.hpp" 24 #include "asio/ip/basic_resolver_query.hpp" 25 26 #include "asio/detail/push_options.hpp" 27 28 namespace asio { 29 namespace ip { 30 31 /// Encapsulates the flags needed for TCP. 32 /** 33 * The asio::ip::tcp class contains flags necessary for TCP sockets. 34 * 35 * @par Thread Safety 36 * @e Distinct @e objects: Safe.@n 37 * @e Shared @e objects: Safe. 38 * 39 * @par Concepts: 40 * Protocol, InternetProtocol. 41 */ 42 class tcp 43 { 44 public: 45 /// The type of a TCP endpoint. 46 typedef basic_endpoint<tcp> endpoint; 47 48 /// Construct to represent the IPv4 TCP protocol. 49 static tcp v4() 50 { 51 return tcp(ASIO_OS_DEF(AF_INET)); 52 } 53 54 /// Construct to represent the IPv6 TCP protocol. 55 static tcp v6() 56 { 57 return tcp(ASIO_OS_DEF(AF_INET6)); 58 } 59 60 /// Obtain an identifier for the type of the protocol. 61 int type() const 62 { 63 return ASIO_OS_DEF(SOCK_STREAM); 64 } 65 66 /// Obtain an identifier for the protocol. 67 int protocol() const 68 { 69 return ASIO_OS_DEF(IPPROTO_TCP); 70 } 71 72 /// Obtain an identifier for the protocol family. 73 int family() const 74 { 75 return family_; 76 } 77 78 /// The TCP socket type. 79 typedef basic_stream_socket<tcp> socket; 80 81 /// The TCP acceptor type. 82 typedef basic_socket_acceptor<tcp> acceptor; 83 84 /// The TCP resolver type. 85 typedef basic_resolver<tcp> resolver; 86 87 88 /// Socket option for disabling the Nagle algorithm. 89 /** 90 * Implements the IPPROTO_TCP/TCP_NODELAY socket option. 91 * 92 * @par Examples 93 * Setting the option: 94 * @code 95 * asio::ip::tcp::socket socket(io_service); 96 * ... 97 * asio::ip::tcp::no_delay option(true); 98 * socket.set_option(option); 99 * @endcode 100 * 101 * @par 102 * Getting the current option value: 103 * @code 104 * asio::ip::tcp::socket socket(io_service); 105 * ... 106 * asio::ip::tcp::no_delay option; 107 * socket.get_option(option); 108 * bool is_set = option.value(); 109 * @endcode 110 * 111 * @par Concepts: 112 * Socket_Option, Boolean_Socket_Option. 113 */ 114 typedef asio::detail::socket_option::boolean< 115 ASIO_OS_DEF(IPPROTO_TCP), ASIO_OS_DEF(TCP_NODELAY)> no_delay; 116 117 /// Compare two protocols for equality. 118 friend bool operator==(const tcp& p1, const tcp& p2) 119 { 120 return p1.family_ == p2.family_; 121 } 122 123 /// Compare two protocols for inequality. 124 friend bool operator!=(const tcp& p1, const tcp& p2) 125 { 126 return p1.family_ != p2.family_; 127 } 128 129 private: 130 // Construct with a specific family. 131 explicit tcp(int protocol_family) 132 : family_(protocol_family) 133 { 134 } 135 136 int family_; 137 }; 138 139 } // namespace ip 140 } // namespace asio 141 142 #include "asio/detail/pop_options.hpp" 143 144 #endif // ASIO_IP_TCP_HPP 145