ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
client_manager_t Struct Reference

Global client manager structure for server-side client coordination. More...

#include <src/server/client.h>

Data Fields

client_info_t clients [MAX_CLIENTS]
 Array of client_info_t structures (backing storage)
 
client_info_tclients_by_id
 uthash head pointer for O(1) client_id -> client_info_t* lookups
 
int client_count
 Current number of active clients.
 
mutex_t mutex
 Legacy mutex (mostly replaced by rwlock)
 
_Atomic uint32_t next_client_id
 Monotonic counter for unique client IDs (atomic for thread-safety)
 

Detailed Description

Global client manager structure for server-side client coordination.

Manages all connected clients in the ascii-chat server. Provides O(1) client lookup via hashtable while maintaining array-based storage for iteration. This structure serves as the central coordination point for client lifecycle management.

ARCHITECTURE:

The client manager uses a dual-storage approach:

  • Array (clients[]): Fast iteration, stable pointers, sequential access
  • Hashtable (client_hashtable): O(1) lookup by client_id

This design provides:

  • O(1) lookups via hashtable
  • O(n) iteration via array (for stats, rendering, etc.)
  • Stable pointers (array elements don't move)
  • Linear memory layout (cache-friendly)

THREAD SAFETY:

Protected by g_client_manager_rwlock (reader-writer lock):

  • Read operations (lookups, stats): Acquire read lock (concurrent)
  • Write operations (add/remove): Acquire write lock (exclusive)

LOCK ORDERING:

  • Always acquire g_client_manager_rwlock BEFORE per-client mutexes
  • Prevents deadlocks in multi-client operations

STRUCTURE FIELDS:

  • clients[]: Array of client_info_t structures (backing storage)
  • client_hashtable: Hashtable for O(1) client_id -> client_info_t* lookups
  • client_count: Current number of active clients
  • mutex: Legacy mutex (mostly replaced by rwlock)
  • next_client_id: Monotonic counter for unique client IDs
Note
The hashtable uses client_id as key and points to elements in clients[] array.
next_client_id is atomic for thread-safe ID assignment.
All client access should go through find_client_by_id() or find_client_by_socket() to ensure proper locking.

Definition at line 63 of file src/server/client.h.

Field Documentation

◆ client_count

int client_manager_t::client_count

Current number of active clients.

Definition at line 69 of file src/server/client.h.

Referenced by broadcast_server_state_to_all_clients().

◆ clients

◆ clients_by_id

client_info_t* client_manager_t::clients_by_id

uthash head pointer for O(1) client_id -> client_info_t* lookups

Definition at line 67 of file src/server/client.h.

Referenced by __attribute__(), and server_main().

◆ mutex

mutex_t client_manager_t::mutex

Legacy mutex (mostly replaced by rwlock)

Definition at line 71 of file src/server/client.h.

Referenced by server_main().

◆ next_client_id

_Atomic uint32_t client_manager_t::next_client_id

Monotonic counter for unique client IDs (atomic for thread-safety)

Definition at line 73 of file src/server/client.h.


The documentation for this struct was generated from the following file: