I would like to implement a timeout while accepting a connection on a
TCP socket. I have reviewed the following example:
http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/example/timeouts/accept_timeout.cpp
And here is my implementation of it, though it does not seem to wait
for the given timeout... And I don't seem to see
CProtobufSocket::HandleAccept being called.
Could anybody tell me what I am missing with this code?
Thanks!
Jean-Sebastien
int CProtobufSocket::Receive(
string port,
google::protobuf::Message & message,
const boost::posix_time::milliseconds timeout
)
{
int result = -1;
boost::asio::io_service ioService;
boost::asio::ip::tcp::acceptor acceptor(ioService,
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),
atoi(port.data())));
boost::asio::ip::tcp::socket socket(ioService);
try
{
boost::asio::deadline_timer mytimer(ioService);
acceptor.async_accept(socket,
boost::bind(CProtobufSocket::HandleAccept,
boost::asio::placeholders::error, result));
mytimer.expires_from_now(timeout);
mytimer.async_wait(boost::bind(CProtobufSocket::Close, &acceptor));
if(0 == result)
{
// receive on the socket.
result = Receive(socket, message, timeout);
}
else
{}
}
catch (std::exception& e)
{
// jstoezel 2910208: removes warning C4101: 'e' : unreferenced
local variable
e;
}
try
{
socket.shutdown(socket.shutdown_both);
socket.close();
}
catch (std::exception& e)
{
// jstoezel 2910208: removes warning C4101: 'e' : unreferenced
local variable
e;
}
return result;
}
void CProtobufSocket::Close(
boost::asio::ip::tcp::acceptor * acceptor
)
{
acceptor->close();
}
void CProtobufSocket::HandleAccept(const boost::system::error_code
&err, int & result)
{
err;
if(err)
{}
else
{
result = 0;
}
}
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
In your real code do you also define the timer as a local object? If
so, it's destroyed by the end of the scope.
Well I did a copy/paste of what the code is, so wysiwyg ;). I have
moved the timer outside of the try block, though I still get the same
behavior, mytimer.async_wait returns right away with no connection...
Since mytimer.async_wait woudl block, my understanding is that the
timer would not be destroyed until the code resumes and get outside of
the scope where it was defined...
Am I missing something?
Jean
int CProtobufSocket::Receive(
string port,
google::protobuf::Message & message,
const boost::posix_time::milliseconds timeout
)
{
int result = -1;
boost::asio::io_service ioService;
boost::asio::deadline_timer mytimer(ioService);
//boost::asio::ip::tcp::acceptor acceptor(ioService,
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),
atoi(port.data())));
boost::asio::ip::tcp::acceptor acceptor(ioService,
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 7777));
boost::asio::ip::tcp::socket socket(ioService);
try
{
acceptor.async_accept(socket,
boost::bind(&CProtobufSocket::HandleAccept,
boost::asio::placeholders::error, result));
//mytimer.expires_from_now(timeout);
mytimer.expires_from_now(timeout);
mytimer.async_wait(boost::bind(&CProtobufSocket::Close, &acceptor));
async_XXX operations never block, that's why they're called asynchronous :).
You can use a blocking version: myTimer.wait(), if that's what you need.