/Src/Dependencies/Boost/boost/asio/local/detail/impl/endpoint.ipp

http://hadesmem.googlecode.com/ · C++ Header · 130 lines · 92 code · 24 blank · 14 comment · 13 complexity · 2ede33b2a28feca567e540b6ab119ae8 MD5 · raw file

  1. //
  2. // local/detail/impl/endpoint.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Derived from a public domain implementation written by Daniel Casimiro.
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
  12. #define BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
  18. #include <cstring>
  19. #include <boost/asio/detail/socket_ops.hpp>
  20. #include <boost/asio/detail/throw_error.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/local/detail/endpoint.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace local {
  27. namespace detail {
  28. endpoint::endpoint()
  29. {
  30. init("", 0);
  31. }
  32. endpoint::endpoint(const char* path_name)
  33. {
  34. using namespace std; // For strlen.
  35. init(path_name, strlen(path_name));
  36. }
  37. endpoint::endpoint(const std::string& path_name)
  38. {
  39. init(path_name.data(), path_name.length());
  40. }
  41. void endpoint::resize(std::size_t new_size)
  42. {
  43. if (new_size > sizeof(boost::asio::detail::sockaddr_un_type))
  44. {
  45. boost::system::error_code ec(boost::asio::error::invalid_argument);
  46. boost::asio::detail::throw_error(ec);
  47. }
  48. else if (new_size == 0)
  49. {
  50. path_length_ = 0;
  51. }
  52. else
  53. {
  54. path_length_ = new_size
  55. - offsetof(boost::asio::detail::sockaddr_un_type, sun_path);
  56. // The path returned by the operating system may be NUL-terminated.
  57. if (path_length_ > 0 && data_.local.sun_path[path_length_ - 1] == 0)
  58. --path_length_;
  59. }
  60. }
  61. std::string endpoint::path() const
  62. {
  63. return std::string(data_.local.sun_path, path_length_);
  64. }
  65. void endpoint::path(const char* p)
  66. {
  67. using namespace std; // For strlen.
  68. init(p, strlen(p));
  69. }
  70. void endpoint::path(const std::string& p)
  71. {
  72. init(p.data(), p.length());
  73. }
  74. bool operator==(const endpoint& e1, const endpoint& e2)
  75. {
  76. return e1.path() == e2.path();
  77. }
  78. bool operator<(const endpoint& e1, const endpoint& e2)
  79. {
  80. return e1.path() < e2.path();
  81. }
  82. void endpoint::init(const char* path_name, std::size_t path_length)
  83. {
  84. if (path_length > sizeof(data_.local.sun_path) - 1)
  85. {
  86. // The buffer is not large enough to store this address.
  87. boost::system::error_code ec(boost::asio::error::name_too_long);
  88. boost::asio::detail::throw_error(ec);
  89. }
  90. using namespace std; // For memcpy.
  91. data_.local = boost::asio::detail::sockaddr_un_type();
  92. data_.local.sun_family = AF_UNIX;
  93. memcpy(data_.local.sun_path, path_name, path_length);
  94. path_length_ = path_length;
  95. // NUL-terminate normal path names. Names that start with a NUL are in the
  96. // UNIX domain protocol's "abstract namespace" and are not NUL-terminated.
  97. if (path_length > 0 && data_.local.sun_path[0] == 0)
  98. data_.local.sun_path[path_length] = 0;
  99. }
  100. } // namespace detail
  101. } // namespace local
  102. } // namespace asio
  103. } // namespace boost
  104. #include <boost/asio/detail/pop_options.hpp>
  105. #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
  106. #endif // BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP