NAME

s2sAddMessage - sends a message to another server.

SYNOPSIS

#include <game-carrier/server.h>

typedef void (GC_CORE_API *S2SAddMessage)(
    GCT_INTPTR s2s_handle,
    GCT_PTR data,
    GCT_SIZE len,
    GCT_INTPTR cookie);

typedef struct s2sAddMessage {
    /* ... Some fields ... */
    S2SAddMessage s2sAddMessage;
    /* ... Some fields ... */
}

Parameters:

  • s2s_handle GCT_INTPTR The server to server connection handle.
  • data GCT_PTR Message data to be sent to the server application.
  • len GCT_SIZE The length of the message being sent in bytes.
  • cookie GCT_INTPTR User data assocaited with the message.

RETURN VALUE

None.

DESCRIPTION

The s2sAddMessage function is used to send a message to another Game Carrier server application.

The s2s_handle parameter represents the server to server connection handle, which is used to identify the active client session to which the message should be sent. Basically, this handle allows the function to determine the appropriate client session for message delivery. The handle is returned after the successful connection establishing in on_client_connect event.

The msg parameter specifies the message data to be sent to the Game Carrier server application.

The len parameter indicates the length of the message data being sent in bytes.

The cookie parameter allows associating the user-specific data with the message and can be utilized for custom handling or identification purposes when the on_client_message_sent event is triggered.

The gc_client_add_message function should be called after receiving a on_client_connect event. will be triggered. This event does not indicate that the message has been delivered to the application. It only means that the message has been handed over to the operating system.

The s2sAddMessage function should be called after receiving a on_client_connect event.

After the message is queued for sending, the on_client_message_sent event will be triggered. This event does not indicate that the message has been delivered to the application. It only means that the message has been handed over to the operating system.

In case of client library the analogeous function is gc_client_add_message.

EXAMPLE

#include <game-carrier/server.h>

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

/**
 * The example below connects to a server,
 * waits for a message and then it disconnects.
 **/

static struct core_api api;

#define S2S_USER_INDEX  1
#define MAGIC_COOKIE   42

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;
        char * msg = "Hello!";
        api.s2sAddMessage(s2s_handle, msg, strlen(msg), MAGIC_COOKIE);
        return;
    }

    api.logMessage(GCL_ERR, "??? S2S connected to an unknown server");
}

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);
    api.logMessage(GCL_ERR, buf);
}

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.s2sDestroy(s2s_handle);
        return;
    }

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

void on_client_message_sent(
    GCT_INTPTR conn_user,
    GCT_INTPTR cookie,
    GCT_SIZE len,
    GCT_INT status)
{
    char buf[256];

    if (conn_user != S2S_USER_INDEX) {
        snprintf(buf, sizeof(buf), "??? Unknown S2S message sent");
        api.logMessage(GCL_ERR, buf);
        return;
    }

    if (cookie != MAGIC_COOKIE) {
        snprintf(buf, sizeof(buf), "??? Unexpected cookie received: " PRId64, cookie);
        api.logMessage(GCL_ERR, buf);
        return;
    }

    if (status != 0) {
        snprintf(buf, sizeof(buf), "S2S message failed to send: %d", status);
        api.logMessage(GCL_ERR, buf);
        return;
    }

    api.logMessage(GCL_NOTICE, "S2S message sent OK");
}

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_message_sent = on_client_message_sent;

    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_connect, on_client_message_sent