NAME
gc_clients_stop_all
- closes all active connections.
SYNOPSIS
#include <game-carrier/client.h>
GC_CLIENT_API
void gc_clients_stop_all(void);
RETURN VALUE
None.
DESCRIPTION
The gc_clients_stop_all
function closes all active connections established with
the Game Carrier server application. It must be called only after successful
invocation of the gc_client_start
function.
The gc_clients_stop_all
will be called automatically in case the
gc_clients_cleanup
is called.
The gc_client_stop_all
function will remain in a wait state until any queued
messages, added by the gc_client_add_message
function, are sent.
At the same time, it will not process any new messages.
There is a possibility of receiving ‘on_client_message
events after the successful
gc_client_stop_all call. It happens when a message is received and queued up to be processed by the application, but at that moment the application hasn't had a chance to handle it yet. In that case, the message will still be processed and the [
on_client_message](/docs/api-reference/core-api/client/messaging/event/on_client_message/) event will be triggered, even if
gc_client_stop_all` has been called and the connections are being closed.
EXAMPLE
#include <game-carrier/client.h>
#include <stdio.h>
#define GAME_CLIENT 1
#define LOBBY_CLIENT 2
int mode = GC_MODE_HYBRID;
GCT_INTPTR game_conn, lobby_conn;
/**
* Finished client session counter.
* We have two session: game and lobby.
* The application will be closed when both sessions are finished.
**/
int finished = 0;
void on_client_connect(GCT_INTPTR conn_user, GCT_INTPTR handle)
{
if (conn_user == GAME_CLIENT) {
printf("Connected to the game server\n");
game_conn = handle;
gc_client_stop(game_conn);
return;
}
if (conn_user == LOBBY_CLIENT) {
printf("Connected to the lobby server\n");
lobby_conn = handle;
gc_client_stop(lobby_conn);
return;
}
printf("??? Connected to an unknown server\n");
}
void on_client_connection_error(GCT_INTPTR conn_user, GCT_CSTR reason)
{
++finished;
if (conn_user == GAME_CLIENT) {
fprintf(stderr, "Failed to connect to the game server: %s\n", reason);
return;
}
if (conn_user == LOBBY_CLIENT) {
fprintf(stderr, "Failed to connect to the lobby server: %s\n", reason);
return;
}
fprintf(stderr, "??? Failed to connect to an unknown server: %s\n", reason);
}
void on_client_disconnect(GCT_INTPTR conn_user)
{
++finished;
}
int main(int argc, char * argv[])
{
int status;
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;
int ret = gc_clients_init(mode);
if (ret != 0) {
fprintf(stderr, "Failed to initialize the Game Carrier client library\n");
return 1;
}
status = gc_client_start(GAME_CLIENT, GC_PROTOCOL_WSS, "game.example.com", 7681, "game", &events);
if (status != 0) {
fprintf(stderr, "Failed to connect to the game server\n");
return 1;
}
status = gc_client_start(LOBBY_CLIENT, GC_PROTOCOL_WSS, "lobby.example.com", 7681, "lobby", &events);
if (status != 0) {
fprintf(stderr, "Failed to connect to the lobby server\n");
return 1;
}
// Wait Loop //
while (finished != 5) {
// Perform related game activity, like drawing a new frame //
draw_new_frame();
// Handling network //
if (mode == GC_MODE_PASSIVE || mode == GC_MODE_HYBRID) {
gc_clients_service();
}
if (finished == 2) {
// Stop all clients for unknown reason //
gc_clients_stop_all();
// Increase finished jobs to avoid reconnecting //
++finished;
// Establish connections again //
status = gc_client_start(GAME_CLIENT, GC_PROTOCOL_WSS, "game.example.com", 7681, "game", &events);
if (status != 0) {
fprintf(stderr, "Failed to connect to the game server\n");
return 1;
}
status = gc_client_start(LOBBY_CLIENT, GC_PROTOCOL_WSS, "lobby.example.com", 7681, "lobby", &events);
if (status != 0) {
fprintf(stderr, "Failed to connect to the lobby server\n");
return 1;
}
}
}
gc_clients_cleanup();
return 0;
}
SEE ALSO
gc_client_stop
, gc_clients_cleanup
, OnClientDisconnect