Boost logo

Boost Users :

Subject: Re: [Boost-users] [asio] async write handler is not called until the socket is not closed
From: Igor R (boost.lists_at_[hidden])
Date: 2009-05-01 08:01:43


> Let's imagine one IO thread. on timer event (each second) it pushes X
> messages to socket with async_write.
> Also, at this time, the IO thread receives Y messages with async_read
> through the same socket.
> Is there any limit in pushing messages with async_write? What it is?

I know nothing about such a limit, but I suspect another issue in your
approach: your description sounds like you don't ensure that
async_write is complete (i.e. the handler is called) before issuing
another one. If my assumption is correct, you might run into various
problems, as the order of execution of your async_writes is undefined.
The same applies to async_read's.
The correct approach would be like this:

// pseudo-code!!
class connection : public enable_shared_from_this<connection>
{
public:
void send(Data data)
{
  socket_.get_io_service().post(bind(&connection::syncSend,
shared_from_this(), data));
}

private:
void syncSend(Data data)
{
  // enqueue the data and ensure the write is active
  someContainer_.push_back(data);
  activeWrite();
}

void activateWrite()
{
  if (!activeWrite_ && unsentDataExists)
  {
    activeWrite_ = true;
    socket_.async_write(allTheData, bind(&connection::sent,
shared_from_this(), _1))
  }
}

void sent()
{
  activeWrite_ = false;
  activateWrite();
}
};


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