简体   繁体   English

Boost Asio async_read不会停止阅读吗?

[英]Boost Asio async_read doesn't stop reading?

So, 所以,

I've been playing around with the Boost asio functions and sockets (specifically the async read/write). 我一直在玩Boost asio函数和套接字(特别是异步读/写)。 Now, I thought that boost::asio::async_read only called the handler when a new buffer came in from the network connection... however it doesn't stop reading the same buffer and thus keeps calling the handler. 现在,我认为boost::asio::async_read仅在网络连接中有新缓冲区进入时才调用处理程序...但是,它不会停止读取相同的缓冲区,因此一直在调用处理程序。 I've been able to mitigate it by checking the number of bytes transferred, however it is basically in a busy-waiting loop wasting CPU cycles. 我已经能够通过检查传输的字节数来缓解这种情况,但是它基本上处于浪费CPU周期的繁忙循环中。

Here is what I have: 这是我所拥有的:

class tcp_connection : : public boost::enable_shared_from_this<tcp_connection> 
{
public:
    // other functions here

    void start()
    {
    boost::asio::async_read(socket_, boost::asio::buffer(buf, TERRAINPACKETSIZE),
        boost::bind(&tcp_connection::handle_read, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
    }

private:
    const unsigned int TERRAINPACKETSIZE = 128;
    char buf[TERRAINPACKETSIZE];


    void handle_read(const boost::system::error_code& error, size_t bytesT)
    {
        if (bytesT > 0)
        { 
             // Do the packet handling stuff here
        }

        boost::asio::async_read(socket_, boost::asio::buffer(buf, TERRAINPACKETSIZE),
        boost::bind(&tcp_connection::handle_read, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
    }
};

Some stuff is cut out, but basically a new connection gets created then start() is called. 删除了一些东西,但是基本上创建了一个新连接,然后调用了start() Is there something I'm missing so that the handle_read method doesn't get continuously called? 我缺少什么东西了,以便handle_read方法不会被连续调用吗?

A wild guess: Do you check error in handle_read ? 一个handle_read猜测:您是否检查handle_read error If the socket is in an error state for some reason, I guess that the nested call to async_read made from handle_read will immediately "complete", resulting in an immediate call to handle_read 如果套接字由于某种原因处于错误状态,我想从handle_readasync_read的嵌套调用将立即“完成”,从而导致对handle_read的立即调用

I was having the same problem. 我遇到了同样的问题。 In my case, I was reading into a std::vector like this: 就我而言,我正在像这样读入std :: vector:

boost::asio::async_read(socket_,
st::asio::buffer(*message,message->size()),
            boost::bind(
              &ActiveSocketServerSession::handleFixLengthRead,
              shared_from_this(),
              boost::asio::placeholders::error,
              boost::asio::placeholders::bytes_transferred)
    );

I have an adaptative vector for accept a variant number of bytes 我有一个自适应向量,可以接受不同数量的字节

    std::vector<unsigned char>* message;
message = new std::vector<unsigned char> (sizePacket);

When I was receiving the first packet all was going fine, but after the first, never stops unlocking handle_reader with no data. 当我收到第一个数据包时,一切都很好,但是在第一个数据包之后,请不要停止解锁没有数据的handle_reader。

My solution was to delete my vector and alloc space again after processing it: 我的解决方案是删除我的向量并在处理它之后再次分配空间:

void ActiveSocketServerSession::handleFixLengthRead(    const         boost::system::error_code& error,
                                                    std::size_t bytes_transferred){

    --> processing your data (save to another site)
--> delete message;
--> message = new std::vector<unsigned char> (sizePacket);

//starting to read again
boost::asio::async_read(socket_,
                boost::asio::buffer(*message,message->size()),
            boost::bind(
              &ActiveSocketServerSession::handleFixLengthRead,
              shared_from_this(),
              boost::asio::placeholders::error,
              boost::asio::placeholders::bytes_transferred)
);

    }else{
        logMessage.str("");
        logMessage << "Error handling data id: "<<getId()<< "from port";
    }
}

I found this solution reading this 我发现这个解决方案读这篇

After putting these two lines all goes fine. 放置这两行之后,一切正常。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM