NAME

s2sSetTimer - set a timer for a given server to server connection.

SYNOPSIS

#include <game-carrier/server.h>

typedef GCT_INT (GC_CORE_API *S2SSetTimer)(
    GCT_INTPTR s2s_handle,
    GCT_INT ms);

typedef struct s2sSetTimer {
    /* ... Some fields ... */
    S2SSetTimer s2sSetTimer;
    /* ... Some fields ... */
}

Parameters:

  • s2s_handle GCT_INTPTR The server to server connection handle.
  • ms GCT_INT Identifies the timer interval in milliseconds.

RETURN VALUE

Returns 0 if the operation is successful.

Standard errno codes like EINVAL or ENOMEM are returned if creation/allocation fails.

DESCRIPTION

The s2sSetTimer is used to schedule a timed event for a connection established with the Game Carrier server application. It allows you to set a timer interval in milliseconds, after which the on_client_timer event will be triggered.

The s2s_handle parameter represents the client connection handle, which is used to identify a specific connection for which the timer event is being scheduled. The handle is returned after the successful connection establishing in on_client_connect event.

The ms parameter is used to specify the timer interval in milliseconds. It determines the duration after which the timer event will be triggered for the specified client connection.

This function should be called following a successful receipt of the on_client_connect event.

You can use gc_client_set_timer to schedule events or actions that need to occur after a specific time interval. For example, you might use it to periodically send a heartbeat message to the server or to check for updates at regular intervals.

When the connection is still established and the timer expires, the on_client_timer event will be triggered. Each connection supports only one timer. If a new timer is set, the old timer will be automatically canceled. Please note that the timer does not have a high resolution, making it unsuitable for precise timing purposes.

In case of client library the analogeous function is gc_client_set_timer.

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, on_client_timer, on_client_connect