tcp.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // ip/tcp.hpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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 ASIO_IP_TCP_HPP
  11. #define 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 "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. #include "asio/detail/push_options.hpp"
  26. namespace asio {
  27. namespace ip {
  28. /// Encapsulates the flags needed for TCP.
  29. /**
  30. * The 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. /// (Deprecated: use resolver::query.) The type of a resolver query.
  45. typedef basic_resolver_query<tcp> resolver_query;
  46. /// (Deprecated: use resolver::iterator.) 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. #if !defined(BOOST_NO_IOSTREAM)
  80. /// The TCP iostream type.
  81. typedef basic_socket_iostream<tcp> iostream;
  82. #endif // !defined(BOOST_NO_IOSTREAM)
  83. /// Socket option for disabling the Nagle algorithm.
  84. /**
  85. * Implements the IPPROTO_TCP/TCP_NODELAY socket option.
  86. *
  87. * @par Examples
  88. * Setting the option:
  89. * @code
  90. * asio::ip::tcp::socket socket(io_service);
  91. * ...
  92. * asio::ip::tcp::no_delay option(true);
  93. * socket.set_option(option);
  94. * @endcode
  95. *
  96. * @par
  97. * Getting the current option value:
  98. * @code
  99. * asio::ip::tcp::socket socket(io_service);
  100. * ...
  101. * asio::ip::tcp::no_delay option;
  102. * socket.get_option(option);
  103. * bool is_set = option.value();
  104. * @endcode
  105. *
  106. * @par Concepts:
  107. * Socket_Option, Boolean_Socket_Option.
  108. */
  109. #if defined(GENERATING_DOCUMENTATION)
  110. typedef implementation_defined no_delay;
  111. #else
  112. typedef asio::detail::socket_option::boolean<
  113. IPPROTO_TCP, TCP_NODELAY> no_delay;
  114. #endif
  115. /// Compare two protocols for equality.
  116. friend bool operator==(const tcp& p1, const tcp& p2)
  117. {
  118. return p1.family_ == p2.family_;
  119. }
  120. /// Compare two protocols for inequality.
  121. friend bool operator!=(const tcp& p1, const tcp& p2)
  122. {
  123. return p1.family_ != p2.family_;
  124. }
  125. private:
  126. // Construct with a specific family.
  127. explicit tcp(int family)
  128. : family_(family)
  129. {
  130. }
  131. int family_;
  132. };
  133. } // namespace ip
  134. } // namespace asio
  135. #include "asio/detail/pop_options.hpp"
  136. #endif // ASIO_IP_TCP_HPP