75.00% Lines (6/8) 100.00% Functions (1/1)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3   // Copyright (c) 2026 Steve Gerbino 3   // Copyright (c) 2026 Steve Gerbino
4   // Copyright (c) 2026 Michael Vandeberg 4   // Copyright (c) 2026 Michael Vandeberg
5   // 5   //
6   // Distributed under the Boost Software License, Version 1.0. (See accompanying 6   // Distributed under the Boost Software License, Version 1.0. (See accompanying
7   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 7   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8   // 8   //
9   // Official repository: https://github.com/cppalliance/corosio 9   // Official repository: https://github.com/cppalliance/corosio
10   // 10   //
11   11  
12   #ifndef BOOST_COROSIO_NATIVE_DETAIL_MAKE_ERR_HPP 12   #ifndef BOOST_COROSIO_NATIVE_DETAIL_MAKE_ERR_HPP
13   #define BOOST_COROSIO_NATIVE_DETAIL_MAKE_ERR_HPP 13   #define BOOST_COROSIO_NATIVE_DETAIL_MAKE_ERR_HPP
14   14  
15   #include <boost/corosio/detail/config.hpp> 15   #include <boost/corosio/detail/config.hpp>
16   #include <boost/corosio/detail/platform.hpp> 16   #include <boost/corosio/detail/platform.hpp>
17   #include <boost/capy/error.hpp> 17   #include <boost/capy/error.hpp>
18   #include <system_error> 18   #include <system_error>
19   19  
20   #if BOOST_COROSIO_POSIX 20   #if BOOST_COROSIO_POSIX
21   #include <errno.h> 21   #include <errno.h>
22   #else 22   #else
23   #ifndef WIN32_LEAN_AND_MEAN 23   #ifndef WIN32_LEAN_AND_MEAN
24   #define WIN32_LEAN_AND_MEAN 24   #define WIN32_LEAN_AND_MEAN
25   #endif 25   #endif
26   #include <Windows.h> 26   #include <Windows.h>
27   #endif 27   #endif
28   28  
29   namespace boost::corosio::detail { 29   namespace boost::corosio::detail {
30   30  
31   #if BOOST_COROSIO_POSIX 31   #if BOOST_COROSIO_POSIX
32   32  
33   /** Convert a POSIX errno value to std::error_code. 33   /** Convert a POSIX errno value to std::error_code.
34   34  
35   Maps ECANCELED to capy::error::canceled. 35   Maps ECANCELED to capy::error::canceled.
36   36  
37   @param errn The errno value. 37   @param errn The errno value.
38   @return The corresponding std::error_code. 38   @return The corresponding std::error_code.
39   */ 39   */
40   inline std::error_code 40   inline std::error_code
HITCBC 41   137 make_err(int errn) noexcept 41   220 make_err(int errn) noexcept
42   { 42   {
HITCBC 43   137 if (errn == 0) 43   220 if (errn == 0)
MISUBC 44   return {}; 44   return {};
45   45  
HITCBC 46   137 if (errn == ECANCELED) 46   220 if (errn == ECANCELED)
MISUBC 47   return capy::error::canceled; 47   return capy::error::canceled;
48   48  
49   // Part of the portable wait contract; system_category's condition 49   // Part of the portable wait contract; system_category's condition
50   // mapping varies by C++ runtime. 50   // mapping varies by C++ runtime.
HITCBC 51   137 if (errn == ENOTSUP) 51   220 if (errn == ENOTSUP)
HITCBC 52   6 return std::make_error_code(std::errc::operation_not_supported); 52   24 return std::make_error_code(std::errc::operation_not_supported);
53   53  
HITCBC 54   131 return std::error_code(errn, std::system_category()); 54   196 return std::error_code(errn, std::system_category());
55   } 55   }
56   56  
57   #else 57   #else
58   58  
59   /** Convert a Windows error code to std::error_code. 59   /** Convert a Windows error code to std::error_code.
60   60  
61   Maps ERROR_OPERATION_ABORTED and ERROR_CANCELLED to 61   Maps ERROR_OPERATION_ABORTED and ERROR_CANCELLED to
62   capy::error::canceled, and ERROR_HANDLE_EOF to capy::error::eof. 62   capy::error::canceled, and ERROR_HANDLE_EOF to capy::error::eof.
63   Every other code passes through std::system_category(). 63   Every other code passes through std::system_category().
64   64  
65   ERROR_NETNAME_DELETED (64) is deliberately not mapped here: IOCP 65   ERROR_NETNAME_DELETED (64) is deliberately not mapped here: IOCP
66   delivers it both for a local closesocket() that cancels pending I/O 66   delivers it both for a local closesocket() that cancels pending I/O
67   and for a remote RST, so it can only be disambiguated per operation 67   and for a remote RST, so it can only be disambiguated per operation
68   kind at the IOCP decode sites (see iocp_make_err in win_overlapped_op). 68   kind at the IOCP decode sites (see iocp_make_err in win_overlapped_op).
69   69  
70   @param dwError The Windows error code (DWORD). 70   @param dwError The Windows error code (DWORD).
71   @return The corresponding std::error_code. 71   @return The corresponding std::error_code.
72   */ 72   */
73   inline std::error_code 73   inline std::error_code
74   make_err(unsigned long dwError) noexcept 74   make_err(unsigned long dwError) noexcept
75   { 75   {
76   if (dwError == 0) 76   if (dwError == 0)
77   return {}; 77   return {};
78   78  
79   if (dwError == ERROR_OPERATION_ABORTED || dwError == ERROR_CANCELLED) 79   if (dwError == ERROR_OPERATION_ABORTED || dwError == ERROR_CANCELLED)
80   return capy::error::canceled; 80   return capy::error::canceled;
81   81  
82   if (dwError == ERROR_HANDLE_EOF) 82   if (dwError == ERROR_HANDLE_EOF)
83   return capy::error::eof; 83   return capy::error::eof;
84   84  
85 - // Part of the portable wait and adoption contracts; 85 + // Part of the portable wait, adoption, bind, and seek contracts;
86   // system_category's condition mapping for WSA codes varies by 86   // system_category's condition mapping for WSA codes varies by
87   // toolchain. 87   // toolchain.
88   if (dwError == WSAEOPNOTSUPP) 88   if (dwError == WSAEOPNOTSUPP)
89   return std::make_error_code(std::errc::operation_not_supported); 89   return std::make_error_code(std::errc::operation_not_supported);
90   if (dwError == WSAENOTSOCK) 90   if (dwError == WSAENOTSOCK)
91   return std::make_error_code(std::errc::not_a_socket); 91   return std::make_error_code(std::errc::not_a_socket);
92   if (dwError == WSAEAFNOSUPPORT) 92   if (dwError == WSAEAFNOSUPPORT)
93   return std::make_error_code( 93   return std::make_error_code(
94   std::errc::address_family_not_supported); 94   std::errc::address_family_not_supported);
95   if (dwError == WSAEPROTOTYPE) 95   if (dwError == WSAEPROTOTYPE)
96   return std::make_error_code(std::errc::wrong_protocol_type); 96   return std::make_error_code(std::errc::wrong_protocol_type);
  97 + if (dwError == WSAEADDRINUSE)
  98 + return std::make_error_code(std::errc::address_in_use);
  99 + if (dwError == WSAEADDRNOTAVAIL)
  100 + return std::make_error_code(std::errc::address_not_available);
  101 + if (dwError == ERROR_NEGATIVE_SEEK)
  102 + return std::make_error_code(std::errc::invalid_argument);
97   103  
98   return std::error_code(static_cast<int>(dwError), std::system_category()); 104   return std::error_code(static_cast<int>(dwError), std::system_category());
99   } 105   }
100   106  
101   #endif 107   #endif
102   108  
103   } // namespace boost::corosio::detail 109   } // namespace boost::corosio::detail
104   110  
105   #endif 111   #endif