NAME

counterSubscribe - subscribes to counter modification events.

SYNOPSIS

#include <game-carrier/server.h>

typedef GCT_INT (GC_CORE_API *CounterSubscribe)(
    GCT_INT adapter,
    GCT_INT id,
    GCT_PTR user);

typedef struct counterSubscribe {
    /* ... Some fields ... */
    CounterSubscribe counterSubscribe;
    /* ... Some fields ... */
}

Parameters:

  • adapter GCT_INT The unique identifier of the adapter.
  • id GCT_INT The unique identifier of the counter.
  • user GCT_PTR A pointer to the user data to pass to the event.

RETURN VALUE

Returns the unique identifier of the subscription upon success.

Returns 0 on failure.

DESCRIPTION

The counterSubscribe function is used to subscribe to counter modification events. Prior to calling this function, the counter must be created or opened. The user parameter will be passed as an argument to the event function. To cancel events, the application can invoke counterUnsubscribe. The event will be triggered in a thread that modifies the counter value.

The adapter parameter represents the unique identifier of the adapter, which is used to call the ‘on_counter_change’ event.

The id parameter identifies the unique identifier of the counter to subscribe to modification events.

The user parameter denotes a pointer to the user data which will be passed to the event.

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, counterUnsubscribe, counterCreate, counterCreateAtomic, counterOpen, counterGetNames