|
Boost Users : |
Subject: [Boost-users] [asio] Closing async client socket after writing last message
From: Nicholas Yue (yue.nicholas_at_[hidden])
Date: 2014-06-18 14:48:25
Hi,
I have adapted the chat tutorial example to send messages to a similar
(chat tutorial based) server.
It is currently working fine however due to the async_write(), I can't
close the socket until after all the pending data has been processed on the
server end.
I have google and found discussion and mentioned about async_write()
completion handler calling socket.close() however, I was not able to find
specific detail as to how that is done.
I want to avoid closing the connection prematurely as it is asynchronous
writing.
My current do_write() looks like this
void DisplayChatClient::do_write(DisplayChatMessage msg)
{
DLOG(INFO) << "DisplayChatClient::do_write()";
bool write_in_progress = !write_msgs_.empty();
write_msgs_.push_back(msg);
if (!write_in_progress)
{
boost::asio::async_write(socket_,
boost::asio::buffer(write_msgs_.front().data(),
write_msgs_.front().length()),
boost::bind(&DisplayChatClient::handle_write, this,
boost::asio::placeholders::error));
}
}
void DisplayChatClient::handle_write(const boost::system::error_code& error)
{
DLOG(INFO) << "DisplayChatClient::handle_write()";
if (!error)
{
write_msgs_.pop_front();
if (!write_msgs_.empty())
{
boost::asio::async_write(socket_,
boost::asio::buffer(write_msgs_.front().data(),
write_msgs_.front().length()),
boost::bind(&DisplayChatClient::handle_write, this,
boost::asio::placeholders::error));
}
}
else
{
do_close();
}
}
Cheers
-- Nicholas Yue Graphics - Arnold, Alembic, RenderMan, OpenGL, HDF5 Custom Dev - C++ porting, OSX, Linux, Windows http://au.linkedin.com/in/nicholasyue https://vimeo.com/channels/naiadtools
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