XMPPTimer.m
1.71 KB
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
#import "XMPPTimer.h"
#import "XMPPLogging.h"
// Log levels: off, error, warn, info, verbose
// Log flags: trace
#if DEBUG
static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#else
static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#endif
@implementation XMPPTimer
{
BOOL isStarted;
dispatch_time_t start;
uint64_t timeout;
uint64_t interval;
dispatch_source_t timer;
}
- (instancetype)initWithQueue:(dispatch_queue_t)queue eventHandler:(dispatch_block_t)block
{
if ((self = [super init]))
{
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_event_handler(timer, block);
isStarted = NO;
}
return self;
}
- (void)dealloc
{
[self cancel];
}
- (void)startWithTimeout:(NSTimeInterval)inTimeout interval:(NSTimeInterval)inInterval
{
if (isStarted)
{
XMPPLogWarn(@"Unable to start timer - already started");
return;
}
start = dispatch_time(DISPATCH_TIME_NOW, 0);
timeout = (inTimeout * NSEC_PER_SEC);
interval = (inInterval > 0.0) ? (inInterval * NSEC_PER_SEC) : DISPATCH_TIME_FOREVER;
dispatch_source_set_timer(timer, dispatch_time(start, timeout), interval, 0);
dispatch_resume(timer);
isStarted = YES;
}
- (void)updateTimeout:(NSTimeInterval)inTimeout fromOriginalStartTime:(BOOL)useOriginalStartTime
{
if (!isStarted)
{
XMPPLogWarn(@"Unable to update timer - not yet started");
return;
}
if (!useOriginalStartTime) {
start = dispatch_time(DISPATCH_TIME_NOW, 0);
}
timeout = (inTimeout * NSEC_PER_SEC);
dispatch_source_set_timer(timer, dispatch_time(start, timeout), interval, 0);
}
- (void)cancel
{
if (timer)
{
dispatch_source_cancel(timer);
#if !OS_OBJECT_USE_OBJC
dispatch_release(timer);
#endif
timer = NULL;
}
}
@end