NAME

on_counter_change - event that gets triggered when a counter gets modified.

SYNOPSIS

#include <game-carrier/server.h>

GC_ADAPTER_EVENT
void on_counter_change(
    GCT_INTPTR user,
    GCT_INT id,
    GCT_COUNTER_VALUE value);

Parameters:

  • user GCT_INTPTR User data retrieved after counterSubscribe call.
  • id GCT_INT Unique identifier of the counter passed to counterSubscribe call.
  • value GCT_COUNTER_VALUE The current value of the counter.

RETURN VALUE

None.

DESCRIPTION

The on_counter_change function represents an event triggered when a counter undergoes modification. This event occurs within a thread responsible for modifying the counter’s value.

The user parameter corresponds to user data that was provided during the counterSubscribe function call.

The id parameter denotes the identifier of the counter that was passed to the counterSubscribe function.

The value parameter represents the current value of the counter.

EXAMPLE

#include <game-carrier/server.h>

#include <stdlib.h>

static struct core_api api;
static GCT_INT adapter_id;

static GCT_INT free_memory_counter_id;
static GCT_INT free_memory_subscription_id;

GC_ADAPTER_EVENT
GCT_INT adapter_initialize(
    GCT_INT id,
    struct core_api * core_api,
    GCT_CPTR load_path)
{
    adapter_id = id;
    api = *core_api;
    return 0;
}

GC_ADAPTER_EVENT
GCT_INT application_initialize(
    GCT_INT id,
    GCT_CSTR name,
    GCT_CSTR type_name,
    GCT_CSTR file_name)
{
    free_memory_counter_id = api.counterOpen("System.Memory.Free.Bytes.Current");
    if (free_memory_counter_id == -1) {
        return -1;
    }

    free_memory_subscription_id = api.counterSubscribe(adapter_id, free_memory_counter_id, NULL);
    if (free_memory_subscription_id == 0) {
        return -1;
    }

    /* Continue application initialization */
    return 0;
}

GC_ADAPTER_EVENT
void on_counter_change(
    GCT_INTPTR user,
    GCT_INT id,
    GCT_INTPTR value)

{
    if (id == free_memory_counter_id) {
        if (value > 90) {
            api.logMessage(GCL_WARN, "Alarm! Alarm! Alarm!");
        }
        api.counterUnsubscribe(free_memory_subscription_id);
    }
}

SEE ALSO

counterCreate, counterCreateAtomic, counterOpen, counterGetNames, counterSubscribe, counterUnsubscribe