chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.72k stars 1.19k forks source link

Windows specific error code 1236 not equivalent to boost::asio::error::connection_aborted #1385

Open buergi opened 7 months ago

buergi commented 7 months ago

The windows specific error code 1236 The network connection was aborted by the local system is equivalent to std::errc::connection_aborted/boost::system::errc::connection_aborted but not to boost::asio::error::connection_aborted.

#include <iostream>
#include <boost/asio/error.hpp>
#include <system_error>

int main()
{
    boost::system::error_code ec(1236, boost::asio::error::get_system_category());
    std::cout << ec.message() << std::endl;
    std::cout << (ec == std::errc::connection_aborted) << std::endl;
    std::cout << (ec == boost::system::errc::connection_aborted) << std::endl;
    std::cout << (ec == boost::asio::error::connection_aborted) << std::endl;
}

outputs

The network connection was aborted by the local system
1
1
0

Tested with boost 1.83.0-1 in mingw64.

I'm not sure if this is intentional. It seems basic_errors are actually system-dependent error_code (implicitly converted due to is_error_code_enum<basic_error>::type), where as boost::system::errc are implicitly converted to system-independent error_condition (due to is_error_condition_enum) as explained here.

However, as all asio examples (e.g. here) are comparing the error code to basic_errors whereas the referenced boost::system documentation recommends comparing to error_condition I'm a bit confused.

So which comparison is the right way to do it? Is it recommended to use asio's errors over generic system errors?