NAME

counterUnsubscribe - unsubscribes from counter modification events.

SYNOPSIS

#include <game-carrier/server.h>

typedef GCT_INT (GC_CORE_API *CounterUnsubscribe)(
    GCT_INT id);

typedef struct counterUnsubscribe {
    /* ... Some fields ... */
    CounterUnsubscribe counterUnsubscribe;
    /* ... Some fields ... */
}

Parameters:

  • id GCT_INT The unique identifier of the counter.

RETURN VALUE

Returns 0 upon success.

In case of failure, an error code such as EINVAL is returned.

DESCRIPTION

The counterUnsubscribe function is used to unsubscribe from counter modification events. The subscription must be created before calling this function.

The id parameter expects the unique identifier of the subscription to unsubscribe from modification events. The subscription identifier is returned upon calling the ‘counterSubscribe’ function.

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

on_counter_change, counterSubscribe, counterCreate, counterCreateAtomic, counterOpen, counterGetNames