69#include <ascii-chat/util/fps.h>
70#include <ascii-chat/util/time.h>
72#include <ascii-chat/common.h>
73#include <ascii-chat/platform/abstraction.h>
74#include <ascii-chat/thread_pool.h>
90static bool g_ping_thread_created =
false;
100static atomic_bool g_ping_thread_exited =
false;
107#define PING_INTERVAL_NS (1LL * NS_PER_SEC_INT)
110#define PING_SLEEP_INTERVAL_NS (1LL * NS_PER_SEC_INT)
141static void *ping_thread_func(
void *arg) {
145 log_debug(
"Ping thread started");
149 static fps_t fps_tracker = {0};
150 static bool fps_tracker_initialized =
false;
151 if (!fps_tracker_initialized) {
154 fps_tracker_initialized =
true;
164 log_debug(
"Connection inactive, exiting ping thread");
170 log_debug(
"Rekey threshold reached, initiating session rekey");
172 log_error(
"Failed to initiate rekey");
180 log_debug(
"Failed to send ping packet");
203 platform_sleep_ns(sleep_ns);
208 log_debug(
"Ping thread stopped");
211 atomic_store(&g_ping_thread_exited,
true);
234 if (g_ping_thread_created) {
235 log_warn(
"Ping thread already created");
240 atomic_store(&g_ping_thread_exited,
false);
242 log_error(
"Failed to spawn ping thread in worker pool");
243 LOG_ERRNO_IF_SET(
"Ping thread creation failed");
247 g_ping_thread_created =
true;
260 if (!g_ping_thread_created) {
269 while (wait_count < 20 && !atomic_load(&g_ping_thread_exited)) {
270 platform_sleep_ns(100 * NS_PER_MS_INT);
274 if (!atomic_load(&g_ping_thread_exited)) {
275 log_warn(
"Ping thread not responding - will be joined by thread pool");
279 g_ping_thread_created =
false;
281 log_debug(
"Ping thread stopped and joined");
292 return atomic_load(&g_ping_thread_exited);
void asciichat_errno_destroy(void)
thread_pool_t * g_client_worker_pool
Global client worker thread pool.
void fps_init_with_interval(fps_t *tracker, int expected_fps, const char *name, uint64_t report_interval_ns)
void fps_frame_ns(fps_t *tracker, uint64_t current_time_ns, const char *context)
bool server_connection_is_active()
Check if server connection is currently active.
int threaded_send_ping_packet(void)
Thread-safe ping packet transmission.
bool server_connection_is_lost()
Check if connection loss has been detected.
void server_connection_lost()
Signal that connection has been lost.
bool crypto_client_should_rekey(void)
Check if session rekeying should be triggered.
int crypto_client_initiate_rekey(void)
Initiate session rekeying (client-initiated)
bool keepalive_thread_exited()
Check if keepalive thread has exited.
int keepalive_start_thread()
Start keepalive/ping thread.
void keepalive_stop_thread()
Stop keepalive/ping thread.
#define PING_SLEEP_INTERVAL_NS
ascii-chat Client Connection Keepalive Management Interface
Server cryptographic operations and per-client handshake management.
ascii-chat Server Mode Entry Point Header
asciichat_error_t thread_pool_spawn(thread_pool_t *pool, void *(*thread_func)(void *), void *thread_arg, int stop_id, const char *thread_name)
uint64_t time_get_ns(void)
uint64_t time_elapsed_ns(uint64_t start_ns, uint64_t end_ns)