views:

173

answers:

1

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(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

+2  A: 

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.

Gianni
yes, I have an io_service in the main thread. I use it to create a shared_ptr to a boost::asio::ip::tcp::socket which I pass to the thread that reads from it.
shaz
@shaz but do you call the `run`or `run_one` functions? Those are the ones that do all the work.
Gianni
Thanks Gianni, that did it!
shaz