NAME

‘counterDecrement’ - decrements the value of a counter by one.

SYNOPSIS

#include <game-carrier/server.h>

typedef GCT_COUNTER_VALUE (GC_CORE_API *CounterDecrement)(
    GCT_INT id);

typedef struct counterDecrement {
    /* ... Some fields ... */
    CounterDecrement counterDecrement;
    /* ... Some fields ... */
}

Parameters:

  • id GCT_INT Unique identifier of the counter.

RETURN VALUE

Returns the new value of the counter upon success.

In the case of failure, returns the GC_COUNTER_INVALID_VALUE, which represents the smallest possible negative value for a 64-bit signed integer type.

DESCRIPTION

The ‘counterDecrement’ function is used to decrease the value of a counter by one. The counter is identified by its unique id. Prior to calling this function, the counter must be either created or opened.

It’s important to note that the function is thread safe only for atomic counters. Additionally, system counters cannot be modified by an application, and in such cases, the function will return the current value without any modifications.

The id parameter represents the unique identifier of the counter to decrement its value.

EXAMPLE

/* Example of a program that initializes a counter
for the number of connections to this application */

#include <game-carrier/server.h>

#include <stdio.h>

static struct core_api api;

/* Identifier of the counter used to store the number
of open connections to the current application. */
static GCT_INT app_conn_counter_id;


GC_ADAPTER_EVENT
GCT_INT adapter_initialize(
    GCT_INT adapter_id,
    struct core_api * core_api,
    GCT_CPTR load_path)
{
    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)
{
    app_conn_counter_id = api.counterCreateAtomic("MyApp.Connections.Current");
    if (app_conn_counter_id == -1)
    {
        return -1;
    }

    /* Continue application initialization */
    return 0;
}

GC_ADAPTER_EVENT
GCT_INTPTR on_connect(
    GCT_INT app_id,
    GCT_INTPTR handle,
    GCT_INT pid,
    GCT_CSTR rip,
    GCT_INT rport,
    GCT_CSTR lip,
    GCT_INT lport,
    GCT_CSTR udata)
{
    GCT_COUNTER_VALUE counter_value = api.counterIncrement(app_conn_counter_id);

    /* Logging current value */
    char buf[256];
    snprintf (buf, sizeof(buf), "Connections: %" PRId64, counter_value);
    api.logMessage(GCL_USER, buf);

    /* Continue connection handling */
    return 0;
}

GC_ADAPTER_EVENT
void on_disconnect(GCT_INTPTR user)
{
    GCT_COUNTER_VALUE counter_value = api.counterDecrement(app_conn_counter_id);

    /* Logging current value */
    char buf[256];
    snprintf (buf, sizeof(buf), "Connections: %" PRId64, counter_value);
    api.logMessage(GCL_USER, buf);

    /* Continue disconnection handling */
}

SEE ALSO

counterCreate, counterCreateAtomic, counterGetNames, counterIncrement, counterAdd, counterSet