tcp.hpp 3.6 KB

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