error.hpp 10 KB

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