NAME

‘on_timer’ - the event that gets triggered when a timer for a connection expires.

SYNOPSIS

#include <game-carrier/server.h>

GC_ADAPTER_EVENT
void on_timer(
    GCT_INTPTR conn_user);

Parameters:

  • conn_user GCT_INTPTR User data associated with the connection.

RETURN VALUE

None.

DESCRIPTION

The on_timer function is invoked by the gcs when a timer expires for a connection. It occurs after the connectionSetTimer call.

The on_timer event is executed within the gcs worker thread context. Since connection events are serialized, synchronization primitives are unnecessary. The connection is not restricted to a specific thread in the worker thread pool, thus different threads may handle connection events.

The user parameter holds user data associated with the connection, retrieved from the on_connect call.

Additionally, the on_client_timer event is triggered when a connection is established, and the timer set by connectionSetTimer reaches expiration. Only one timer per connection is supported, but it should be noted that the timer does not have high resolution for precise timing purposes.

EXAMPLE

#include <game-carrier/server.h>

#include <stdlib.h>

/**
 * The example below drops connections if they are silent
 * and interval is greatrer than specified in TIMER_INTERVAL.
 */

#define TIMER_INTERVAL 1000

static struct core_api api;

struct connection_data {
    GCT_INTPTR handle;
    /* User data asociated with connection */
};

GC_ADAPTER_EVENT
int adapter_initialize(int id, struct core_api *core_api, const void *load_path)
{
    api = *core_api;
    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)
{
    /* Allocate user data */
    struct connection_data * conn = malloc(sizeof(struct connection_data));
    if (conn == NULL) {
        return GC_DROP_CONNECTION;
    }

    /* Save connection handle */
    conn->handle = handle;

    /* Set the timer */
    api.connectionSetTimer(handle, TIMER_INTERVAL);

    /* Return user data */
    return (GCT_INTPTR)conn;
}

GC_ADAPTER_EVENT
void on_message(
    GCT_INTPTR user,
    GCT_PTR data,
    GCT_SIZE len)
{
    struct connection_data * conn = (struct connection_data *)user;

    /* Update timer */
    api.connectionSetTimer(conn->handle, TIMER_INTERVAL);
}

GC_ADAPTER_EVENT
void on_timer(
    GCT_INTPTR user)
{
    struct connection_data * conn = (struct connection_data *)user;

    /* Close the connection without messages */
    api.connectionClose(conn->handle, GC_CLOSE_REASON_DROP);
}

SEE ALSO

connectionSetTimer, on_connect, on_disconnect