udp.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // udp.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_UDP_HPP
  11. #define BOOST_ASIO_IP_UDP_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_datagram_socket.hpp>
  17. #include <boost/asio/ip/basic_endpoint.hpp>
  18. #include <boost/asio/ip/basic_resolver.hpp>
  19. #include <boost/asio/ip/basic_resolver_iterator.hpp>
  20. #include <boost/asio/ip/basic_resolver_query.hpp>
  21. #include <boost/asio/detail/socket_types.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace ip {
  25. /// Encapsulates the flags needed for UDP.
  26. /**
  27. * The boost::asio::ip::udp class contains flags necessary for UDP sockets.
  28. *
  29. * @par Thread Safety
  30. * @e Distinct @e objects: Safe.@n
  31. * @e Shared @e objects: Safe.
  32. *
  33. * @par Concepts:
  34. * Protocol, InternetProtocol.
  35. */
  36. class udp
  37. {
  38. public:
  39. /// The type of a UDP endpoint.
  40. typedef basic_endpoint<udp> endpoint;
  41. /// The type of a resolver query.
  42. typedef basic_resolver_query<udp> resolver_query;
  43. /// The type of a resolver iterator.
  44. typedef basic_resolver_iterator<udp> resolver_iterator;
  45. /// Construct to represent the IPv4 UDP protocol.
  46. static udp v4()
  47. {
  48. return udp(PF_INET);
  49. }
  50. /// Construct to represent the IPv6 UDP protocol.
  51. static udp v6()
  52. {
  53. return udp(PF_INET6);
  54. }
  55. /// Obtain an identifier for the type of the protocol.
  56. int type() const
  57. {
  58. return SOCK_DGRAM;
  59. }
  60. /// Obtain an identifier for the protocol.
  61. int protocol() const
  62. {
  63. return IPPROTO_UDP;
  64. }
  65. /// Obtain an identifier for the protocol family.
  66. int family() const
  67. {
  68. return family_;
  69. }
  70. /// The UDP socket type.
  71. typedef basic_datagram_socket<udp> socket;
  72. /// The UDP resolver type.
  73. typedef basic_resolver<udp> resolver;
  74. /// Compare two protocols for equality.
  75. friend bool operator==(const udp& p1, const udp& p2)
  76. {
  77. return p1.family_ == p2.family_;
  78. }
  79. /// Compare two protocols for inequality.
  80. friend bool operator!=(const udp& p1, const udp& p2)
  81. {
  82. return p1.family_ != p2.family_;
  83. }
  84. private:
  85. // Construct with a specific family.
  86. explicit udp(int family)
  87. : family_(family)
  88. {
  89. }
  90. int family_;
  91. };
  92. } // namespace ip
  93. } // namespace asio
  94. } // namespace boost
  95. #include <boost/asio/detail/pop_options.hpp>
  96. #endif // BOOST_ASIO_IP_UDP_HPP