endpoint.ipp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // generic/detail/impl/endpoint.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_GENERIC_DETAIL_IMPL_ENDPOINT_IPP
  11. #define BOOST_ASIO_GENERIC_DETAIL_IMPL_ENDPOINT_IPP
  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 <cstring>
  17. #include <typeinfo>
  18. #include <boost/asio/detail/socket_ops.hpp>
  19. #include <boost/asio/detail/throw_error.hpp>
  20. #include <boost/asio/detail/throw_exception.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/generic/detail/endpoint.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace generic {
  27. namespace detail {
  28. endpoint::endpoint()
  29. {
  30. init(0, 0, 0);
  31. }
  32. endpoint::endpoint(const void* sock_addr,
  33. std::size_t sock_addr_size, int sock_protocol)
  34. {
  35. init(sock_addr, sock_addr_size, sock_protocol);
  36. }
  37. void endpoint::resize(std::size_t new_size)
  38. {
  39. if (new_size > sizeof(boost::asio::detail::sockaddr_storage_type))
  40. {
  41. boost::system::error_code ec(boost::asio::error::invalid_argument);
  42. boost::asio::detail::throw_error(ec);
  43. }
  44. else
  45. {
  46. size_ = new_size;
  47. protocol_ = 0;
  48. }
  49. }
  50. bool operator==(const endpoint& e1, const endpoint& e2)
  51. {
  52. using namespace std; // For memcmp.
  53. return e1.size() == e2.size() && memcmp(e1.data(), e2.data(), e1.size()) == 0;
  54. }
  55. bool operator<(const endpoint& e1, const endpoint& e2)
  56. {
  57. if (e1.protocol() < e2.protocol())
  58. return true;
  59. if (e1.protocol() > e2.protocol())
  60. return false;
  61. using namespace std; // For memcmp.
  62. std::size_t compare_size = e1.size() < e2.size() ? e1.size() : e2.size();
  63. int compare_result = memcmp(e1.data(), e2.data(), compare_size);
  64. if (compare_result < 0)
  65. return true;
  66. if (compare_result > 0)
  67. return false;
  68. return e1.size() < e2.size();
  69. }
  70. void endpoint::init(const void* sock_addr,
  71. std::size_t sock_addr_size, int sock_protocol)
  72. {
  73. if (sock_addr_size > sizeof(boost::asio::detail::sockaddr_storage_type))
  74. {
  75. boost::system::error_code ec(boost::asio::error::invalid_argument);
  76. boost::asio::detail::throw_error(ec);
  77. }
  78. using namespace std; // For memset and memcpy.
  79. memset(&data_.generic, 0, sizeof(boost::asio::detail::sockaddr_storage_type));
  80. if (sock_addr_size > 0)
  81. memcpy(&data_.generic, sock_addr, sock_addr_size);
  82. size_ = sock_addr_size;
  83. protocol_ = sock_protocol;
  84. }
  85. } // namespace detail
  86. } // namespace generic
  87. } // namespace asio
  88. } // namespace boost
  89. #include <boost/asio/detail/pop_options.hpp>
  90. #endif // BOOST_ASIO_GENERIC_DETAIL_IMPL_ENDPOINT_IPP