NAME

connectionAddMessage - Adds a message to the send queue of a connection.

SYNOPSIS

#include <game-carrier/server.h>

typedef GCT_INT (GC_CORE_API *ConnectionAddMessage)(
    GCT_INTPTR conn_handle,
    GCT_PTR data,
    GCT_SIZE len,
    GCT_INTPTR cookie);

typedef struct connectionAddMessage {
    /* ... Some fields ... */
    ConnectionAddMessage connectionAddMessage;
    /* ... Some fields ... */
}

Parameters:

  • conn_handle GCT_INTPTR Connection handle.
  • data GCT_PTR Pointer to the message data.
  • len GCT_SIZE The length of the message.
  • cookie GCT_INTPTR User data associated with the message.

RETURN VALUE

Returns 0 if the invocation is successful.

Returns an error code, such as EINVAL if there are bad arguments in the call.

DESCRIPTION

The connectionAddMessage function is utilized to add a message to the send queue of a connection. This method serves the purpose of sending messages to clients.

It is important to note that the connectionAddMessage function should only be called for open connections. If a connection is closed, either an error code will be returned by the connectionAddMessage function, or the function will be applied to another open connection with the same handle.

The conn_handle parameter represents the connection handle, which was previously provided in the on_connect call.

The msg parameter is a pointer to the message data. The message data will be copied to an internal buffer, ensuring its safety even after the function call, allowing for its deallocation.

The len parameter indicates the length of the message.

The cookie parameter is an additional piece of information associated with the message. It is an integer value that will be passed to the on_message_sent call.

EXAMPLE

#include <game-carrier/server.h>

#include <stdlib.h>
#include <string.h>

#define COOKIE_MAGIC  0xFACE

static struct core_api api;

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

int is_good_connection(GCT_INT rport, GCT_CSTR rip, GCT_CSTR udata);

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)
{
    /* Check if this connection can be accepted */
    if (!is_good_connection(rport, rip, udata)) {
        return GC_DROP_CONNECTION;
    }

    /* 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;

    /* Send greetings */
    const char * msg = "Hello!";
    int status = api.connectionAddMessage(handle, msg, strlen(msg), COOKIE_MAGIC);
    if (status != 0) {
        api.logMessage(GCL_ERR, "Failed to send message");
        return GC_DROP_CONNECTION;
    }

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

GC_ADAPTER_EVENT
void on_message_sent(
    GCT_INTPTR conn_user,
    GCT_INTPTR cookie,
    GCT_SIZE len,
    GCT_INT status)
{
    if (cookie != COOKIE_MAGIC) {
        api.logMessage(GCL_ERR, "Invalid cookie!");
        return;
    }
}

SEE ALSO

on_connect, on_disconnect, on_message_sent