error_code.ipp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // error_code.ipp
  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_ERROR_CODE_IPP
  11. #define ASIO_ERROR_CODE_IPP
  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/detail/push_options.hpp"
  17. #include <boost/config.hpp>
  18. #include <cerrno>
  19. #include <cstring>
  20. #include "asio/detail/pop_options.hpp"
  21. #include "asio/error.hpp"
  22. #include "asio/detail/local_free_on_block_exit.hpp"
  23. #include "asio/detail/socket_types.hpp"
  24. namespace asio {
  25. inline std::string error_code::message() const
  26. {
  27. if (*this == error::already_open)
  28. return "Already open.";
  29. if (*this == error::not_found)
  30. return "Not found.";
  31. if (*this == error::fd_set_failure)
  32. return "The descriptor does not fit into the select call's fd_set.";
  33. if (category_ == error::get_ssl_category())
  34. return "SSL error.";
  35. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  36. value_type value = value_;
  37. if (category() != error::get_system_category() && *this != error::eof)
  38. return "asio error";
  39. if (*this == error::eof)
  40. value = ERROR_HANDLE_EOF;
  41. char* msg = 0;
  42. DWORD length = ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
  43. | FORMAT_MESSAGE_FROM_SYSTEM
  44. | FORMAT_MESSAGE_IGNORE_INSERTS, 0, value,
  45. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char*)&msg, 0, 0);
  46. detail::local_free_on_block_exit local_free_obj(msg);
  47. if (length && msg[length - 1] == '\n')
  48. msg[--length] = '\0';
  49. if (length && msg[length - 1] == '\r')
  50. msg[--length] = '\0';
  51. if (length)
  52. return msg;
  53. else
  54. return "asio error";
  55. #else // defined(BOOST_WINDOWS)
  56. if (*this == error::eof)
  57. return "End of file.";
  58. if (*this == error::host_not_found)
  59. return "Host not found (authoritative).";
  60. if (*this == error::host_not_found_try_again)
  61. return "Host not found (non-authoritative), try again later.";
  62. if (*this == error::no_recovery)
  63. return "A non-recoverable error occurred during database lookup.";
  64. if (*this == error::no_data)
  65. return "The query is valid, but it does not have associated data.";
  66. if (*this == error::not_found)
  67. return "Element not found.";
  68. #if !defined(__sun)
  69. if (*this == error::operation_aborted)
  70. return "Operation aborted.";
  71. #endif // !defined(__sun)
  72. if (*this == error::service_not_found)
  73. return "Service not found.";
  74. if (*this == error::socket_type_not_supported)
  75. return "Socket type not supported.";
  76. if (category() != error::get_system_category())
  77. return "asio error";
  78. #if defined(__sun) || defined(__QNX__)
  79. using namespace std;
  80. return strerror(value_);
  81. #elif defined(__MACH__) && defined(__APPLE__) \
  82. || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) \
  83. || defined(_AIX) || defined(__hpux) || defined(__osf__)
  84. char buf[256] = "";
  85. strerror_r(value_, buf, sizeof(buf));
  86. return buf;
  87. #else
  88. char buf[256] = "";
  89. return strerror_r(value_, buf, sizeof(buf));
  90. #endif
  91. #endif // defined(BOOST_WINDOWS)
  92. }
  93. } // namespace asio
  94. #include "asio/detail/pop_options.hpp"
  95. #endif // ASIO_ERROR_CODE_IPP