ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
keepalive.h File Reference

ascii-chat Client Connection Keepalive Management Interface More...

Go to the source code of this file.

Functions

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

Detailed Description

ascii-chat Client Connection Keepalive Management Interface

Defines the interface for connection keepalive thread management and ping/pong coordination in the ascii-chat client.

Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
2025

Definition in file keepalive.h.

Function Documentation

◆ keepalive_start_thread()

int keepalive_start_thread ( client_connection_t * connection)

Start keepalive/ping thread.

Returns
0 on success, negative on error

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

Definition at line 209 of file keepalive.c.

209 {
210 if (connection->ping_thread_created) {
211 log_warn("Ping thread already created");
212 return 0;
213 }
214
215 // Start ping thread for keepalive
216 atomic_store_bool(&connection->ping_thread_exited, false);
217 if (thread_pool_spawn(connection->client_worker_pool, ping_thread_func, connection, 3, "keepalive_ping") != ASCIICHAT_OK) {
218 log_error("Failed to spawn ping thread in worker pool");
219 LOG_ERRNO_IF_SET("Ping thread creation failed");
220 return -1;
221 }
222
223 connection->ping_thread_created = true;
224 return 0;
225}
#define LOG_ERRNO_IF_SET(message)
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
@ ASCIICHAT_OK
Definition error_codes.h:50
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
thread_pool_t * client_worker_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)
Spawn a worker thread in the pool.

References ASCIICHAT_OK, atomic_store_bool(), client_connection::client_worker_pool, LOG_ERRNO_IF_SET, log_error, log_warn, client_connection::ping_thread_created, client_connection::ping_thread_exited, and thread_pool_spawn().

Referenced by client_protocol_start().

◆ keepalive_stop_thread()

void keepalive_stop_thread ( client_connection_t * connection)

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

235 {
236 if (!connection)
237 return;
238 if (!connection->ping_thread_created) {
239 return;
240 }
241
242 // Don't call signal_exit() here - that's for global shutdown only!
243 // The ping thread monitors connection state and will exit when connection is lost
244
245 // Wait for thread to exit gracefully
246 int wait_count = 0;
247 while (wait_count < 20 && !atomic_load_bool(&connection->ping_thread_exited)) {
248 platform_sleep_ns(100 * NS_PER_MS_INT); // 100ms
249 wait_count++;
250 }
251
252 if (!atomic_load_bool(&connection->ping_thread_exited)) {
253 log_warn("Ping thread not responding - will be joined by thread pool");
254 }
255
256 // Thread will be joined by thread_pool_stop_all() in client_connection_stop()
257 connection->ping_thread_created = false;
258
259 log_debug("Ping thread stopped and joined");
260}
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
#define NS_PER_MS_INT
Definition time.h:172

References atomic_load_bool(), log_debug, log_warn, NS_PER_MS_INT, client_connection::ping_thread_created, and client_connection::ping_thread_exited.

◆ keepalive_thread_exited()

bool keepalive_thread_exited ( client_connection_t * connection)

Check if keepalive thread has exited.

Returns
true if thread exited, false otherwise

Check if keepalive thread has exited

Returns
true if thread has exited, false otherwise

Definition at line 269 of file keepalive.c.

269 {
270 return atomic_load_bool(&connection->ping_thread_exited);
271}

References atomic_load_bool(), and client_connection::ping_thread_exited.