Concord - C Discord API library
A Discord API wrapper library written in C
timers.c

Demonstrates the Timer API for callback scheduling

#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.");
}
}
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";
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);
}
CCORDcode ccord_global_init()
Initialize global shared-resources not API-specific.
void ccord_global_cleanup()
Cleanup global shared-resources.
Public functions and datatypes.
void discord_cleanup(struct discord *client)
Free a Discord Client handle.
CCORDcode discord_run(struct discord *client)
Start a connection to the Discord Gateway.
struct discord * discord_config_init(const char config_file[])
Create a Discord Client handle by a config.json file.
bool discord_timer_cancel(struct discord *client, unsigned id)
cancels a timer, this will delete the timer if DISCORD_TIMER_DELETE_AUTO is enabled
unsigned discord_timer_interval(struct discord *client, discord_ev_timer on_tick_cb, discord_ev_timer on_status_changed_cb, void *data, int64_t delay, int64_t interval, int64_t repeat)
creates a repeating timer that automatically deletes itself upon completion
unsigned discord_timer(struct discord *client, discord_ev_timer on_tick_cb, discord_ev_timer on_status_changed_cb, void *data, int64_t delay)
creates a one shot timer that automatically deletes itself upon completion
@ DISCORD_TIMER_TICK
Definition: discord.h:365
@ DISCORD_TIMER_CANCELED
Definition: discord.h:363
@ DISCORD_TIMER_DELETE
Definition: discord.h:359
struct used for modifying, and getting info about a timer
Definition: discord.h:373
void * data
Definition: discord.h:384
enum discord_timer_flags flags
Definition: discord.h:377
unsigned id
Definition: discord.h:375
int64_t interval
Definition: discord.h:388
int64_t repeat
Definition: discord.h:390
int64_t delay
Definition: discord.h:386
The Discord client handler.
Definition: discord-internal.h:1190