在野兽1.70.0中使用Tcp超时

时间:2019-04-16 12:29:54

标签: c++ boost ide boost-beast

我刚刚将Boost库从1.68.0更新为1.70.0,以获得(beast) websocket ssl client async example中的超时操作。

在上面的链接中,您将看到:

void
    on_resolve(
        beast::error_code ec,
        tcp::resolver::results_type results)
    {
        if(ec)
            return fail(ec, "resolve");

        // Set a timeout on the operation
        beast::get_lowest_layer(ws_).expires_after(std::chrono::seconds(30));

        // Make the connection on the IP address we get from a lookup
        beast::get_lowest_layer(ws_).async_connect(
            results,
            beast::bind_front_handler(
                &session::on_connect,
                shared_from_this()));
    }

有多个功能正在使用此结构进行超时。而对于我的代码(在eclipse-cdt中,我看到的是这样的

Screenshot for this

错误提示(当鼠标指针悬停在 expires_after async_connect 上时):

  

方法'expires_after'无法解析
  
  方法“ async_connect”无法解析

并且当鼠标指针移到“ get_lowest_layer”上时,错误提示

  

无效的参数'
  候选人是:
  boost :: beast :: detail :: lowest_layer_type_impl <#0,bool74 0值43 8 2 201 2
  boost :: beast :: detail :: has_next_layer_impl
  boost :: beast :: detail :: has_next_layer_impl 1#0 0 71 4417 0 0> :: type和get_lowest_layer(#0&)   '

我想知道我是否需要为此链接一些库。我不知道是哪一个。 有什么建议吗?

2 个答案:

答案 0 :(得分:1)

这与库无关。

  1. boost :: beast是模板库,因此没有共享库。

  2. 您的编辑器使用定义而不是链接来显示此IDE错误。基本上,您的编辑器无法找到您要指向的标题。

如果我不得不猜测,您已手动编译了boost以使用boost :: beast,因为它在大多数现代Linux发行版中均不可用。或者您可能没有使用Linux。该示例包含一些包含,并且您的IDE无法解析它们,因为它们不在您的系统包含(/usr/include)中。因此,它不知道在哪里看。

因此,总而言之,您的构建系统未与IDE正确耦合。

要解决此问题,请尝试了解您的IDE如何解决丢失的标头。向其中添加包含boost标头的包含路径。

答案 1 :(得分:0)

我通过将超时设置为

解决了代码中的问题(使用beast 1.70.0)。
void
    on_resolve(
        beast::error_code ec,
        tcp::resolver::results_type results)
    {
        if(ec)
            return fail(ec, "resolve");

        // Set a timeout on the operation
        ws_.next_layer().expires_after(std::chrono::seconds(30));

        // Make the connection on the IP address we get from a lookup
         ws_.next_layer().async_connect(
            results,
            beast::bind_front_handler(
                &session::on_connect,
                shared_from_this()));
    }

我还对我的代码(使用beast 1.68.0)进行了如下更改

void Foo::closetimer_websocket(beast::error_code ec) {

    if (ec.message() == "Success") {
        ioc.stop();
    }
}

// closetimer_websocket is the member of class Foo. And FooObject is its instance
void session::SetAsyncOpTimeoutInSec(unsigned int time_inSeconds) {
    TcpTimer.expires_from_now((boost::posix_time::seconds(time_inSeconds)));
    TcpTimer.async_wait(bind(&Foo::closetimer_websocket, FooObject, placeholders::_1));
}

void session::on_resolve(beast::error_code ec,
        tcp::resolver::results_type results) {
     if(ec)
            return fail(ec, "resolve");

    //Set the timeout
    SetAsyncOpTimeoutInSec(5);

    // Make the connection on the IP address we get from a lookup
    net::async_connect(ws_.next_layer().next_layer(), results.begin(),
            results.end(),
            bind(&session::on_connect, shared_from_this(), placeholders::_1));
}