NAME
counterAdd
- adds or subtracts a specific value for a counter.
SYNOPSIS
#include <game-carrier/server.h>
typedef GCT_COUNTER_VALUE (GC_CORE_API *CounterAdd)(
GCT_INT id,
GCT_COUNTER_VALUE val);
typedef struct counterAdd {
/* ... Some fields ... */
CounterAdd counterAdd;
/* ... Some fields ... */
}
Parameters:
id
GCT_INT
Unique identifier of the counter.val
GCT_COUNTER_VALUE
New value to add/subtract to/from a 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 ‘counterAdd’ function is used to add or subtract a specific value for a coutner.
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.
Theid
parameter represents the unique identifier of the counter to which a value
will be added
The val
parameter identifies the value to be added or subtracted to/from the counter.
EXAMPLE
#include <game-carrier/server.h>
#include <stdio.h>
static struct core_api api;
static GCT_INT app_conn_counter_id;
GC_ADAPTER_EVENT
GCT_INT adapter_initialize(int id, struct core_api * core_api, const void * 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.counterAdd(app_conn_counter_id, 1);
/* 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 cid)
{
GCT_COUNTER_VALUE counter_value = api.counterAdd(app_conn_counter_id, -1);
/* 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
, counterDecrement
, counterSet