提升deadline_timer并没有开始

时间:2011-08-07 10:39:53

标签: boost asynchronous boost-asio

我们已经基于boost asio编写了一个单线程客户端。客户端需要实现超时,以便在规定的时间段内未完成服务器的读取或写入时连接中断。但deadline_timer的async_wait没有启动,直到我没有为io_service调用run。现在,如果我在io_service上调用run,那么我就无法读取和写入服务器。

请参阅我当前代码的摘录:

  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_sCommAddress
            ), 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
                                                    )
                                       );
    }
 }

我需要连接,写入服务器,然后从服务器获得响应,对于每个操作,我需要启动超时。如果我从io_service调用run,那么我就不能进行这三次调用。

1 个答案:

答案 0 :(得分:1)

  

我需要连接,写入服务器,然后从中获取响应   服务器和每个操作我需要启动超时。如果我打电话   从io_service运行然后我无法进行这三次调用。

使用deadline_timer::async_wait()时,您需要使用相应的异步套接字方法,例如socket::async_connect()而不是socket::connect()