NAME
connectionAddMessage
- Schedules a timed event for a connection.
SYNOPSIS
#include <game-carrier/server.h>
typedef GCT_INT (GC_CORE_API *ConnectionSetTimer)(
GCT_INTPTR conn_handle,
GCT_INT ms);
typedef struct connectionSetTimer {
/* ... Some fields ... */
ConnectionSetTimer connectionSetTimer;
/* ... Some fields ... */
}
Parameters:
conn_handle
GCT_INTPTR
The 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 connectionSetTimer
is used to schedule a timed event for a connection.
It allows you to set a timer interval in milliseconds, after which on_timer
event will be triggered.
The conn_handle
parameter represents the handle, which is used to identify a specific connection for which the timer event is being scheduled.
The handle is obtained after the successful connection establishing in on_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_connect
event.
You can use connectionSetTimer
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 client 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 by the game, 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.
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);
}