-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
|
||
#include "discord.h" | ||
#include "discord-internal.h" | ||
|
||
struct timer_data { | ||
discord_ev_timer cb; | ||
void *data; | ||
int64_t interval; | ||
int flags; | ||
}; | ||
|
||
static int | ||
cmp_timers(const void *a, const void *b) { | ||
const int64_t l = *(int64_t *)a; | ||
const int64_t r = *(int64_t *)b; | ||
if (l == r || (l < 0 && r < 0)) | ||
return 0; | ||
if (l < 0) return 1; | ||
if (r < 0) return -1; | ||
return l > r ? 1 : -1; | ||
} | ||
|
||
void | ||
discord_timers_init(struct discord *client) { | ||
client->timers.internal.q = | ||
priority_queue_create(sizeof(int64_t), sizeof(struct timer_data), | ||
cmp_timers, 0); | ||
client->timers.user.q = | ||
priority_queue_create(sizeof(int64_t), sizeof(struct timer_data), | ||
cmp_timers, 0); | ||
} | ||
|
||
void | ||
discord_timers_cleanup(struct discord *client) { | ||
priority_queue_destroy(client->timers.user.q); | ||
priority_queue_destroy(client->timers.internal.q); | ||
} | ||
|
||
unsigned | ||
discord_timer_ctl( | ||
struct discord *client, struct discord_timer *timer, unsigned id, | ||
discord_ev_timer timer_cb, | ||
void *data, int64_t start_after, int64_t interval, int flags) | ||
{ | ||
struct timer_data tdata = { | ||
.cb = timer_cb, | ||
.data = data, | ||
.interval = interval, | ||
.flags = flags, | ||
}; | ||
int64_t now = start_after >= 0 | ||
? (int64_t)discord_timestamp_us(client) + start_after * 1000 | ||
: -1; | ||
if (!id) { | ||
return priority_queue_push(timer->q, &now, &tdata); | ||
} else { | ||
return priority_queue_update(timer->q, id, &now, &tdata) ? id : 0; | ||
} | ||
} | ||
|
||
void | ||
discord_timers_run(struct discord *client, struct discord_timer *timer) { | ||
const int64_t now = (int64_t)discord_timestamp_us(client); | ||
priority_queue_id id; | ||
struct timer_data data; | ||
for (int64_t trigger; | ||
(id = priority_queue_peek(timer->q, &trigger, &data));) | ||
{ | ||
if (trigger > now || trigger == -1) return; | ||
if (data.cb) | ||
data.cb(client, id, data.data, 0); | ||
if (data.interval) { | ||
int64_t next = now + data.interval * 1000; | ||
priority_queue_update(timer->q, id, &next, &data); | ||
} else priority_queue_pop(timer->q, NULL, NULL); | ||
} | ||
} | ||
|
||
unsigned | ||
discord_timer( | ||
struct discord *client, unsigned id, | ||
discord_ev_timer timer_cb, void *data, | ||
int64_t start_after, int64_t interval, int flags) | ||
{ | ||
return discord_timer_ctl(client, &client->timers.user, id, timer_cb, data, | ||
start_after, interval, flags); | ||
} |