-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtimers.c
86 lines (72 loc) · 2.35 KB
/
timers.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "discord.h"
#include "log.h"
static void
print_timer_info(struct discord_timer *timer, const char *name)
{
printf("Timer '%s' id:%u flags:%i "
"delay:%" PRIi64 " interval:%" PRIi64 " repeat:%" PRIi64 "\n",
name, timer->id, timer->flags, timer->delay, timer->interval,
timer->repeat);
}
static void
on_timer_tick(struct discord *client, struct discord_timer *timer)
{
print_timer_info(timer, "on_timer_tick");
if (timer->repeat == 1) {
puts("Canceling repeating timer.");
timer->flags |= DISCORD_TIMER_CANCELED;
}
}
static void
on_timer_status_changed(struct discord *client, struct discord_timer *timer)
{
print_timer_info(timer, "on_timer_status_changed");
if (timer->flags & DISCORD_TIMER_CANCELED) {
puts("TIMER CANCELED");
}
if (timer->flags & DISCORD_TIMER_DELETE) {
puts("TIMER DELETED - FREEING DATA");
}
}
static void
use_same_function(struct discord *client, struct discord_timer *timer)
{
print_timer_info(timer, "use_same_function");
if (timer->flags & DISCORD_TIMER_TICK) {
puts("TIMER TICK");
}
if (timer->flags & DISCORD_TIMER_CANCELED) {
puts("TIMER CANCELED");
}
if (timer->flags & DISCORD_TIMER_DELETE) {
puts("TIMER DELETED - FREEING DATA");
free(timer->data);
}
}
int
main(int argc, char *argv[])
{
const char *config_file = argc > 1 ? argv[1] : "../config.json";
ccord_global_init();
struct discord *client = discord_config_init(config_file);
for (int i = 0; i < 10; i++)
// one shot auto deleting timer
discord_timer(client, on_timer_tick, on_timer_status_changed, NULL,
i * 1000);
// repeating auto deleting timer
discord_timer_interval(client, on_timer_tick, on_timer_status_changed,
NULL, 0, 1000, 10);
discord_timer(client, use_same_function, use_same_function, malloc(1024),
1000);
unsigned id_to_cancel = discord_timer(
client, use_same_function, use_same_function, malloc(1024), 1000);
discord_timer_cancel(client, id_to_cancel);
discord_run(client);
// discord_cleanup will cancel all timers that are still active
discord_cleanup(client);
ccord_global_cleanup();
}