error.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // error.hpp
  3. // ~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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_ERROR_HPP
  11. #define BOOST_ASIO_ERROR_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/config.hpp>
  16. #include <boost/cerrno.hpp>
  17. #include <boost/system/error_code.hpp>
  18. #if defined(BOOST_ASIO_WINDOWS) \
  19. || defined(__CYGWIN__) \
  20. || defined(BOOST_ASIO_WINDOWS_RUNTIME)
  21. # include <winerror.h>
  22. #else
  23. # include <cerrno>
  24. # include <netdb.h>
  25. #endif
  26. #if defined(GENERATING_DOCUMENTATION)
  27. /// INTERNAL ONLY.
  28. # define BOOST_ASIO_NATIVE_ERROR(e) implementation_defined
  29. /// INTERNAL ONLY.
  30. # define BOOST_ASIO_SOCKET_ERROR(e) implementation_defined
  31. /// INTERNAL ONLY.
  32. # define BOOST_ASIO_NETDB_ERROR(e) implementation_defined
  33. /// INTERNAL ONLY.
  34. # define BOOST_ASIO_GETADDRINFO_ERROR(e) implementation_defined
  35. /// INTERNAL ONLY.
  36. # define BOOST_ASIO_WIN_OR_POSIX(e_win, e_posix) implementation_defined
  37. #elif defined(BOOST_ASIO_WINDOWS_RUNTIME)
  38. # define BOOST_ASIO_NATIVE_ERROR(e) __HRESULT_FROM_WIN32(e)
  39. # define BOOST_ASIO_SOCKET_ERROR(e) __HRESULT_FROM_WIN32(WSA ## e)
  40. # define BOOST_ASIO_NETDB_ERROR(e) __HRESULT_FROM_WIN32(WSA ## e)
  41. # define BOOST_ASIO_GETADDRINFO_ERROR(e) __HRESULT_FROM_WIN32(WSA ## e)
  42. # define BOOST_ASIO_WIN_OR_POSIX(e_win, e_posix) e_win
  43. #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  44. # define BOOST_ASIO_NATIVE_ERROR(e) e
  45. # define BOOST_ASIO_SOCKET_ERROR(e) WSA ## e
  46. # define BOOST_ASIO_NETDB_ERROR(e) WSA ## e
  47. # define BOOST_ASIO_GETADDRINFO_ERROR(e) WSA ## e
  48. # define BOOST_ASIO_WIN_OR_POSIX(e_win, e_posix) e_win
  49. #else
  50. # define BOOST_ASIO_NATIVE_ERROR(e) e
  51. # define BOOST_ASIO_SOCKET_ERROR(e) e
  52. # define BOOST_ASIO_NETDB_ERROR(e) e
  53. # define BOOST_ASIO_GETADDRINFO_ERROR(e) e
  54. # define BOOST_ASIO_WIN_OR_POSIX(e_win, e_posix) e_posix
  55. #endif
  56. #include <boost/asio/detail/push_options.hpp>
  57. namespace boost {
  58. namespace asio {
  59. namespace error {
  60. enum basic_errors
  61. {
  62. /// Permission denied.
  63. access_denied = BOOST_ASIO_SOCKET_ERROR(EACCES),
  64. /// Address family not supported by protocol.
  65. address_family_not_supported = BOOST_ASIO_SOCKET_ERROR(EAFNOSUPPORT),
  66. /// Address already in use.
  67. address_in_use = BOOST_ASIO_SOCKET_ERROR(EADDRINUSE),
  68. /// Transport endpoint is already connected.
  69. already_connected = BOOST_ASIO_SOCKET_ERROR(EISCONN),
  70. /// Operation already in progress.
  71. already_started = BOOST_ASIO_SOCKET_ERROR(EALREADY),
  72. /// Broken pipe.
  73. broken_pipe = BOOST_ASIO_WIN_OR_POSIX(
  74. BOOST_ASIO_NATIVE_ERROR(ERROR_BROKEN_PIPE),
  75. BOOST_ASIO_NATIVE_ERROR(EPIPE)),
  76. /// A connection has been aborted.
  77. connection_aborted = BOOST_ASIO_SOCKET_ERROR(ECONNABORTED),
  78. /// Connection refused.
  79. connection_refused = BOOST_ASIO_SOCKET_ERROR(ECONNREFUSED),
  80. /// Connection reset by peer.
  81. connection_reset = BOOST_ASIO_SOCKET_ERROR(ECONNRESET),
  82. /// Bad file descriptor.
  83. bad_descriptor = BOOST_ASIO_SOCKET_ERROR(EBADF),
  84. /// Bad address.
  85. fault = BOOST_ASIO_SOCKET_ERROR(EFAULT),
  86. /// No route to host.
  87. host_unreachable = BOOST_ASIO_SOCKET_ERROR(EHOSTUNREACH),
  88. /// Operation now in progress.
  89. in_progress = BOOST_ASIO_SOCKET_ERROR(EINPROGRESS),
  90. /// Interrupted system call.
  91. interrupted = BOOST_ASIO_SOCKET_ERROR(EINTR),
  92. /// Invalid argument.
  93. invalid_argument = BOOST_ASIO_SOCKET_ERROR(EINVAL),
  94. /// Message too long.
  95. message_size = BOOST_ASIO_SOCKET_ERROR(EMSGSIZE),
  96. /// The name was too long.
  97. name_too_long = BOOST_ASIO_SOCKET_ERROR(ENAMETOOLONG),
  98. /// Network is down.
  99. network_down = BOOST_ASIO_SOCKET_ERROR(ENETDOWN),
  100. /// Network dropped connection on reset.
  101. network_reset = BOOST_ASIO_SOCKET_ERROR(ENETRESET),
  102. /// Network is unreachable.
  103. network_unreachable = BOOST_ASIO_SOCKET_ERROR(ENETUNREACH),
  104. /// Too many open files.
  105. no_descriptors = BOOST_ASIO_SOCKET_ERROR(EMFILE),
  106. /// No buffer space available.
  107. no_buffer_space = BOOST_ASIO_SOCKET_ERROR(ENOBUFS),
  108. /// Cannot allocate memory.
  109. no_memory = BOOST_ASIO_WIN_OR_POSIX(
  110. BOOST_ASIO_NATIVE_ERROR(ERROR_OUTOFMEMORY),
  111. BOOST_ASIO_NATIVE_ERROR(ENOMEM)),
  112. /// Operation not permitted.
  113. no_permission = BOOST_ASIO_WIN_OR_POSIX(
  114. BOOST_ASIO_NATIVE_ERROR(ERROR_ACCESS_DENIED),
  115. BOOST_ASIO_NATIVE_ERROR(EPERM)),
  116. /// Protocol not available.
  117. no_protocol_option = BOOST_ASIO_SOCKET_ERROR(ENOPROTOOPT),
  118. /// Transport endpoint is not connected.
  119. not_connected = BOOST_ASIO_SOCKET_ERROR(ENOTCONN),
  120. /// Socket operation on non-socket.
  121. not_socket = BOOST_ASIO_SOCKET_ERROR(ENOTSOCK),
  122. /// Operation cancelled.
  123. operation_aborted = BOOST_ASIO_WIN_OR_POSIX(
  124. BOOST_ASIO_NATIVE_ERROR(ERROR_OPERATION_ABORTED),
  125. BOOST_ASIO_NATIVE_ERROR(ECANCELED)),
  126. /// Operation not supported.
  127. operation_not_supported = BOOST_ASIO_SOCKET_ERROR(EOPNOTSUPP),
  128. /// Cannot send after transport endpoint shutdown.
  129. shut_down = BOOST_ASIO_SOCKET_ERROR(ESHUTDOWN),
  130. /// Connection timed out.
  131. timed_out = BOOST_ASIO_SOCKET_ERROR(ETIMEDOUT),
  132. /// Resource temporarily unavailable.
  133. try_again = BOOST_ASIO_WIN_OR_POSIX(
  134. BOOST_ASIO_NATIVE_ERROR(ERROR_RETRY),
  135. BOOST_ASIO_NATIVE_ERROR(EAGAIN)),
  136. /// The socket is marked non-blocking and the requested operation would block.
  137. would_block = BOOST_ASIO_SOCKET_ERROR(EWOULDBLOCK)
  138. };
  139. enum netdb_errors
  140. {
  141. /// Host not found (authoritative).
  142. host_not_found = BOOST_ASIO_NETDB_ERROR(HOST_NOT_FOUND),
  143. /// Host not found (non-authoritative).
  144. host_not_found_try_again = BOOST_ASIO_NETDB_ERROR(TRY_AGAIN),
  145. /// The query is valid but does not have associated address data.
  146. no_data = BOOST_ASIO_NETDB_ERROR(NO_DATA),
  147. /// A non-recoverable error occurred.
  148. no_recovery = BOOST_ASIO_NETDB_ERROR(NO_RECOVERY)
  149. };
  150. enum addrinfo_errors
  151. {
  152. /// The service is not supported for the given socket type.
  153. service_not_found = BOOST_ASIO_WIN_OR_POSIX(
  154. BOOST_ASIO_NATIVE_ERROR(WSATYPE_NOT_FOUND),
  155. BOOST_ASIO_GETADDRINFO_ERROR(EAI_SERVICE)),
  156. /// The socket type is not supported.
  157. socket_type_not_supported = BOOST_ASIO_WIN_OR_POSIX(
  158. BOOST_ASIO_NATIVE_ERROR(WSAESOCKTNOSUPPORT),
  159. BOOST_ASIO_GETADDRINFO_ERROR(EAI_SOCKTYPE))
  160. };
  161. enum misc_errors
  162. {
  163. /// Already open.
  164. already_open = 1,
  165. /// End of file or stream.
  166. eof,
  167. /// Element not found.
  168. not_found,
  169. /// The descriptor cannot fit into the select system call's fd_set.
  170. fd_set_failure
  171. };
  172. inline const boost::system::error_category& get_system_category()
  173. {
  174. return boost::system::system_category();
  175. }
  176. #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  177. extern BOOST_ASIO_DECL
  178. const boost::system::error_category& get_netdb_category();
  179. extern BOOST_ASIO_DECL
  180. const boost::system::error_category& get_addrinfo_category();
  181. #else // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  182. inline const boost::system::error_category& get_netdb_category()
  183. {
  184. return get_system_category();
  185. }
  186. inline const boost::system::error_category& get_addrinfo_category()
  187. {
  188. return get_system_category();
  189. }
  190. #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  191. extern BOOST_ASIO_DECL
  192. const boost::system::error_category& get_misc_category();
  193. static const boost::system::error_category& system_category
  194. = boost::asio::error::get_system_category();
  195. static const boost::system::error_category& netdb_category
  196. = boost::asio::error::get_netdb_category();
  197. static const boost::system::error_category& addrinfo_category
  198. = boost::asio::error::get_addrinfo_category();
  199. static const boost::system::error_category& misc_category
  200. = boost::asio::error::get_misc_category();
  201. } // namespace error
  202. } // namespace asio
  203. } // namespace boost
  204. namespace boost {
  205. namespace system {
  206. template<> struct is_error_code_enum<boost::asio::error::basic_errors>
  207. {
  208. static const bool value = true;
  209. };
  210. template<> struct is_error_code_enum<boost::asio::error::netdb_errors>
  211. {
  212. static const bool value = true;
  213. };
  214. template<> struct is_error_code_enum<boost::asio::error::addrinfo_errors>
  215. {
  216. static const bool value = true;
  217. };
  218. template<> struct is_error_code_enum<boost::asio::error::misc_errors>
  219. {
  220. static const bool value = true;
  221. };
  222. } // namespace system
  223. } // namespace boost
  224. namespace boost {
  225. namespace asio {
  226. namespace error {
  227. inline boost::system::error_code make_error_code(basic_errors e)
  228. {
  229. return boost::system::error_code(
  230. static_cast<int>(e), get_system_category());
  231. }
  232. inline boost::system::error_code make_error_code(netdb_errors e)
  233. {
  234. return boost::system::error_code(
  235. static_cast<int>(e), get_netdb_category());
  236. }
  237. inline boost::system::error_code make_error_code(addrinfo_errors e)
  238. {
  239. return boost::system::error_code(
  240. static_cast<int>(e), get_addrinfo_category());
  241. }
  242. inline boost::system::error_code make_error_code(misc_errors e)
  243. {
  244. return boost::system::error_code(
  245. static_cast<int>(e), get_misc_category());
  246. }
  247. } // namespace error
  248. } // namespace asio
  249. } // namespace boost
  250. #include <boost/asio/detail/pop_options.hpp>
  251. #undef BOOST_ASIO_NATIVE_ERROR
  252. #undef BOOST_ASIO_SOCKET_ERROR
  253. #undef BOOST_ASIO_NETDB_ERROR
  254. #undef BOOST_ASIO_GETADDRINFO_ERROR
  255. #undef BOOST_ASIO_WIN_OR_POSIX
  256. #if defined(BOOST_ASIO_HEADER_ONLY)
  257. # include <boost/asio/impl/error.ipp>
  258. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  259. #endif // BOOST_ASIO_ERROR_HPP