Boost logo

Boost Users :

Subject: Re: [Boost-users] [asio] Closing async client socket after writing last message
From: Lee Clagett (forum_at_[hidden])
Date: 2014-06-18 23:15:29


On Wed, Jun 18, 2014 at 4:49 PM, Nicholas Yue <yue.nicholas_at_[hidden]>
wrote:

> On 18 June 2014 12:59, Bjorn Reese <breese_at_[hidden]> wrote:
>
>> On 06/18/2014 08:48 PM, Nicholas Yue wrote:
>>
>> write_msgs_.pop_front();
>>> if (!write_msgs_.empty())
>>>
>>
>> Swap these two lines; otherwise you do not write the last message.
>>
> Thanks for the heads up.
>
> Does that mean that the examples will example this problem ?
>
>
> http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp03/chat/chat_client.cpp
>
> as it does a pop before checking
>
> Cheers
> --
> Nicholas Yue
>

AFAIK this isn't a bug in the example code or in your adaption. The
implementation does a push_back() before the async_write request, and does
a pop_front() when it completes. So its using the std::deque to store the
contents of the message during the async operation. The .empty() check is
done in the async_write callback in case multiple messages were queued
while an async_write was still pending.

As for your original question - information was a little sparse, so I will
guess that you want the client to write out all of the queued messages and
then initiate the socket close. You need a mechanism for delaying a close
request until a future point in time. The close() method will have to
"post" another method (safe_close perhaps) that only closes the connection
if the queue is empty - otherwise safe_close starts yet-another-event to be
handled in the future. This other event can be an async timer or an
immediate change to a flag that handle_write reads when the queue is empty.
I'm not aware of any other implementations. Both of my suggested
implementations (timer or flag) have drawbacks, so the particular
application has to be considered.

Lee



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