简体   繁体   English

当`boost :: asio :: ip :: tcp :: resolver :: resolve()`失败时,应该提供哪个`boost :: system :: error_code`值?

[英]Which `boost::system::error_code` value should be provided when `boost::asio::ip::tcp::resolver::resolve()` fails?

I want to return a boost::system::error_code indicationg whether a host/service could be resolved or not. 我想返回一个boost::system::error_code指示是否可以解析主机/服务。 There might be multiple reasons why a host/service look-up failed (eg network connection problems or an invalid argument). 主机/服务查找失败可能有多种原因(例如,网络连接问题或无效参数)。

What should be returned? 应该归还什么?

You have to come up with error code and category in order to create error_code object. 您必须提供错误代码和类别才能创建error_code对象。 Here is an example, assuming that error is due to another host refusing connection: 下面是一个示例,假设错误是由于另一台主机拒绝连接:

error_code ec (errc::connection_refused, system_category());
return ec;

You can also pass errno value as error code when using system category. 使用系统类别时,您还可以将errno值作为错误代码传递。 For example: 例如:

#include <fstream>
#include <cerrno>
#include <boost/system/system_error.hpp>

void foo ()
{
    ifstream file ("test.txt");
    if (!file.is_open ())
    {
        int err_code = errno;
        boost::system::error_code ec (err_code
            , boost::system::system_category ());
        throw boost::system::system_error (ec, "cannot open file");
    }
}

Unfortunately, this library is poorly documented , so I can recommend you to look into header files to figure things out. 不幸的是,这个库文档很少,所以我建议你查看头文件来解决问题。 The code is fairly simple and straight forward there. 代码相当简单直接。

Just in case your compiler supports C++11 and you are willing to use it, this functionality made it into standard. 如果您的编译器支持C ++ 11并且您愿意使用它,则此功能使其成为标准。 As far as I know gcc 4.6.1 has it already. 据我所知gcc 4.6.1已经有了。 Here is a simple example: 这是一个简单的例子:

#include <cerrno>
#include <system_error>

std::error_code
SystemError::getLastError ()
{
    int err_code = errno;
    return std::error_code (err_code, std::system_category ());
}

void foo ()
{
    throw std::system_error (getLastError (), "something went wrong");
}

Generally, libraries pass error_code object around if there is no need to throw and use system_error to throw an exception describing system failures. 通常,如果不需要抛出并使用system_error抛出描述系统故障的异常,库会传递error_code对象。 Another reason to use error_code without exceptions is when you need to signal the error across different threads. 使用error_code而无异常的另一个原因是当您需要跨不同线程发出错误信号时。 But C++11 has a solution for propagating exceptions across threads . 但是C ++ 11有一个跨线程传播异常的解决方案

Hope it helps! 希望能帮助到你!

It's impossible to get this right from outside resolve() . 从外面resolve()这是不可能的。 But you can get it to do it for you, by using one of the overloads that takes an error_code& as an out-parameter: 但是你可以通过使用带有error_code& out参数的重载之一来让它为你做到:

and then return the error_code it sets. 然后返回它设置的error_code。 I trust that this will wrap up errno or h_errno as appropriate. 我相信这会在适当时包装errnoh_errno

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 boost::asio::ip::tcp::resolver::resolve() 永远阻塞 - boost::asio::ip::tcp::resolver::resolve() blocks forever boost::system::error_code 和 boost::system::error_code::value() 有什么区别? - What is the difference between boost::system::error_code and boost::system::error_code::value()? boost :: asio :: yield_context可以设置std :: error_code而不是boost :: system :: error_code吗? - Can boost::asio::yield_context set a std::error_code instead of boost::system::error_code? 使用boost :: asio :: write()boost :: system :: error_code永远不会设置 - Using boost::asio::write() boost::system::error_code never gets set boost :: system :: error_code :: message()使用boost :: asio socket抛出访问冲突异常 - boost::system::error_code::message() throwing access violation exception with boost::asio socket 如何从其他异常中分别捕获boost asio boost :: system :: error_code连接异常? - How to catch boost asio boost::system::error_code connect exception separatly from other exceptions? 摆脱TCP代理中的boost依赖关系。 等效的boost :: system :: error_code - Get rid of boost dependencies in TCP proxy. boost::system::error_code equivalent async_resolve中boost :: asio :: ip :: tcp :: resolver :: iterator的生命周期是多长? - What's the lifetime of boost::asio::ip::tcp::resolver::iterator from async_resolve? GStreamer GError to boost::system::error_code? - GStreamer GError to boost::system::error_code? 什么是升压系统error_code number 2 - What is boost system error_code number 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM