我有哪裏boost::asio::async_read
第二呼叫失敗的問題,以一種奇怪的方式:爲什麼boost :: asio :: async_read無法讀取請求的字節數?
std::atomic<error_code> ec(asio::error::would_block);
size_t len = 0;
// 1st call
asio::async_read(socket,
asio::buffer(buffer+X),
asio::transfer_exactly(512-X),
[&] (error_code const& err, size_t bytesTransferred)
{
len = bytesTransferred;
ec.store(err, std::memory_order_release);
}
);
/////// ... wait for read to complete ...
// 2nd call
asio::async_read(socket,
asio::buffer(buffer),
asio::transfer_exactly(512),
[&] (error_code const& err, size_t bytesTransferred)
{
len = bytesTransferred;
ec.store(err, std::memory_order_release);
}
);
恆X
是因爲我有一些數據已經是我以另一種方式得到的,所以 第一次讀更小。說X = 364,那麼bytesTransferred
將是第一次148。然而我的問題是,第二次讀取再次返回148個字節,即使讀取完全是512字節。
我很困惑。第二個電話沒有錯誤情況(我檢查了err
)。 bytesTransferred
是由aync_read
傳給我的一個參數,它是148個字節的兩倍。第一次,它匹配堆棧中較高的asio::transfer_exactly(148)
。第二次callstack顯然有asio::transfer_exactly(512)
。這裏發生了什麼?
雖然第二次調用是特別的,但第三次調用再次讀取512字節,但也獲得512字節。
[MCVE]
#include <iostream>
#include <atomic>
#include <boost/asio/buffer.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/read_until.hpp>
#include <boost/asio/read.hpp>
// Minimal example, code that works has error checking removed. Class members turned itno globals etc.
namespace {
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::socket sock(io_service);
std::vector<char> data(512);
boost::asio::mutable_buffers_1 buffer(&data[0], data.size());
unsigned read_counter = 1;
std::atomic<unsigned> read_timeout;
}
boost::system::error_code openSocket(const std::string &server,
const std::string &port)
{
boost::system::error_code error = boost::asio::error::host_not_found;
using boost::asio::ip::tcp;
tcp::resolver::query query(server, port);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;
while (error && endpoint_iterator != end)
{
sock.close();
sock.connect(*endpoint_iterator++, error);
}
if (error)
{
std::cerr << "No route\n";
sock.close(); // Would be wrong to leave it open.
}
return error;
}
int read(size_t bytesNeeded)
{
size_t buffer_len = boost::asio::buffer_size(buffer);
size_t byteShift = buffer_len - bytesNeeded; // Read into back of buffer.
const int timeoutSeconds = 10;
boost::asio::deadline_timer deadline(io_service);
deadline.expires_from_now(boost::posix_time::seconds(timeoutSeconds)); // This will reset any outstanding timer
read_counter += 2; // If we'd use +1, after 4 billion cycles it would reset to 0
read_timeout.store(0, std::memory_order_release); // 0 = no timeout.
unsigned read_counter_copy = read_counter; // Can't capture global.
deadline.async_wait([read_counter_copy](boost::system::error_code const&) {
// read_timeout is very intentionally captured by value - timeout events are numbered
read_timeout.store(read_counter_copy, std::memory_order_release); }
);
// Start reading "asynchronously", wait for completion or timeout:
std::atomic<boost::system::error_code> ec(boost::asio::error::would_block);
size_t len = 0;
boost::asio::async_read(sock, boost::asio::buffer(buffer + byteShift), boost::asio::transfer_exactly(bytesNeeded),
[&, bytesNeeded](boost::system::error_code const& err, size_t bytesTransferred)
{
if (bytesTransferred != bytesNeeded) {
std::cout << bytesTransferred << " , " << err.message() << std::endl;
}
len = bytesTransferred;
ec.store(err, std::memory_order_release);
}
);
do {
io_service.run_one();
} while (read_timeout.load(std::memory_order_acquire) != read_counter && // Continue if the **last** read didn't time out
(ec.load(std::memory_order_acquire) == boost::asio::error::would_block) && // ec.store() not called,
!io_service.stopped()); // and program still running.
deadline.cancel(); // This will set read_timeout, if it wasn't set yet. But we ignore it from now on.
if (ec.load(std::memory_order_acquire))
{
std::cerr << "oops\n"; // Real error handling omitted.
throw std::runtime_error("");
}
else if (read_timeout == read_counter)
{
std::cerr << "timeout\n";
}
else if (len != bytesNeeded)
{
// This is the real problem.
std::cerr << "Asked " << bytesNeeded << " got " << len;
}
return (int)len;
}
int main(int argc, char* argv[])
{
do try {
::openSocket("192.168.6.30", "80");
read(148); // Assume that data[] already has 364 bytes on the first call.
for (;;)
{
read(512); // Full buffers on every subsequent call.
// Do something with data[] here.
}
}
catch (std::runtime_error) { } while (true);
}
的do try catch while
是必要的,因爲錯誤僅發生後我拔掉的另一側。在第二次調用read(148)
後,下一次讀取(512)`失敗。
[更新] 這不僅僅是transfer_exactly
。與transfer_at_least(512)
我也遇到了同樣的問題,一個多餘的148字節讀取。 (兩者應具有相同的行爲,如讀至少 512字節到緩衝區這僅 512字節不能讀取更多或更少字節)
你能提供[MCVE]嗎?你使用哪種增強版本? –
它說它應該填充緩衝區?指定所有讀取方法以返回實際傳輸的字節數,因爲它們可能*不填充緩衝區。在套接字的情況下,如果一次只有148個字節到達,那就是你會得到的。 – EJP
@EJP http://www.boost.org/doc/libs/1_60_0/doc/html/booster_asio/reference/transfer_exactly.html – sehe