ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
Connection Keepalive

💓 Ping/pong keepalive and timeout detection More...

Files

file  keepalive.c
 ðŸ’“ Client keepalive: periodic ping/pong exchange for reliable connection failure detection
 
file  keepalive.h
 ascii-chat Client Connection Keepalive Management Interface
 

Functions

int keepalive_start_thread ()
 Start keepalive/ping thread.
 
void keepalive_stop_thread ()
 Stop keepalive/ping thread.
 
bool keepalive_thread_exited ()
 Check if keepalive thread has exited.
 

Detailed Description

💓 Ping/pong keepalive and timeout detection

Connection Keepalive

Overview

The keepalive subsystem manages connection liveness by sending periodic ping packets and detecting pong timeouts to identify dead connections.

Implementation: src/client/keepalive.c, src/client/keepalive.h

Configuration

Timing Parameters:

  • Ping interval: 5 seconds
  • Pong timeout: 30 seconds
  • Timeout triggers reconnection

Keepalive Thread

static void *keepalive_thread_func(void *arg)
{
(void)arg;
time_t last_pong_time = time(NULL);
// Send ping packet
send_packet_to_server(PACKET_TYPE_PING, NULL, 0,
// Wait for ping interval
for (int i = 0; i < PING_INTERVAL_SEC * 10; i++) {
platform_sleep_usec(100000); // 100ms
}
// Check pong timeout
time_t now = time(NULL);
if (now - last_pong_time > PONG_TIMEOUT_SEC) {
log_warn("Pong timeout (%d seconds) - connection may be dead", PONG_TIMEOUT_SEC);
break;
}
}
atomic_store(&g_keepalive_thread_exited, true);
return NULL;
}
bool should_exit(void)
Definition main.c:90
bool server_connection_is_active()
Check if server connection is currently active.
uint32_t server_connection_get_client_id()
Get client ID assigned by server.
void server_connection_lost()
Signal that connection has been lost.

Pong Recording

void keepalive_record_pong(void)
{
// Update last pong time (called by protocol handler)
g_last_pong_time = time(NULL);
}
See also
src/client/keepalive.c
src/client/keepalive.h
Client Overview

Function Documentation

◆ keepalive_start_thread()

int keepalive_start_thread ( )

#include <keepalive.c>

Start keepalive/ping thread.

Start keepalive/ping thread

Creates and starts the ping thread for connection keepalive. Must be called after successful server connection establishment.

Returns
0 on success, negative on error
0 on success, negative on error

Definition at line 233 of file keepalive.c.

233 {
234 if (g_ping_thread_created) {
235 log_warn("Ping thread already created");
236 return 0;
237 }
238
239 // Start ping thread for keepalive
240 atomic_store(&g_ping_thread_exited, false);
241 if (thread_pool_spawn(g_client_worker_pool, ping_thread_func, NULL, 3, "keepalive_ping") != ASCIICHAT_OK) {
242 log_error("Failed to spawn ping thread in worker pool");
243 LOG_ERRNO_IF_SET("Ping thread creation failed");
244 return -1;
245 }
246
247 g_ping_thread_created = true;
248 return 0;
249}
thread_pool_t * g_client_worker_pool
Global client worker thread pool.
asciichat_error_t thread_pool_spawn(thread_pool_t *pool, void *(*thread_func)(void *), void *thread_arg, int stop_id, const char *thread_name)
Definition thread_pool.c:70

References g_client_worker_pool, and thread_pool_spawn().

Referenced by protocol_start_connection().

◆ keepalive_stop_thread()

void keepalive_stop_thread ( )

#include <keepalive.c>

Stop keepalive/ping thread.

Stop keepalive/ping thread

Gracefully stops the ping thread and cleans up resources. Safe to call multiple times.

Definition at line 259 of file keepalive.c.

259 {
260 if (!g_ping_thread_created) {
261 return;
262 }
263
264 // Don't call signal_exit() here - that's for global shutdown only!
265 // The ping thread monitors connection state and will exit when connection is lost
266
267 // Wait for thread to exit gracefully
268 int wait_count = 0;
269 while (wait_count < 20 && !atomic_load(&g_ping_thread_exited)) {
270 platform_sleep_ns(100 * NS_PER_MS_INT); // 100ms
271 wait_count++;
272 }
273
274 if (!atomic_load(&g_ping_thread_exited)) {
275 log_warn("Ping thread not responding - will be joined by thread pool");
276 }
277
278 // Thread will be joined by thread_pool_stop_all() in protocol_stop_connection()
279 g_ping_thread_created = false;
280
281 log_debug("Ping thread stopped and joined");
282}

Referenced by protocol_stop_connection().

◆ keepalive_thread_exited()

bool keepalive_thread_exited ( )

#include <keepalive.c>

Check if keepalive thread has exited.

Check if keepalive thread has exited

Returns
true if thread has exited, false otherwise
true if thread exited, false otherwise

Definition at line 291 of file keepalive.c.

291 {
292 return atomic_load(&g_ping_thread_exited);
293}