[英]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参数的重载之一来让它为你做到:
iterator resolve(const query & q, boost::system::error_code & ec)
iterator resolve(const endpoint_type & e, boost::system::error_code & ec)
and then return the error_code it sets. 然后返回它设置的error_code。 I trust that this will wrap up
errno
or h_errno
as appropriate. 我相信这会在适当时包装
errno
或h_errno
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.