boost::asio::io_service async_write 在循环中执行 async_read

boost::asio::io_service async_write in loop while doing an async_read

所以我的问题是我有一个异步 TCP 服务器和相应的异步客户端。我需要的是一种能够(连续)写入我的客户端的方法,一个实时变量,同时能够从客户端接收命令。

我现在所拥有的是,如果客户端发送应该触发此操作的命令,它只会发送一条测试消息,但我只能找到一种方法来发送一条消息,因为在那之后服务器会挂起等待客户端命令。

此函数处理从客户端发送的命令,并在接收后将它们传递给函数 h_read:

void conn::h_write()   {
    memset( data_, '[=13=]', sizeof(char)*max_length );
    async_read_until(sock_ , input_buffer_, '\n',
    boost::bind(&conn::h_read, shared_from_this()));

}

这里我检查命令是否应该触发向客户端连续写入实时缓冲区的命令,在这种情况下,命令是 "c".

void conn::h_read(){
    std::string line;
    std::istream is(&input_buffer_);
    std::getline(is, line);
    std::string output = "";
    output.reserve(5000);
    if ( line == "exit"){
        return;
    }
    if ( line.empty() ){
        memset( data_, '[=14=]', sizeof(char)*max_length );
        async_read_until(sock_ , input_buffer_, '\n', boost::bind(&conn::h_read, shared_from_this()));
        return;
    }

    clientMessage_ = line;      
    clientMessage_ += '\n';

    if ( clientMessage_.substr(0,1) == "c" ){
        std::stringstream toSend;
        streamON = true;
        toSend.str("c l 1 ");
        toSend << std::fixed << std::setprecision( 2 ) << luxID1[0];
        toSend.str(" ");
        // Here I sent the real time value for the first time
        boost::asio::async_write(sock_, boost::asio::buffer( toSend.str() ), boost::bind(&conn::sendRealTime, shared_from_this()));
    }       
    else{ // Does't really matter to this example
        // Do some stuff here and send to client
        boost::asio::async_write(sock_, boost::asio::buffer( I2CrxBuf_ ), boost::bind(&conn::h_write, shared_from_this()));
    }
}

现在这个函数应该处理变量的连续发送,但同时能够读取客户端命令:

void conn::sendRealTime(){
    if ( streamON ){
        boost::asio::async_write(sock_, boost::asio::buffer( "This is a test\n" ), boost::bind(&conn::h_write, shared_from_this()));
        memset( data_, '[=15=]', sizeof(char)*max_length );
        async_read_until(sock_ , input_buffer_, '\n', boost::bind(&conn::h_read, shared_from_this()));
    }
    else{
        memset( data_, '[=15=]', sizeof(char)*max_length );
        async_read_until(sock_ , input_buffer_, '\n', boost::bind(&conn::h_read, shared_from_this()));
    }
}

问题是它在第一次调用 "async_read_util" 函数后阻塞。

我什至不知道我想要的是否可行,但如果可行,请有人帮助我如何做?

What I need is a way to be able to write to my client (continuously), a real time variable, while at the same time being able to receive commands from the client.

这不是问题。

将您的读写功能分开。在成功连接/初始化套接字后调用 async_read_until 一次。然后在您的读取处理程序中再次调用它。无处。这是执行读取操作的常用方法。

另请参阅documentation.

The program must ensure that the stream performs no other read operations (such as async_read, async_read_until, the stream's async_read_some function, or any other composed operations that perform reads) until this operation completes.

请记住,读取缓冲区中的数据可能比您预期的要多。

After a successful async_read_until operation, the streambuf may contain additional data beyond the delimiter. An application will typically leave that data in the streambuf for a subsequent async_read_until operation to examine.