问题: 问题:我在boost :: asio中遇到async_read_some问题。有时变量m_deBuff在回调方法TCPSession :: handle_read中发生变化。我无法弄清楚为什么会这样?我在另一边使用tcp套接字,我已经通过wireshark分析了数据,看起来完好无损。我也在回送接口127.0.0.1上发送和接收。
当我把std :: cout<海峡;并且在handle_read内部睡觉。
void TCPSession::start( TCPSessionListner* parent )
{
std::cout << "TCPSession::started" << std::endl;
m_parent = parent;
m_socket.async_read_some(boost::asio::buffer(m_deBuff, MAX_LEN),
boost::bind(&TCPSession::handle_read, weak_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void TCPSession::handle_read(const boost::system::error_code& error, size_t bytes_transferred)
{
if (!error)
{
m_workingBuffer.insert(m_workingBuffer.end(), m_deBuff, m_deBuff + bytes_transferred);
if( m_parent != 0 )
{
if (false == m_rawRead)
{
std::vector< std::string > messages;
Messages::TcpStreamToMessages(m_workingBuffer, messages);
m_parent->onData( messages, m_sessionID );
}
if (false == m_workingBuffer.empty() && true == m_rawRead)
{
std::string str(m_workingBuffer.begin(), m_workingBuffer.end());
std::cout << str;
boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); // HERE, while debugging after std::cout m_deBuff has changed (only sometimes)...
//m_parent->onRawData(m_workingBuffer, m_sessionID);
m_workingBuffer.clear();
}
}
m_socket.async_read_some(boost::asio::buffer(m_deBuff, MAX_LEN),
boost::bind(&TCPSession::handle_read, weak_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
if( m_parent != 0 )
{
m_parent->onSessionDisconnected( m_sessionID );
}
}
}