NAME

on_client_timer - event occurs when a timer expires for a connection.

SYNOPSIS

#include <game-carrier/common.h>

typedef void (*OnClientTimer)(GCT_INTPTR conn_user);

typedef struct on_client_timer {
    /* ... Some fields ... */
    OnClientTimer on_client_timer;
    /* ... Some fields ... */
}

Parameters:

  • conn_user GCT_INTPTR User data associated with the connection.

RETURN VALUE

None.

DESCRIPTION

The on_client_timer event is the member of the client_events struct. The on_client_timer event occurs when a connection is established and the timer set using the s2sSetTimer function has expired.

The conn_user parameter represents the user-defined data that is associated with a connection. This data is passed to all event handlers that are defined in the client_events struct. This parameter allows the user to find their own connection-related data. It could be a pointer to a structure allocated before creation a connection, or an index in some container. If the user has created multiple connections, they could use different conn_user values for each connection and then use that value in the event handlers to determine which connection the event is associated with.

It’s important to note that the timer does not have high resolution and therefore cannot be used for precise timing purposes.

EXAMPLE

#include <game-carrier/server.h>

#include <stdio.h>
#include <string.h>

/**
 * The example below connects to a server,
 * waits for a message with TIMER_DURATION timeout.
 **/

static struct core_api api;

#define S2S_USER_INDEX     1
#define TIMER_DURATION  1000

GCT_INT adapter_id;
GCT_INTPTR s2s_handle;

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;
}

void on_client_connect(GCT_INTPTR conn_user, GCT_INTPTR handle)
{
    if (conn_user == S2S_USER_INDEX) {
        api.logMessage(GCL_NOTICE, "S2S connected");
        s2s_handle = handle;
        api.s2sSetTimer(s2s_handle, TIMER_DURATION);
        return;
    }

    printf("??? Connected to the unknown server\n");
}

void on_client_connection_error(GCT_INTPTR conn_user, GCT_CSTR reason)
{
    char buf[256];

    if (conn_user == S2S_USER_INDEX) {
        snprintf(buf, sizeof(buf), "S2S connection failed: %s.", reason);
        api.logMessage(GCL_ERR, buf);
        return;
    }

    snprintf(buf, sizeof(buf), "??? Unknown S2S connection failed:  %s", reason);
}

void on_client_disconnect(GCT_INTPTR conn_user)
{
    if (conn_user == S2S_USER_INDEX) {
        api.logMessage(GCL_NOTICE, "S2S disconect");
        return;
    }

    api.logMessage(GCL_ERR, "??? Unknown S2S disconect");
}

void on_client_message(
    GCT_INTPTR conn_user, GCT_PTR data, GCT_SIZE len)
{
    char buf[256];

    if (conn_user == S2S_USER_INDEX) {
        snprintf(buf, sizeof(buf), "S2S message: %s", (char*)data);
        api.logMessage(GCL_NOTICE, buf);
        api.s2sSetTimer(s2s_handle, GC_CANCEL_TIMER);
        api.s2sDestroy(s2s_handle);
        return;
    }

    snprintf(buf, sizeof(buf), "??? Unknown S2S message: %s", (char*)data);
    api.logMessage(GCL_ERR, buf);
}

void on_client_timer(GCT_INTPTR conn_user)
{
    if (s2s_handle == S2S_USER_INDEX) {
        api.logMessage(GCL_ERR, "Timer expired, no answer in time!");
        api.s2sDestroy(s2s_handle);
        return;
    }

    api.logMessage(GCL_ERR, "??? Unknown timer has expireed");
}

GC_ADAPTER_EVENT
void application_start(GCT_INT app_id)
{
    ClientEvents events;
    events.on_client_connect = on_client_connect;
    events.on_client_connection_error = on_client_connection_error;
    events.on_client_disconnect = on_client_disconnect;
    events.on_client_message = on_client_message;
    events.on_client_timer = on_client_timer;

    int status = api.s2sCreate(adapter_id, S2S_USER_INDEX, GC_PROTOCOL_WSS, "server.example.com", 7681, "server", &events);
    if (status != 0) {
        api.logMessage(GCL_ERR, "Failed to allocate S2S.");
    }
}

SEE ALSO

s2sCreate, s2sSetTimer