ascii-chat 0.11.33
Video chat in your terminal
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 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.
#define log_warn(...)
Log a WARN message.
Definition log/log.h:574
@ PACKET_TYPE_PING
Keepalive ping packet.
Definition packet.h:383
bool should_exit(void)
Definition misc.c:131

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 236 of file keepalive.c.

236 {
237 if (g_ping_thread_created) {
238 log_warn("Ping thread already created");
239 return 0;
240 }
241
242 // Register keepalive atomics with named debug registry
243 static bool keepalive_atomics_registered = false;
244 if (!keepalive_atomics_registered) {
245 NAMED_REGISTER_ATOMIC(&g_ping_thread_exited, "keepalive_ping_thread_exit_confirmation", NULL);
246 keepalive_atomics_registered = true;
247 }
248
249 // Start ping thread for keepalive
250 atomic_store_bool(&g_ping_thread_exited, false);
251 if (thread_pool_spawn(g_client_worker_pool, ping_thread_func, NULL, 3, "keepalive_ping") != ASCIICHAT_OK) {
252 log_error("Failed to spawn ping thread in worker pool");
253 LOG_ERRNO_IF_SET("Ping thread creation failed");
254 return -1;
255 }
256
257 g_ping_thread_created = true;
258 return 0;
259}
#define LOG_ERRNO_IF_SET(message)
Check if any error occurred and log it if so.
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
Definition atomic.c:177
thread_pool_t * g_client_worker_pool
Global client worker thread pool.
asciichat_error_t thread_pool_spawn(void *pool, void *(*thread_func)(void *), void *thread_arg, int stop_id, const char *name)
Spawn a worker thread in the pool.
#define NAMED_REGISTER_ATOMIC(a, name, parent_ptr)
Register an atomic_t with automatic format specifier.
@ ASCIICHAT_OK
Definition error_codes.h:51
#define log_error(...)
Log an ERROR message.
Definition log/log.h:587

References ASCIICHAT_OK, atomic_store_bool(), g_client_worker_pool, LOG_ERRNO_IF_SET, log_error, log_warn, NAMED_REGISTER_ATOMIC, 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 269 of file keepalive.c.

269 {
270 if (!g_ping_thread_created) {
271 return;
272 }
273
274 // Don't call signal_exit() here - that's for global shutdown only!
275 // The ping thread monitors connection state and will exit when connection is lost
276
277 // Wait for thread to exit gracefully
278 int wait_count = 0;
279 while (wait_count < 20 && !atomic_load_bool(&g_ping_thread_exited)) {
280 platform_sleep_ns(100 * NS_PER_MS_INT); // 100ms
281 wait_count++;
282 }
283
284 if (!atomic_load_bool(&g_ping_thread_exited)) {
285 log_warn("Ping thread not responding - will be joined by thread pool");
286 }
287
288 // Thread will be joined by thread_pool_stop_all() in protocol_stop_connection()
289 g_ping_thread_created = false;
290
291 log_debug("Ping thread stopped and joined");
292}
bool atomic_load_bool(atomic_t *a)
Atomically load a boolean value.
Definition atomic.c:169
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548
#define NS_PER_MS_INT
Definition time.h:156
void platform_sleep_ns(uint64_t ns)
Platform-safe sleep function with nanosecond precision.

References atomic_load_bool(), log_debug, log_warn, NS_PER_MS_INT, and platform_sleep_ns().

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 301 of file keepalive.c.

301 {
302 return atomic_load_bool(&g_ping_thread_exited);
303}

References atomic_load_bool().