2012-05-26 77 views
12

我使用這個代碼讀取使用流緩衝緩衝升壓ASIO讀寫

socket_.async_read_some(boost::asio::buffer(data_, max_length), 
     boost::bind(&session::handle_read, this, 
     boost::asio::placeholders::error, 
     boost::asio::placeholders::bytes_transferred)); 

,這寫

boost::asio::async_write(socket_, 
    boost::asio::buffer(data_, bytes_transferred), 
    boost::bind(&session::handle_write, this, 
    boost::asio::placeholders::error)); 

其中socket_的插座,MAX_LENGTH是用枚舉值1024 data_是長度爲max_length的char數組。

但我想用streambuf替換char數組緩衝區。我試過

boost::asio::streambuf streamBuffer; 
    socket_.async_read_some(boost::asio::buffer(streamBuffer), 
     boost::bind(&session::handle_read, this, 
     boost::asio::placeholders::error, 
     boost::asio::placeholders::bytes_transferred)); 

但是不行。我該怎麼做 ?

回答

12

您需要從streambuf獲得mutable_buffers_type才能用作async_read_some的第一個參數。

boost::asio::streambuf streamBuffer; 
    boost::asio::streambuf::mutable_buffers_type mutableBuffer = 
     streamBuffer.prepare(max_length); 
    socket_.async_read_some(boost::asio::buffer(mutableBuffer), 
     boost::bind(&session::handle_read, this, 
     boost::asio::placeholders::error, 
     boost::asio::placeholders::bytes_transferred)); 

見獲取更多信息asio文檔herehere