开发者

boost deadline_timer not kicking off

We have written a single threaded client based on the boost asio. The client needs to implement a timeout, so that the connection is broken if the read or write from server was not completed in a stipulated time period. But the async_wait for the deadline_timer does not kick off untill I don't call run for the io_service. Now if I call run on the io_service then my reading and writing to the server is not possible.

Please see the excerpts from my current code:

  typedef boost::scoped_ptr<boost::asio::ip::tcp::socket> SocketPtr;
  typedef boost::shared_ptr<boost::asio::deadline_timer>  DLTPtr;

   SocketPtr m_SocketPtrClient;
   DLPtr m_ClientTimeoutDLTPtr;

    boost::asio::io_service ios;
    m_SocketPtrClient.reset( new boost::asio::ip::tcp::socket( ios));

    m_ClientTimeoutDLTPtr.reset( new deadline_timer( ios));

    m_ClientTimeoutDLTPtr->expires_from_now( boost::posix_time::seconds( m_uiCommTimeout));
    m_ClientTimeoutDLTPtr->async_wait( boost::bind( &MyClass::clientTimeoutHandler,
                                                      this,
                                                      boost::asio::placeholders::error
                                                    )
                                       );

    m_SocketPtrClient->connect
    (
        boost::asio::ip::tcp::endpoint
        (
            boost::asio::ip::address::from_string
            (
                m_sCommAddress == "localhost" ?  "127.0.0.1" : m_sC开发者_C百科ommAddress
            ), m_usCommPort
        ), ec
    );

    if( !ec && m_SocketPtrClient->is_open())
    {
        m_ClientTimeoutDLTPtr->cancel();
    }
    else
    {
        m_ClientTimeoutDLTPtr->cancel();
        m_SocketPtrClient->close();
        return eStateError;
    }

    //install a timeout handler
    m_ClientTimeoutDLTPtr->expires_from_now( boost::posix_time::seconds( m_uiCommTimeout));
    m_ClientTimeoutDLTPtr->async_wait( boost::bind( &MyClass::clientTimeoutHandler,
                                                      this,
                                                      boost::asio::placeholders::error
                                                    )
                                       );

    ec = writeToServer( *m_SocketPtrClient);
    if( ec)
    {
        // do error handling and throw an exception
    }
    m_ClientTimeoutDLTPtr->cancel();

    //install a timeout handler
    m_ClientTimeoutDLTPtr->expires_from_now( boost::posix_time::seconds( m_uiCommTimeout));
    m_ClientTimeoutDLTPtr->async_wait( boost::bind( &MyClass::clientTimeoutHandler,
                                                      this,
                                                      boost::asio::placeholders::error
                                                    )
                                       );
    ec = readFromServer( *m_SocketPtrClient);
    if( ec)
    {
        // do error handling and throw an exception
    }
    m_ClientTimeoutDLTPtr->cancel();


   void MyClass::clientTimeoutHandler( const boost::system::error_code& ec)
   {
    if( ec)
    {
      m_ClientTimeoutDLTPtr->cancel();
      m_SocketPtrClient->close();

      m_ssMsg << std::endl << "break all handling because of timeout on io_service of Client!";
     }
    else
    {
       m_ClientTimeoutDLTPtr->expires_from_now( boost::posix_time::seconds( m_uiCommTimeout));
       m_ClientTimeoutDLTPtr->async_wait( boost::bind( &MyClass::clientTimeoutHandler,
                                                      this,
                                                      boost::asio::placeholders::error
                                                    )
                                       );
    }
 }

I need to connect, write to the server and then get response from the server and for each operation I need the timeout to kickoff. If I call the run from io_service then I can't make my these three calls.


I need to connect, write to the server and then get response from the server and for each operation I need the timeout to kickoff. If I call the run from io_service then I can't make my these three calls.

When using deadline_timer::async_wait() you will need to use the corresponding asynchronous socket methods such as socket::async_connect() instead of socket::connect().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜