async_xxx函數模板化完成處理程序的類型。該處理程序不必是簡單的「回調」,它可以是任何公開正確的operator()簽名的東西。
你應該這樣能夠做這樣的事情:
// Warning: Not tested
struct MyReadHandler
{
MyReadHandler(Whatever ContextInformation) : m_Context(ContextInformation){}
void
operator()(const boost::system::error_code& error, std::size_t bytes_transferred)
{
// Use m_Context
// ...
}
Whatever m_Context;
};
boost::asio::async_read(socket, buffer, MyReadHander(the_context));
或者,你也可以有你的處理程序作爲一個普通的功能,並在調用點綁定它,如asio tutorial描述。上面的例子將是:
void
HandleRead(
const boost::system::error_code& error,
std::size_t bytes_transferred
Whatever context
)
{
//...
}
boost::asio::async_read(socket, buffer, boost::bind(&HandleRead,
boost::asio::placeholders::error_code,
boost::asio::placeholders::bytes_transferred,
the_context
));
謝謝!這工作完美。 – fingerprint211b 2010-01-22 16:51:43