lxgeek
lxgeek

Reputation: 1814

Why the timer can't work when use libary of asio?

the codes is:

Initialize the object:

tcp_client tcp_client_obj(*this,io_service,tcp_peer_context, ip_, port_, "", datas, ret, stdout_msgs, stderr_msgs,peer_check_flag_,deploy_type_);
            io_service.run();

the member functions:

  void tcp_client::handle_total_timeout(const boost::system::error_code& error_)
       {
            if (error_ != boost::asio::error::operation_aborted)
            {
                stderr_msgs.push_back("proxy total timeout");
                ret = MESSAGE_ERROR_CLIENT_TOTAL_TIMEOUT;
                close();
            }
        }

    void tcp_client::start()
        {
            boost::asio::ip::tcp::resolver resolver(socket.get_io_service());
            string temp = boost::lexical_cast<string>(port);
            boost::asio::ip::tcp::resolver::query query(ip.c_str(), temp.c_str());
            boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);


            total_timer.expires_from_now(boost::posix_time::seconds(total_timeout));
            total_timer.async_wait(boost::bind(&tcp_client::handle_total_timeout,  this, boost::asio::placeholders::error));

            connect_timer.expires_from_now(boost::posix_time::seconds(connect_timeout));
            connect_timer.async_wait(boost::bind(&tcp_client::handle_connect_timeout,  this, boost::asio::placeholders::error));

            now_time = global::currenttime::getms();
            boost::asio::async_connect(socket.lowest_layer(), iterator, boost::bind(&tcp_client::handle_connect, this,boost::asio::placeholders::error));
            LDeployInfo(str(format("[TCP Client] Connecting server@%s:%d") % ip % port));
        }

And I want to test the function of handle_total_time(). So I add the codes

bool tcp_client::check_release_md5()
    {
          while()
          {
             //just test this.
          }
          /*
           * do somethings.
           */
         }

the check_release_md5 will be called after the function of start().
Why the handle_total_timeout() can't work?
Actually,the check_release_md5 will run forever.

Upvotes: 0

Views: 85

Answers (1)

DeVadder
DeVadder

Reputation: 1404

edit: Dear people from the future: As it turned out, the problem was that the first handler that was called, never returned. As such io_service was unable to post() the second one, which would have cancelled the first.

First let me state my personal opinion: Mark member variables in some way.

Do you guarantee that io_service.run() it is still running when you get here? To be more precise, do you maybe call io_service.run() after constructing but before calling start()?

In that case io_service has returned already and nothing will be handled. And another possibility: Did io_service maybe run() at some earlier point in your code and you forgot to reset() it?

Upvotes: 2

Related Questions