ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
lib/network/tcp/server.h
Go to the documentation of this file.
1#pragma once
2
63#include <stdint.h>
64#include <stdbool.h>
65#include <stdatomic.h>
66#include "common.h"
67#include "platform/socket.h"
69#include "thread_pool.h"
70#include "uthash.h"
71
72// Forward declarations
74typedef struct tcp_server tcp_server_t;
75
84typedef void (*tcp_client_cleanup_fn)(void *client_data);
85
93typedef void (*tcp_client_foreach_fn)(socket_t socket, void *client_data, void *user_arg);
94
102typedef struct {
104 struct sockaddr_storage addr;
105 socklen_t addr_len;
106 void *user_data;
108
119typedef void *(*tcp_client_handler_fn)(void *arg);
120
136
150
167
179
193
203
214
226asciichat_error_t tcp_server_add_client(tcp_server_t *server, socket_t socket, void *client_data);
227
239
250asciichat_error_t tcp_server_get_client(tcp_server_t *server, socket_t socket, void **out_data);
251
262void tcp_server_foreach_client(tcp_server_t *server, tcp_client_foreach_fn callback, void *user_arg);
263
273
274// ============================================================================
275// Client Context Utilities
276// ============================================================================
277
289const char *tcp_client_context_get_ip(const tcp_client_context_t *ctx, char *buf, size_t len);
290
301
311void tcp_server_reject_client(socket_t socket, const char *reason);
312
313// ============================================================================
314// Client Thread Pool Management
315// ============================================================================
316
337asciichat_error_t tcp_server_spawn_thread(tcp_server_t *server, socket_t client_socket, void *(*thread_func)(void *),
338 void *thread_arg, int stop_id, const char *thread_name);
339
352
363asciichat_error_t tcp_server_get_thread_count(tcp_server_t *server, socket_t client_socket, size_t *count);
🔌 Cross-platform abstraction layer umbrella header for ascii-chat
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:46
int socket_t
Socket handle type (POSIX: int)
Definition socket.h:50
pthread_mutex_t mutex_t
Mutex type (POSIX: pthread_mutex_t)
Definition mutex.h:38
void(* tcp_client_foreach_fn)(socket_t socket, void *client_data, void *user_arg)
Callback for iterating over clients.
void tcp_server_set_cleanup_callback(tcp_server_t *server, tcp_client_cleanup_fn cleanup_fn)
Set client cleanup callback.
void tcp_server_reject_client(socket_t socket, const char *reason)
Reject client connection with reason.
void *(* tcp_client_handler_fn)(void *arg)
Client handler thread function type.
void(* tcp_client_cleanup_fn)(void *client_data)
Callback for client cleanup.
asciichat_error_t tcp_server_spawn_thread(tcp_server_t *server, socket_t client_socket, void *(*thread_func)(void *), void *thread_arg, int stop_id, const char *thread_name)
Spawn a worker thread for a client.
asciichat_error_t tcp_server_remove_client(tcp_server_t *server, socket_t socket)
Remove client from registry.
void tcp_server_foreach_client(tcp_server_t *server, tcp_client_foreach_fn callback, void *user_arg)
Iterate over all clients.
asciichat_error_t tcp_server_stop_client_threads(tcp_server_t *server, socket_t client_socket)
Stop all threads for a client in stop_id order.
asciichat_error_t tcp_server_init(tcp_server_t *server, const tcp_server_config_t *config)
Initialize TCP server.
asciichat_error_t tcp_server_get_client(tcp_server_t *server, socket_t socket, void **out_data)
Get client data.
int tcp_client_context_get_port(const tcp_client_context_t *ctx)
Get port number from client context.
asciichat_error_t tcp_server_run(tcp_server_t *server)
Run TCP server accept loop.
asciichat_error_t tcp_server_get_thread_count(tcp_server_t *server, socket_t client_socket, size_t *count)
Get thread count for a client.
void tcp_server_shutdown(tcp_server_t *server)
Shutdown TCP server.
const char * tcp_client_context_get_ip(const tcp_client_context_t *ctx, char *buf, size_t len)
Get formatted IP address from client context.
size_t tcp_server_get_client_count(tcp_server_t *server)
Get client count.
asciichat_error_t tcp_server_add_client(tcp_server_t *server, socket_t socket, void *client_data)
Add client to registry.
Cross-platform socket interface for ascii-chat.
Per-client connection context.
socket_t client_socket
Client connection socket.
socklen_t addr_len
Address length.
void * user_data
User-provided data from config.
Client registry entry.
void * client_data
User-provided client data.
UT_hash_handle hh
uthash handle
thread_pool_t * threads
Thread pool for client worker threads.
socket_t socket
Client socket (hash key)
TCP server configuration.
bool bind_ipv6
Whether to bind IPv6 socket.
void * user_data
User data passed to each client handler.
tcp_client_handler_fn client_handler
Client handler callback.
bool bind_ipv4
Whether to bind IPv4 socket.
const char * ipv4_address
IPv4 bind address (NULL or empty = don't bind)
int accept_timeout_sec
select() timeout in seconds (for responsive shutdown)
const char * ipv6_address
IPv6 bind address (NULL or empty = don't bind)
TCP server state.
mutex_t clients_mutex
Mutex protecting client registry.
atomic_bool running
Server running flag (set false to shutdown)
tcp_server_config_t config
Server configuration.
tcp_client_entry_t * clients
Hash table of connected clients.
socket_t listen_socket
IPv4 listen socket.
socket_t listen_socket6
IPv6 listen socket.
tcp_client_cleanup_fn cleanup_fn
Callback for cleaning up client data.
Thread pool structure.
🧵 Generic thread pool abstraction for managing worker threads
#️⃣ Wrapper for uthash.h that ensures common.h is included first