|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
Go to the source code of this file.
Functions | |
| asciichat_error_t | tcp_server_init (tcp_server_t *server, const tcp_server_config_t *config) |
| Initialize TCP server. | |
| asciichat_error_t | tcp_server_run (tcp_server_t *server) |
| Run TCP server accept loop. | |
| void | tcp_server_shutdown (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. | |
| asciichat_error_t | tcp_server_add_client (tcp_server_t *server, socket_t socket, void *client_data) |
| Add client to registry. | |
| asciichat_error_t | tcp_server_remove_client (tcp_server_t *server, socket_t socket) |
| Remove client from registry. | |
| asciichat_error_t | tcp_server_get_client (tcp_server_t *server, socket_t socket, void **out_data) |
| Get client data. | |
| void | tcp_server_foreach_client (tcp_server_t *server, tcp_client_foreach_fn callback, void *user_arg) |
| Iterate over all clients. | |
| size_t | tcp_server_get_client_count (tcp_server_t *server) |
| Get client count. | |
| 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. | |
| int | tcp_client_context_get_port (const tcp_client_context_t *ctx) |
| Get port number from client context. | |
| void | tcp_server_reject_client (socket_t socket, const char *reason) |
| Reject client connection with reason. | |
| 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_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_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.
Extracts and formats the client IP address from the connection context. Works for both IPv4 and IPv6 addresses.
| ctx | Client context structure |
| buf | Output buffer for formatted IP string |
| len | Buffer size (recommend INET6_ADDRSTRLEN = 46 bytes) |
Definition at line 463 of file lib/network/tcp/server.c.
References tcp_client_context_t::addr, ERROR_INVALID_PARAM, format_ip_address(), and SET_ERRNO.
Referenced by acds_client_handler().
| int tcp_client_context_get_port | ( | const tcp_client_context_t * | ctx | ) |
Get port number from client context.
Extracts the client port number from the connection context. Works for both IPv4 and IPv6 addresses.
| ctx | Client context structure |
Definition at line 488 of file lib/network/tcp/server.c.
References tcp_client_context_t::addr, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, and SET_ERRNO.
| asciichat_error_t tcp_server_add_client | ( | tcp_server_t * | server, |
| socket_t | socket, | ||
| void * | client_data | ||
| ) |
Add client to registry.
Thread-safe registration of a connected client with arbitrary user data. The client_data pointer is stored as-is (caller retains ownership).
| server | Server structure |
| socket | Client socket (used as lookup key) |
| client_data | User-provided client data (can be NULL) |
Definition at line 337 of file lib/network/tcp/server.c.
References ASCIICHAT_OK, tcp_client_entry::client_data, tcp_server::clients, tcp_server::clients_mutex, ERROR_INVALID_PARAM, ERROR_MEMORY, INVALID_SOCKET_VALUE, log_debug, mutex_lock, mutex_unlock, SAFE_FREE, SAFE_MALLOC, SAFE_SNPRINTF, SET_ERRNO, tcp_client_entry::socket, thread_pool_create(), and tcp_client_entry::threads.
Referenced by acds_client_handler().
| void tcp_server_foreach_client | ( | tcp_server_t * | server, |
| tcp_client_foreach_fn | callback, | ||
| void * | user_arg | ||
| ) |
Iterate over all clients.
Thread-safe iteration over all connected clients. The callback is called once per client while holding the clients mutex.
| server | Server structure |
| callback | Function to call for each client |
| user_arg | User argument passed to callback |
Definition at line 432 of file lib/network/tcp/server.c.
References tcp_client_entry::client_data, tcp_server::clients, tcp_server::clients_mutex, mutex_lock, mutex_unlock, and tcp_client_entry::socket.
Referenced by signaling_broadcast(), signaling_relay_ice(), and signaling_relay_sdp().
| asciichat_error_t tcp_server_get_client | ( | tcp_server_t * | server, |
| socket_t | socket, | ||
| void ** | out_data | ||
| ) |
Get client data.
Thread-safe lookup of client data by socket.
| server | Server structure |
| socket | Client socket to look up |
| out_data | Output pointer for client data (set to NULL if not found) |
Definition at line 410 of file lib/network/tcp/server.c.
References ASCIICHAT_OK, tcp_client_entry::client_data, tcp_server::clients, tcp_server::clients_mutex, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, mutex_lock, mutex_unlock, and SET_ERRNO.
| size_t tcp_server_get_client_count | ( | tcp_server_t * | server | ) |
Get client count.
Thread-safe count of connected clients.
| server | Server structure |
Definition at line 447 of file lib/network/tcp/server.c.
References tcp_server::clients, tcp_server::clients_mutex, mutex_lock, and mutex_unlock.
Referenced by acds_client_handler(), and acds_server_shutdown().
| 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.
Thread-safe count of worker threads spawned for a client.
| server | Server structure | |
| client_socket | Client socket to query | |
| [out] | count | Output pointer for thread count (set to 0 if client not found) |
Definition at line 588 of file lib/network/tcp/server.c.
References ASCIICHAT_OK, tcp_server::clients, tcp_server::clients_mutex, ERROR_INVALID_PARAM, ERROR_NOT_FOUND, INVALID_SOCKET_VALUE, mutex_lock, mutex_unlock, SET_ERRNO, thread_pool_get_count(), and tcp_client_entry::threads.
| asciichat_error_t tcp_server_init | ( | tcp_server_t * | server, |
| const tcp_server_config_t * | config | ||
| ) |
Initialize TCP server.
Creates and binds TCP sockets according to configuration. At least one of IPv4 or IPv6 must be successfully bound.
| server | Server structure to initialize |
| config | Server configuration |
Definition at line 96 of file lib/network/tcp/server.c.
References ASCIICHAT_OK, tcp_server_config_t::bind_ipv4, tcp_server_config_t::bind_ipv6, tcp_server::cleanup_fn, tcp_server::clients, tcp_server::clients_mutex, tcp_server::config, ERROR_INVALID_PARAM, ERROR_NETWORK_BIND, ERROR_THREAD, INVALID_SOCKET_VALUE, tcp_server_config_t::ipv4_address, tcp_server_config_t::ipv6_address, tcp_server::listen_socket, tcp_server::listen_socket6, log_warn, mutex_init(), tcp_server_config_t::port, tcp_server::running, and SET_ERRNO.
Referenced by acds_server_init(), and server_main().
| void tcp_server_reject_client | ( | socket_t | socket, |
| const char * | reason | ||
| ) |
Reject client connection with reason.
Helper for rejecting clients due to rate limits, capacity limits, etc. Logs the rejection reason and closes the socket.
| socket | Client socket to close |
| reason | Human-readable rejection reason (for logging) |
Definition at line 507 of file lib/network/tcp/server.c.
References ERROR_INVALID_PARAM, INVALID_SOCKET_VALUE, log_warn, SET_ERRNO, and socket_close().
Referenced by acds_client_handler().
| asciichat_error_t tcp_server_remove_client | ( | tcp_server_t * | server, |
| socket_t | socket | ||
| ) |
Remove client from registry.
Thread-safe removal of a client. If a cleanup callback is set, it will be called with the client_data before removal.
| server | Server structure |
| socket | Client socket to remove |
Definition at line 373 of file lib/network/tcp/server.c.
References ASCIICHAT_OK, tcp_server::cleanup_fn, tcp_client_entry::client_data, tcp_server::clients, tcp_server::clients_mutex, ERROR_INVALID_PARAM, log_debug, mutex_lock, mutex_unlock, SAFE_FREE, SET_ERRNO, thread_pool_destroy(), and tcp_client_entry::threads.
Referenced by acds_client_handler().
| asciichat_error_t tcp_server_run | ( | tcp_server_t * | server | ) |
Run TCP server accept loop.
Accepts client connections and spawns handler threads. Blocks until server->running is set to false.
Uses select() with timeout to handle dual-stack sockets and allow responsive shutdown.
| server | Initialized server structure |
Definition at line 150 of file lib/network/tcp/server.c.
References tcp_server_config_t::accept_timeout_sec, tcp_client_context_t::addr, tcp_client_context_t::addr_len, ASCIICHAT_OK, asciichat_thread_create(), tcp_server_config_t::client_handler, tcp_client_context_t::client_socket, tcp_server::config, EINTR, ERROR_INVALID_PARAM, format_ip_address(), INVALID_SOCKET_VALUE, tcp_server::listen_socket, tcp_server::listen_socket6, log_debug, log_error, log_info, log_warn, tcp_server::running, SAFE_FREE, SAFE_MALLOC, SAFE_STRNCPY, SET_ERRNO, socket_close(), socket_fd_isset(), socket_fd_set(), socket_fd_zero(), socket_get_error_string(), socket_get_last_error(), socket_select(), tcp_client_context_t::user_data, and tcp_server_config_t::user_data.
Referenced by acds_server_run(), and server_main().
| void tcp_server_set_cleanup_callback | ( | tcp_server_t * | server, |
| tcp_client_cleanup_fn | cleanup_fn | ||
| ) |
Set client cleanup callback.
Sets the callback function that will be called when a client is removed from the registry. Use this to free any allocated client_data.
| server | Server structure |
| cleanup_fn | Cleanup callback (or NULL to disable) |
Definition at line 330 of file lib/network/tcp/server.c.
References tcp_server::cleanup_fn.
| void tcp_server_shutdown | ( | tcp_server_t * | server | ) |
Shutdown TCP server.
Closes listen sockets and cleans up resources. Does NOT wait for client threads to exit (caller's responsibility).
| server | Server structure to clean up |
Definition at line 273 of file lib/network/tcp/server.c.
References tcp_server::cleanup_fn, tcp_client_entry::client_data, tcp_server::clients, tcp_server::clients_mutex, INVALID_SOCKET_VALUE, tcp_server::listen_socket, tcp_server::listen_socket6, log_debug, log_info, mutex_destroy(), mutex_lock, mutex_unlock, tcp_server::running, SAFE_FREE, socket_close(), thread_pool_destroy(), and tcp_client_entry::threads.
Referenced by acds_server_init(), acds_server_shutdown(), and server_main().
| asciichat_error_t tcp_server_spawn_thread | ( | tcp_server_t * | server, |
| socket_t | client_socket, | ||
| void *(*)(void *) | thread_func, | ||
| void * | thread_arg, | ||
| int | stop_id, | ||
| const char * | thread_name | ||
| ) |
Spawn a worker thread for a client.
Creates and tracks a new worker thread for the specified client. Threads are identified by stop_id for ordered cleanup - lower stop_id values are stopped first when the client disconnects.
Example stop_id ordering:
| server | Server structure |
| client_socket | Client socket to spawn thread for |
| thread_func | Thread function to execute |
| thread_arg | Argument passed to thread function |
| stop_id | Cleanup order (lower = stop first) |
| thread_name | Thread name for debugging (max 63 chars) |
Definition at line 521 of file lib/network/tcp/server.c.
References ASCIICHAT_OK, tcp_server::clients, tcp_server::clients_mutex, ERROR_INVALID_PARAM, ERROR_NOT_FOUND, INVALID_SOCKET_VALUE, log_debug, mutex_lock, mutex_unlock, SET_ERRNO, thread_pool_get_count(), thread_pool_spawn(), and tcp_client_entry::threads.
Referenced by create_client_render_threads().
| 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.
Stops all worker threads spawned for the specified client. Threads are stopped in ascending stop_id order (lower values first). Joins each thread to ensure it has fully exited before proceeding.
| server | Server structure |
| client_socket | Client socket whose threads to stop |
Definition at line 557 of file lib/network/tcp/server.c.
References ASCIICHAT_OK, tcp_server::clients, tcp_server::clients_mutex, ERROR_INVALID_PARAM, ERROR_NOT_FOUND, INVALID_SOCKET_VALUE, log_debug, mutex_lock, mutex_unlock, SET_ERRNO, thread_pool_stop_all(), and tcp_client_entry::threads.