c++ - 从用于 boost deadline_timer 的处理程序访问类数据

标签 c++ boost boost-asio

我能够为 boost deadline_time(它是一个成员)创建一个处理程序 通过声明它是静态的。不幸的是,这会阻止访问非静态成员数据。

我有一系列超时。所以我的想法是有一个单一的 deadline_timer 同时维护超时事件的有序列表。 每次下一次超时事件发生时, 该类将使用该类中的下一个超时事件重新触发计时器 计算此超时事件的剩余时间。

要使这个概念起作用,处理程序需要操纵 非静态数据。但这是不可能的,因为 boost::asio 需要静态处理程序。

有人知道如何处理这个吗?

class TimerController {
public:
void setTimer(const eibaddr_t gad, const timesecs_t timedelay);  
void cancelTimer(const eibaddr_t gad);
bool isRunning(const eibaddr_t gad);
void setGad(const eibaddr_t gad);
static void timerHandler(const boost::system::error_code &ec); 
private:
boost::asio::deadline_timer* m_pTimer;
struct timerList_s
{
    eibaddr_t gad;
    boost::posix_time::ptime absTimeOut;
    timerList_s(const timerList_s& elem) : gad(elem.gad),
                                           absTimeOut(elem.absTimeOut)
    {
    };
    timerList_s(const eibaddr_t& pgad, const boost::posix_time::ptime pato) 
        : gad(pgad),
          absTimeOut(pato)
    {
    };
    timerList_s& operator= (const timerList_s& elem)
    {
        gad = elem.gad;
        absTimeOut = elem.absTimeOut;
        return *this;
    };
    bool operator< (const timerList_s& elem) const
    {
        return (absTimeOut < elem.absTimeOut);
    };
    bool operator== (const timerList_s& elem) const
    {
        return (gad == elem.gad);
    };
};
std::list<timerList_s> m_timers;

最佳答案

可以使用 boost::bind 以下列方式将 deadline_timer 类与非静态数据一起使用 deadline_.async_wait(bind(&client: :check_deadline, 这个));. ASIO 示例中提供的详细信息,例如 here .

关于c++ - 从用于 boost deadline_timer 的处理程序访问类数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9775119/

相关文章:

android - Android 上的 Boost.Asio 因找不到服务而失败(Boost 1.53;NDK r8e)

c++ - 将 C++ 项目升级到 VS2015,但链接器仍在寻找 VC100 Boost 库

c++ - 如何将websocket客户端连接到服务器?

c++ - 在 c++ asio 中,如何为 http 或 https 创建相同的套接字对象(接口(interface)/抽象问题?)

c++ - 使用 Boost Asio 接受 IPv6 链接范围地址

c++ - 在将 unique_ptr 移动到同一对象的基类构造后,使用指向对象的原始指针初始化字段

c++ - 在 C 中声明两个同名的全局变量

c++ - boost::bind 和 io_service 出现问题

C++在可变参数模板内专门化可变参数模板

c# - 在程序内存中执行 BATCH 脚本