包括ping超时功能

时间:2014-06-24 19:21:35

标签: c++ boost-asio

我有服务器A从服务器B接收更新。我想在服务器A中添加功能,如果它在1分钟内没有收到消息(服务器B将发送更新和ping消息),服务器A将会进入暂停状态并等待消息再次进入。

我正在研究一个boost :: asio :: deadline_timer,但是我无法弄清楚它是否可能,或者你是否可以异步运行它。我尝试了一个在自己的线程中运行并使用截止时间计时器的类,但我无法取消并重新启动截止时间计时器。这是我用过的一些示例代码。

实施:

void ping_timeout::reset_timer()
{
    ping_timeout_.cancel();
    ping_timeout_.expires_from_now(boost::posix_time::seconds(60));
    //Call to clear the cache of a static class, which is the paused state I would like
    ping_timeout_.async_wait(boost::bind(&cache::empty_cache));
    io_.run();
}

我无法通过调用重置计时器从我的主执行线程中取消截止时间计时器,我猜是因为io_.run()正在等待60秒到期。

我可以做任何修改吗,我可以通过任何库来实现我想要的结果吗?任何帮助将不胜感激。

谢谢

编辑:

主循环:

ping_timeout timeout;
boost::thread(boost::bind(&cache::run_io,boost::ref(service)));
while(true)
{
    std::string message = s_recv(subscriber);
    }
    if(message.compare("UPDATE") == 0)
    {
       //Process update 
    }
    else if(message.compare("PING") == 0)
    {
        timeout.reset_timer();
    }
}

编辑2:

工作代码:

void cache::process_cache()
{
    boost::asio::io_service service;
    boost::asio::io_service::work work(service);
    boost::thread(boost::bind(&cache::run_io,boost::ref(service)));

    boost::asio::deadline_timer timer(service,boost::posix_time::seconds(60));
    timer.async_wait(boost::bind(&cache::empty_cache,boost::asio::placeholders::error));

    while(true)
    {
        std::string message = s_recv(subscriber);
        if(message.compare("UPDATE") == 0)
        {
            //Process update 
        }
        else if(message.compare("PING") == 0)
        {
            timer.cancel();
            timer.expires_from_now(boost::posix_time::seconds(60));
            timer.async_wait(boost::bind(&cache::empty_cache,boost::asio::placeholders::error));
        }
    }
}

void cache::empty_cache(const boost::system::error_code& e)
{
    if(e.value() == 0)
    {
        //Clear cache  
    }
}


void cache::run_io(boost::asio::io_service& io)
{
    io.run();
}

1 个答案:

答案 0 :(得分:1)

boost::asio::io_service::run()是一个阻止通话。在您的具体情况下,您应该避免在主线程中调用它。

注意:在典型的异步驱动应用中,您应该围绕run方法构建您的应用。

对于定时器代码逻辑,类似的东西应该起作用:

boost::asio::io_service service;
// Creates a work object to prevent the thread from exiting after the first job is done
boost::asio::io_service::work work(service);

// Creates the timer and post the aync wait now, will only start when service.run() is called
boost::asio::deadline_timer timer(service, boost::posix_time::seconds(60));
timer.async_wait(boost::bind(&cache::empty_cache, ...));

// Starts the worker thread to allow the timer to asynchronously waits
boost::thread ping_thread(boost::bind(&boost::asio::io_service::run, &service));

while (true) // you should add a condition in order to leave if the timer expires
{
    std::string message = s_recv(subscriber);

    /**/ if (message == "UPDATE")
    {
        // Process update
    }
    else if (message == "PING")
    {
        // Cancel the current timer
        timer.cancel();
        // Start another async wait
        timer.async_wait(boost::bind(&cache::empty_cache, ...));
    }
}