error_code.ipp 3.6 KB

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