Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost.asio error codes on windows are platform-dependent?

I am writing a small network client program, it uses boost.asio as the network layer, this is a function used to cast error codes of boost.asio to error codes of my program.

Today I found that if my client program starts while the server is not opened, connect will fails, boost.asio's error code will not be 'asio::error::connection_refused' or 'system::errc::connection_refused' but 'ERROR_CONNECTION_REFUSED', this is a windows platform-dependent error code, to deal with this I added a macro to the code, but I think this is just ugly.

inline ErrorCode::Code cast(const boost::system::error_code& ec, ErrorCode::Code d = ErrorCode::unknownError)
{
    if (ec == boost::asio::error::operation_aborted) {
        return ErrorCode::operationCanceled;
    }
    if (ec == boost::system::errc::connection_refused || ec == boost::asio::error::connection_refused) {
        return ErrorCode::connectionRefused;
    }

    if (ec.category() == boost::system::get_system_category()) {
#if defined(BOOST_ASIO_WINDOWS)
        if (ec.value() == ERROR_CONNECTION_REFUSED) {
            return ErrorCode::connectionRefused;
        }
#endif
    }
    return d;
}

Is there other correct way to deal with this ?


I am confused, default_error_condition's value is still 1225, on my computer, 'boost::system::errc::connection_refused' is 107, and 'boost::asio::error::connection_refused' is 10061.

So where is the platform-independent error code ?

like image 535
amanjiang Avatar asked Oct 27 '25 03:10

amanjiang


1 Answers

It looks like you want to use using error_condition instead of error_code here.

Class error_condition

The class error_condition describes an object used to hold values identifying error conditions. [ Note: error_condition values are portable abstractions, while error_code values are implementation specific. --end note ]

Documentation suggests error_code::default_error_condition should work for you.

Also highly interesting is this page: http://en.highscore.de/cpp/boost/errorhandling.html

This chapter introduces two Boost C++ Libraries assisting a developer in leveraging error handling: Boost.System translates operating system specific error codes into platform independent ones

boost::system::error_condition is used just like boost::system::error_code. Both the value() and category() method can be called for the boost::system::error_condition object as shown in the above example.

The reason for having two more or less identical classes is fairly simple: While boost::system::error_code is used for platform dependent error codes, boost::system::error_condition is used to access platform independent error codes instead. By calling the default_error_condition() method, a platform dependent error code is translated into a platform independent error code of type boost::system::error_condition.

like image 67
sehe Avatar answered Oct 28 '25 18:10

sehe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!