tcp.hpp 3.7 KB

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