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