summaryrefslogtreecommitdiff
path: root/pjlib/src/pj++/timer.hpp
blob: ccca633ad81d6ffb9f2a64aa4c3cb00c83199fab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* $Header: /pjproject/pjlib/src/pj++/timer.hpp 4     8/24/05 10:29a Bennylp $ */
#ifndef __PJPP_TIMER_H__
#define __PJPP_TIMER_H__

#include <pj/timer.h>
#include <pj++/types.hpp>

class PJ_Timer_Heap;

class PJ_Timer_Entry : private pj_timer_entry
{
    friend class PJ_Timer_Heap;

public:
    static void timer_heap_callback(pj_timer_heap_t *, pj_timer_entry *);

    PJ_Timer_Entry() { cb = &timer_heap_callback; }
    PJ_Timer_Entry(int arg_id, void *arg_user_data)
    {
	cb = &timer_heap_callback; 
	init(arg_id, arg_user_data);
    }

    virtual void on_timeout() = 0;

    void init(int arg_id, void *arg_user_data)
    {
	id = arg_id;
	user_data = arg_user_data;
    }

    int get_id() const
    {
	return id;
    }

    void set_id(int arg_id)
    {
	id = arg_id;
    }

    void set_user_data(void *arg_user_data)
    {
	user_data = arg_user_data;
    }

    void *get_user_data() const
    {
	return user_data;
    }

    const PJ_Time_Val &get_timeout() const
    {
	pj_assert(sizeof(PJ_Time_Val) == sizeof(pj_time_val));
	return (PJ_Time_Val&)_timer_value;
    }
};

class PJ_Timer_Heap
{
public:
    PJ_Timer_Heap() {}

    bool create(PJ_Pool *pool, pj_size_t initial_count, 
		unsigned flag = PJ_TIMER_HEAP_SYNCHRONIZE)
    {
	ht_ = pj_timer_heap_create(pool->pool_(), initial_count, flag);
	return ht_ != NULL;
    }

    pj_timer_heap_t *get_timer_heap()
    {
	return ht_;
    }

    bool schedule( PJ_Timer_Entry *ent, const PJ_Time_Val &delay)
    {
	return pj_timer_heap_schedule(ht_, ent, &delay) == 0;
    }

    bool cancel(PJ_Timer_Entry *ent)
    {
	return pj_timer_heap_cancel(ht_, ent) == 1;
    }

    pj_size_t count()
    {
	return pj_timer_heap_count(ht_);
    }

    void earliest_time(PJ_Time_Val *t)
    {
	pj_timer_heap_earliest_time(ht_, t);
    }

    int poll(PJ_Time_Val *next_delay = NULL)
    {
	return pj_timer_heap_poll(ht_, next_delay);
    }

private:
    pj_timer_heap_t *ht_;
};

#endif	/* __PJPP_TIMER_H__ */