野兽async_read()如何工作&有没有选择的余地? [英] How does beast async_read() work & is there an option for it?

查看:126
本文介绍了野兽async_read()如何工作&有没有选择的余地?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对boost::asio的基础知识不是很熟悉.我正在执行一项已连接到Web服务器并读取响应的任务.响应在生成响应时以随机时间抛出.

I am not very familiar with the boost::asio fundamentals. I am working on a task where I have connected to a web server and reading the response. The response is thrown at a random period, i.e. as and when the response is generated.

为此,我使用的是boost::beast库,该库包装在boost::asio基础知识上.

For this I am using the boost::beast library which is wrapped over the boost::asio fundamentals.

我发现async_read()函数正在等待直到收到响应.

I have found that the async_read() function is waiting until it is receiving a response.

现在,问题是:在文档&示例显示了websocket通信的异步方式,其中websocket在收到响应后便关闭了.

Now, the thing is : in documentations & example the asynchronous way of websocket communication is shown where the websocket is closed after it receives the response.

以下代码(野兽文档)完成

void read_resp(boost::system::error_code ec, std::size_t bytes_transferred) {
    boost::ignore_unused(bytes_transferred);

    if(ec)
        return fail(ec, "write");

    // Read a message into our buffer
    ws_.async_read(buffer_, std::bind(&session::close_ws_, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}

void close_ws_(boost::system::error_code ec, std::size_t bytes_transferred) {
    boost::ignore_unused(bytes_transferred);

    if(ec)
        return fail(ec, "read");

    // Close the WebSocket connection
    ws_.async_close(websocket::close_code::normal, std::bind(&session::on_close, shared_from_this(), std::placeholders::_1));
}

在此程序中,假定发送已在接收之前完成.而且只有一次响应可以期望来自服务器.之后,它(客户端)前进并关闭websocket.

In this program it is assumed that the sending is completed before receiving. And there is only once response to expect from server. After which, it(client) goes forward and closes the websocket.

但是在我的程序中:

我必须检查书写部分是否已经结束,如果还没有结束,那么在编写过程中,websocket应该出现,并检查响应,直到现在为止所写的内容.

I have to check whether the writing part has ended, if not, while the writing is in progress the websocket should come and check the response for whatever is written till now.

现在,我放置了一个if,它将告诉我的程序我的写作是否受到强迫?如果不是,则程序应返回到写入部分并写入所需的内容.如果是,则关闭连接.

Now for this, I have put an if else which will tell my program whether or not my writing is compeleted or not? if not, then the program should go back to the write section and write the required thing. If Yes then go and close the connection.

void write_bytes(//Some parameters) {
    //write on websocket

    //go to read_resp
}

void read_resp(boost::system::error_code ec, std::size_t bytes_transferred) {
    boost::ignore_unused(bytes_transferred);

    if(ec)
        return fail(ec, "write");

    // Read a message into our buffer
    if (write_completed){
        ws_.async_read(buffer_, std::bind(&session::close_ws_, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
    } else {
        ws_.async_read(buffer_, std::bind(&session::write_bytes, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
    }
}

void close_ws_(//Some parameters) {
    //Same as before
}

现在我要做的是写入完成后,等待3秒钟,每隔1秒钟读取一次网络套接字,然后在3秒钟后关闭网络套接字.

Now what I want to do is,After the write is completed wait for 3 seconds and read the websocket after every 1 second, and after 3rd second go to close the websocket.

为此,我另外提供了一个,以检查read_resp

For that I have included one more if else to check the 3 second condition to the read_resp

void read_resp(boost::system::error_code ec, std::size_t bytes_transferred) {
    boost::ignore_unused(bytes_transferred);

    if(ec)
        return fail(ec, "write");

    // Read a message into our buffer
    if (write_completed){
        if (3_seconds_completed) {
            ws_.async_read(buffer_, std::bind(&session::close_ws_, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
        } else {
            usleep(1000000); //wait for a second
            ws_.async_read(buffer_, std::bind(&session::read_resp, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
        }
    } else {
        ws_.async_read(buffer_, std::bind(&session::write_bytes, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
    }
}

但是Websocket正在等待async-read接收到某些内容,并且这样做只会导致会话超时.

But the websocket is waiting for async-read to receive something and in doing so it just goes to session timeout.

如何只看是否有要阅读的内容,然后继续执行回叫?

how can just just see if something is there to read and then move on to execute the call back?

推荐答案

这可能只是我在这里遇到的问题的答案.我无法保证将来的读者的解决方案.

This might just be an answer for my problem here. I can't guarentee the solution for future readers.

我已经删除了read_resp()自循环,只需在write_completed == true时让async_read()移至close_ws_.

I have removed the read_resp() self-loop and simply let the async_read() to move to close_ws_ when write_completed == true.

async_read会一直等待,直到它收到响应,并且不会继续进行下一步,从而导致超时.

The async_read will wait for as long as it receives the response and do not move on to next step thus causing the timeout.

这篇关于野兽async_read()如何工作&有没有选择的余地?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆