ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
include/ascii-chat/network/tcp/server.h
Go to the documentation of this file.
1#pragma once
2
65#include <stdint.h>
66#include <stdbool.h>
67#include "../../atomic.h"
68#include "../../common.h"
69#include "../../platform/socket.h"
70#include "../../platform/abstraction.h"
71#include "../../thread_pool.h"
72#include <ascii-chat/uthash.h> // For UT_hash_handle
73
74// Forward declarations
76typedef struct tcp_server tcp_server_t;
77
86typedef void (*tcp_client_cleanup_fn)(void *client_data);
87
95typedef void (*tcp_client_foreach_fn)(socket_t socket, void *client_data, void *user_arg);
96
105typedef void (*tcp_status_update_fn)(void *user_data);
106
114typedef struct {
116 struct sockaddr_storage addr;
117 socklen_t addr_len;
118 void *user_data;
120
131typedef void *(*tcp_client_handler_fn)(void *arg);
132
150
164
181
193
208
217void tcp_server_destroy(tcp_server_t *server);
218
229
241asciichat_error_t tcp_server_add_client(tcp_server_t *server, socket_t socket, void *client_data);
242
254
265asciichat_error_t tcp_server_get_client(tcp_server_t *server, socket_t socket, void **out_data);
266
277void tcp_server_foreach_client(tcp_server_t *server, tcp_client_foreach_fn callback, void *user_arg);
278
288
289// ============================================================================
290// Client Context Utilities
291// ============================================================================
292
304const char *tcp_client_context_get_ip(const tcp_client_context_t *ctx, char *buf, size_t len);
305
316
326void tcp_server_reject_client(socket_t socket, const char *reason);
327
328// ============================================================================
329// Client Thread Pool Management
330// ============================================================================
331
352asciichat_error_t tcp_server_spawn_thread(tcp_server_t *server, socket_t client_socket, void *(*thread_func)(void *),
353 void *thread_arg, int stop_id, const char *thread_name);
354
367
378asciichat_error_t tcp_server_get_thread_count(tcp_server_t *server, socket_t client_socket, size_t *count);
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
int socket_t
void(* tcp_client_foreach_fn)(socket_t socket, void *client_data, void *user_arg)
Callback for iterating over clients.
void tcp_server_destroy(tcp_server_t *server)
Shutdown TCP server.
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.
void(* tcp_status_update_fn)(void *user_data)
Callback for periodic status updates.
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.
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.
ClangTool/LibTooling compatibility shim for stdbool.h.
Atomic value wrapper for integral/boolean types.
Definition atomic.h:76
Read-write lock type (POSIX: pthread_rwlock_t with debug tracking)
Definition rwlock.h:61
socket_t client_socket
Client connection socket.
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)
bool bind_ipv6
Whether to bind IPv6 socket.
void * status_update_data
User data passed to status update callback.
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)
const char * ipv6_address
IPv6 bind address (NULL or empty = don't bind)
double accept_timeout_sec
select() timeout in seconds (supports decimals, e.g. 0.05 for 50ms)
tcp_status_update_fn status_update_fn
Optional status update callback (called on timeout, NULL to disable)
atomic_t 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.
rwlock_t clients_rwlock
Read-write lock (allows concurrent readers, exclusive writers)
void thread_pool_t