Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::asio - Can't get timeout on accept to work
From: Jean-Sebastien Stoezel (js.stoezel_at_[hidden])
Date: 2010-02-18 10:26:02


Hello,

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));

On Thu, Feb 18, 2010 at 9:18 AM, Igor R <boost.lists_at_[hidden]> wrote:
>>   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));
>
>
> 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.
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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