Trying to replace my boost::asio::read with boost::asio::async_read
So, the code I started with and which works (with important caveats below)
int reply_length = boost::asio::read(*m_socketptr, boost::asio::buffer((char*)reply, 6));
This works, I get the header which I then decode and follow up with another read which gets me my message and then I loop back to the top and read another header. This pegs my CPU at 100% so I want to replace the header read above with something like the following:
m_socketptr->async_read_some开发者_如何学JAVA(boost::asio::buffer(m_data, 6), boost::bind(&CSListener::handleRead, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
or
boost::asio::async_read(*m_socketptr, boost::asio::buffer(m_data, 6), boost::bind(&CSListener::handleRead, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
Either way I code it, the handleRead method is not ever getting called. Help!?
TIA
I assume you have created an io_service
somewhere in your code? You need to call its io_service.run()
or io_service.run_one()
to make it work. If you need it to be async, then run_one()
is you man; put a call to it in you app's/thread's main loop.
精彩评论