[asio-users] Waiting for the data while async_write
Brought to you by:
chris_kohlhoff
From: Michael IV <exp...@gm...> - 2017-10-23 16:26:28
|
Hi again! I need an advice. I have a situation where the data can be unavailable when asyn_write is getting called. What's the best practice to wait for the data in such a case? Here is the case: void Connection::Send() { //we write syncrhoniously MyData packet; bool gotPacket = task->data_queue.try_dequeue(packet); if (!gotPacket) { // >> ???? // Should I run a while(!gotPacket) here? } //fill buffers with data here... auto self(shared_from_this()); asio::async_write(mSocket, buffers, [this, self](const asio::error_code error, size_t bytes_transfered) { if (error) { std::cout << "Async write interrupted with error:" << error.message() << std::endl; return; } assert(bytes_transfered); //chain: Send(); } ); } So should I run a while( !gotPacket) loop to wait for the data to be available? Or I can somehow skip this async_write without losing the chaining? Thanks in advance. |