NAME
‘counterSet’ - sets the new value for a counter.
SYNOPSIS
#include <game-carrier/server.h>
typedef GCT_COUNTER_VALUE (GC_CORE_API *CounterSet)(
GCT_INT id,
GCT_COUNTER_VALUE val);
typedef struct counterSet {
/* ... Some fields ... */
CounterSet counterSet;
/* ... Some fields ... */
}
Parameters:
id
GCT_INT
Unique identifier of the counter.val
GCT_COUNTER_VALUE
New value to set for a counter.
RETURN VALUE
Returns the newly set value for 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 ‘counterSet’ function is used to set a specific value for a counter identified by its id.
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 set
the new value for it.
The val
parameter identifies the new value to set for a counter.
EXAMPLE
#include <game-carrier/server.h>
#include <stdio.h>
static struct core_api api;
static GCT_INT app_conn_counter_id;
static GCT_INT app_max_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;
}
app_max_conn_counter_id = api.counterCreateAtomic("MyApp.Connections.Max");
if (app_max_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);
GCT_COUNTER_VALUE max_value = api.counterGet(app_max_conn_counter_id);
if (counter_value > max_value) {
api.counterSet(app_max_conn_counter_id, counter_value);
}
/* 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 conn_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
, counterDecrement
, counterAdd