tcp.hpp 3.8 KB

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