Boost logo

Boost Users :

From: James Vanns (james.vanns_at_[hidden])
Date: 2008-07-02 08:11:13


Hi, I don't quite understand how to register an async call back once with an IO service? Consider this model:

* Server accepts connection
* connection object *registers* it's read callback (to be executed whenever there is a
  read available from the underlying epoll)
* connection object is doing some processing or conversing with the main server etc.
* epoll (or IO service) says there is data to be read
* call back gets executed
* the processing resumes (based on the newly received data)

I'm prettu sure that is normal. But how do I actually achieve this with ASIO? I mean, I have the main server that runs io_service::run() so it knows when there is data to be read on a socket - that socket is of course associated with this client connection object and it is this object that acts on the data (and may also send of course). However, using the async_read methods I appear to have to keep re-registering the callback in the actual callback:

client::register_read_callback()
{
    m_socket.async_read_some(buffer(m_inbound, p_max),
                             boost::bind(&base_io::read_callback,
                                         shared_from_this(),
                                         placeholders::error,
                                         placeholders::bytes_transferred));

}

client::read_callback(error_code &ec, size_t bytes)
{
   // I have to call register_read_callback() again here!
}

client::start() // I get called when I am established between two endpoints
{
   register_read_callback();
   do_some_processing();
}

client::do_some_processing()
{
}

I'm trying to build a library that transparently buffers messages received from an endpoint. THe library silently receives the data (through the epoll based io service and the asio tcp sockets), builds the messages from the received data and puts them in a stack ready to be poppped off by an application calling pop_msg().

I'm having difficulty in separating these two operations.

Regards,

Jim Vanns

-- 
James Vanns
Systems Programmer
Framestore CFC Ltd.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net