|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
Go to the source code of this file.
Data Structures | |
| struct | tcp_client_audio_packet_t |
| Audio packet for async network transmission. More... | |
| struct | tcp_client |
| TCP client connection and state management. More... | |
Macros | |
| #define | TCP_CLIENT_AUDIO_QUEUE_SIZE 256 |
Typedefs | |
| typedef struct tcp_client | tcp_client_t |
| TCP client connection and state management. | |
Functions | |
| tcp_client_t * | tcp_client_create (void) |
| Create and initialize a TCP client instance. | |
| void | tcp_client_destroy (tcp_client_t **client_ptr) |
| Destroy TCP client and free all resources. | |
| bool | tcp_client_is_active (const tcp_client_t *client) |
| Check if connection is currently active. | |
| bool | tcp_client_is_lost (const tcp_client_t *client) |
| Check if connection was lost. | |
| socket_t | tcp_client_get_socket (const tcp_client_t *client) |
| Get current socket descriptor. | |
| uint32_t | tcp_client_get_id (const tcp_client_t *client) |
| Get client ID assigned by server. | |
| void | tcp_client_signal_lost (tcp_client_t *client) |
| Signal that connection was lost (triggers reconnection) | |
| void | tcp_client_close (tcp_client_t *client) |
| Close connection gracefully. | |
| void | tcp_client_shutdown (tcp_client_t *client) |
| Shutdown connection forcefully (for signal handlers) | |
| void | tcp_client_cleanup (tcp_client_t *client) |
| Cleanup connection resources. | |
| int | tcp_client_connect (tcp_client_t *client, const char *address, int port, int reconnect_attempt, bool first_connection, bool has_ever_connected) |
| Establish TCP connection to server. | |
| int | tcp_client_send_packet (tcp_client_t *client, packet_type_t type, const void *data, size_t len) |
| Send packet with thread-safe mutex protection. | |
| int | tcp_client_send_ping (tcp_client_t *client) |
| Send ping packet. | |
| int | tcp_client_send_pong (tcp_client_t *client) |
| Send pong packet. | |
| int | tcp_client_send_audio_opus (tcp_client_t *client, const uint8_t *opus_data, size_t opus_size, int sample_rate, int frame_duration) |
| Send Opus-encoded audio frame. | |
| int | tcp_client_send_audio_opus_batch (tcp_client_t *client, const uint8_t *opus_data, size_t opus_size, const uint16_t *frame_sizes, int frame_count) |
| Send Opus audio batch packet. | |
| int | tcp_client_send_terminal_capabilities (tcp_client_t *client, unsigned short width, unsigned short height) |
| Send terminal capabilities packet. | |
| int | tcp_client_send_join (tcp_client_t *client, const char *display_name, uint32_t capabilities) |
| Send client join packet. | |
| int | tcp_client_send_stream_start (tcp_client_t *client, uint32_t stream_type) |
| Send stream start packet. | |
| int | tcp_client_send_audio_batch (tcp_client_t *client, const float *samples, int num_samples, int batch_count) |
| Send audio batch packet. | |
| #define TCP_CLIENT_AUDIO_QUEUE_SIZE 256 |
Definition at line 64 of file lib/network/tcp/client.h.
| typedef struct tcp_client tcp_client_t |
TCP client connection and state management.
Encapsulates all state for a single TCP client connection, including:
| void tcp_client_cleanup | ( | tcp_client_t * | client | ) |
Cleanup connection resources.
| client | TCP client instance |
Definition at line 317 of file lib/network/tcp/client.c.
References tcp_client::connection_lost, tcp_client::server_ip, tcp_client::should_reconnect, and tcp_client_close().
| void tcp_client_close | ( | tcp_client_t * | client | ) |
Close connection gracefully.
| client | TCP client instance |
Definition at line 280 of file lib/network/tcp/client.c.
References tcp_client::connection_active, INVALID_SOCKET_VALUE, log_debug, tcp_client::my_client_id, socket_is_valid(), and tcp_client::sockfd.
Referenced by tcp_client_cleanup().
| int tcp_client_connect | ( | tcp_client_t * | client, |
| const char * | address, | ||
| int | port, | ||
| int | reconnect_attempt, | ||
| bool | first_connection, | ||
| bool | has_ever_connected | ||
| ) |
Establish TCP connection to server.
Performs full connection lifecycle including DNS resolution, socket creation, connection with timeout, and socket configuration. Does NOT perform crypto handshake or send initial packets - those are application responsibilities.
| client | TCP client instance |
| address | Server hostname or IP address |
| port | Server port number |
| reconnect_attempt | Current reconnection attempt (0 for first, 1+ for retries) |
| first_connection | True if this is the very first connection since program start |
| has_ever_connected | True if client has successfully connected at least once |
Performs full connection lifecycle:
| client | TCP client instance |
| address | Server hostname or IP address |
| port | Server port number |
| reconnect_attempt | Current reconnection attempt (0 for first, 1+ for retries) |
| first_connection | True if this is the very first connection since program start |
| has_ever_connected | True if client has successfully connected at least once |
Definition at line 411 of file lib/network/tcp/client.c.
References ASCIICHAT_OK, CONNECT_TIMEOUT, connect_with_timeout(), tcp_client::connection_active, tcp_client::connection_lost, ERROR_INVALID_PARAM, format_ip_address(), INVALID_SOCKET_VALUE, log_debug, log_error, log_info, log_warn, tcp_client::my_client_id, NET_TO_HOST_U16, network_error_string(), platform_sleep_usec(), SAFE_SNPRINTF, SAFE_STRNCPY, tcp_client::server_ip, SET_ERRNO, tcp_client::should_reconnect, socket_configure_buffers(), socket_create(), socket_is_valid(), socket_set_keepalive(), and tcp_client::sockfd.
| tcp_client_t * tcp_client_create | ( | void | ) |
Create and initialize a TCP client instance.
Allocates a new tcp_client_t structure and initializes all fields to safe defaults. This function must be called before starting any worker threads.
Returns NULL if allocation fails or mutex initialization fails. Check errno or use HAS_ERRNO() for detailed error information.
Create and initialize a TCP client instance.
Allocates tcp_client_t and initializes all fields to safe defaults.
Definition at line 95 of file lib/network/tcp/client.c.
References tcp_client::audio_capture_thread_created, tcp_client::audio_capture_thread_exited, tcp_client::audio_ctx, tcp_client::audio_send_queue, tcp_client::audio_send_queue_cond, tcp_client::audio_send_queue_head, tcp_client::audio_send_queue_initialized, tcp_client::audio_send_queue_mutex, tcp_client::audio_send_queue_tail, tcp_client::audio_sender_should_exit, tcp_client::audio_sender_thread_created, tcp_client::capture_thread_created, tcp_client::capture_thread_exited, cond_init(), tcp_client::connection_active, tcp_client::connection_lost, tcp_client::crypto_ctx, tcp_client::crypto_initialized, tcp_client::data_thread_created, tcp_client::data_thread_exited, tcp_client::encryption_enabled, tcp_client::has_tty, INVALID_SOCKET_VALUE, tcp_client::is_first_frame_of_connection, tcp_client::last_active_count, log_debug, log_error, mutex_destroy(), mutex_init(), tcp_client::my_client_id, tcp_client::ping_thread_created, tcp_client::ping_thread_exited, SAFE_FREE, SAFE_MALLOC, tcp_client::send_mutex, tcp_client::server_ip, tcp_client::server_state_initialized, tcp_client::should_clear_before_next_frame, tcp_client::should_reconnect, tcp_client::sockfd, and tcp_client::tty_info.
| void tcp_client_destroy | ( | tcp_client_t ** | client_ptr | ) |
Destroy TCP client and free all resources.
Destroys all synchronization primitives and frees the client structure. This function must be called AFTER all worker threads have been joined.
This function is NOT thread-safe. All worker threads must be joined before calling this function.
| client_ptr | Pointer to client pointer (set to NULL after free) |
Destroy TCP client and free all resources.
Must be called AFTER all threads have been joined.
Definition at line 182 of file lib/network/tcp/client.c.
References tcp_client::audio_capture_thread_created, tcp_client::audio_capture_thread_exited, tcp_client::audio_send_queue_cond, tcp_client::audio_send_queue_mutex, tcp_client::capture_thread_created, tcp_client::capture_thread_exited, cond_destroy(), tcp_client::data_thread_created, tcp_client::data_thread_exited, INVALID_SOCKET_VALUE, log_debug, log_warn, mutex_destroy(), tcp_client::ping_thread_created, tcp_client::ping_thread_exited, SAFE_FREE, tcp_client::send_mutex, socket_is_valid(), and tcp_client::sockfd.
Referenced by connection_context_cleanup().
| uint32_t tcp_client_get_id | ( | const tcp_client_t * | client | ) |
Get client ID assigned by server.
| client | TCP client instance |
Definition at line 255 of file lib/network/tcp/client.c.
References tcp_client::my_client_id.
| socket_t tcp_client_get_socket | ( | const tcp_client_t * | client | ) |
Get current socket descriptor.
| client | TCP client instance |
Definition at line 248 of file lib/network/tcp/client.c.
References INVALID_SOCKET_VALUE, and tcp_client::sockfd.
| bool tcp_client_is_active | ( | const tcp_client_t * | client | ) |
Check if connection is currently active.
| client | TCP client instance |
Definition at line 230 of file lib/network/tcp/client.c.
References tcp_client::connection_active.
| bool tcp_client_is_lost | ( | const tcp_client_t * | client | ) |
Check if connection was lost.
| client | TCP client instance |
Definition at line 239 of file lib/network/tcp/client.c.
References tcp_client::connection_lost.
| int tcp_client_send_audio_batch | ( | tcp_client_t * | client, |
| const float * | samples, | ||
| int | num_samples, | ||
| int | batch_count | ||
| ) |
Send audio batch packet.
| client | TCP client instance |
| samples | Audio sample buffer |
| num_samples | Number of samples in buffer |
| batch_count | Number of chunks in batch |
Definition at line 878 of file lib/network/tcp/client.c.
References tcp_client::connection_active, tcp_client::crypto_ctx, crypto_handshake_get_context(), crypto_handshake_is_ready(), tcp_client::crypto_initialized, ERROR_INVALID_PARAM, ERROR_NETWORK, INVALID_SOCKET_VALUE, mutex_lock, mutex_unlock, send_audio_batch_packet(), tcp_client::send_mutex, SET_ERRNO, tcp_client::sockfd, and tcp_client_signal_lost().
| int tcp_client_send_audio_opus | ( | tcp_client_t * | client, |
| const uint8_t * | opus_data, | ||
| size_t | opus_size, | ||
| int | sample_rate, | ||
| int | frame_duration | ||
| ) |
Send Opus-encoded audio frame.
| client | TCP client instance |
| opus_data | Opus-encoded audio data |
| opus_size | Size of encoded frame |
| sample_rate | Sample rate in Hz |
| frame_duration | Frame duration in milliseconds |
Definition at line 618 of file lib/network/tcp/client.c.
References ASCIICHAT_OK, buffer_pool_alloc(), buffer_pool_free(), tcp_client::connection_active, tcp_client::crypto_ctx, crypto_handshake_get_context(), crypto_handshake_is_ready(), tcp_client::crypto_initialized, ERROR_INVALID_PARAM, ERROR_MEMORY, ERROR_NETWORK, HOST_TO_NET_U32, INVALID_SOCKET_VALUE, mutex_lock, mutex_unlock, packet_send(), PACKET_TYPE_AUDIO_OPUS, tcp_client::send_mutex, send_packet_secure(), SET_ERRNO, tcp_client::sockfd, and tcp_client_signal_lost().
| int tcp_client_send_audio_opus_batch | ( | tcp_client_t * | client, |
| const uint8_t * | opus_data, | ||
| size_t | opus_size, | ||
| const uint16_t * | frame_sizes, | ||
| int | frame_count | ||
| ) |
Send Opus audio batch packet.
| client | TCP client instance |
| opus_data | Opus-encoded audio data (multiple frames) |
| opus_size | Total size of Opus data |
| frame_sizes | Array of individual frame sizes |
| frame_count | Number of frames in batch |
Definition at line 690 of file lib/network/tcp/client.c.
References av_send_audio_opus_batch(), tcp_client::connection_active, tcp_client::crypto_ctx, crypto_handshake_get_context(), crypto_handshake_is_ready(), tcp_client::crypto_initialized, ERROR_INVALID_PARAM, ERROR_NETWORK, INVALID_SOCKET_VALUE, mutex_lock, mutex_unlock, tcp_client::send_mutex, SET_ERRNO, tcp_client::sockfd, and tcp_client_signal_lost().
| int tcp_client_send_join | ( | tcp_client_t * | client, |
| const char * | display_name, | ||
| uint32_t | capabilities | ||
| ) |
Send client join packet.
| client | TCP client instance |
| display_name | Client display name |
| capabilities | Client capability flags |
Definition at line 814 of file lib/network/tcp/client.c.
References client_info_packet_t::capabilities, client_info_packet_t::client_id, tcp_client::connection_active, tcp_client::crypto_ctx, crypto_handshake_get_context(), crypto_handshake_is_ready(), tcp_client::crypto_initialized, client_info_packet_t::display_name, ERROR_INVALID_PARAM, HOST_TO_NET_U32, INVALID_SOCKET_VALUE, LOG_INFO, log_network_message(), MAX_DISPLAY_NAME_LEN, mutex_lock, mutex_unlock, PACKET_TYPE_CLIENT_JOIN, REMOTE_LOG_DIRECTION_CLIENT_TO_SERVER, SAFE_MEMSET, SAFE_SNPRINTF, tcp_client::send_mutex, SET_ERRNO, tcp_client::sockfd, and tcp_client_send_packet().
| int tcp_client_send_packet | ( | tcp_client_t * | client, |
| packet_type_t | type, | ||
| const void * | data, | ||
| size_t | len | ||
| ) |
Send packet with thread-safe mutex protection.
All packet transmission goes through this function to ensure packets aren't interleaved on the wire. Automatically handles encryption if crypto context is ready.
| client | TCP client instance |
| type | Packet type identifier |
| data | Packet payload |
| len | Payload length |
All packet transmission goes through this function to ensure packets aren't interleaved on the wire.
Definition at line 340 of file lib/network/tcp/client.c.
References ASCIICHAT_OK, tcp_client::connection_active, tcp_client::crypto_ctx, crypto_handshake_get_context(), crypto_handshake_is_ready(), tcp_client::crypto_initialized, ERROR_INVALID_PARAM, ERROR_NETWORK, log_debug, mutex_lock, mutex_unlock, tcp_client::send_mutex, send_packet_secure(), SET_ERRNO, and tcp_client::sockfd.
Referenced by tcp_client_send_join(), tcp_client_send_ping(), tcp_client_send_pong(), tcp_client_send_stream_start(), and tcp_client_send_terminal_capabilities().
| int tcp_client_send_ping | ( | tcp_client_t * | client | ) |
Send ping packet.
| client | TCP client instance |
Definition at line 374 of file lib/network/tcp/client.c.
References PACKET_TYPE_PING, and tcp_client_send_packet().
| int tcp_client_send_pong | ( | tcp_client_t * | client | ) |
Send pong packet.
| client | TCP client instance |
Definition at line 383 of file lib/network/tcp/client.c.
References PACKET_TYPE_PONG, and tcp_client_send_packet().
| int tcp_client_send_stream_start | ( | tcp_client_t * | client, |
| uint32_t | stream_type | ||
| ) |
Send stream start packet.
| client | TCP client instance |
| stream_type | Type of stream (audio/video) |
Definition at line 856 of file lib/network/tcp/client.c.
References tcp_client::connection_active, ERROR_INVALID_PARAM, HOST_TO_NET_U32, INVALID_SOCKET_VALUE, PACKET_TYPE_STREAM_START, SET_ERRNO, tcp_client::sockfd, and tcp_client_send_packet().
| int tcp_client_send_terminal_capabilities | ( | tcp_client_t * | client, |
| unsigned short | width, | ||
| unsigned short | height | ||
| ) |
Send terminal capabilities packet.
| client | TCP client instance |
| width | Terminal width in characters |
| height | Terminal height in characters |
Definition at line 733 of file lib/network/tcp/client.c.
References apply_color_mode_override(), terminal_capabilities_packet_t::capabilities, terminal_capabilities_t::capabilities, terminal_capabilities_packet_t::color_count, terminal_capabilities_t::color_count, terminal_capabilities_packet_t::color_level, terminal_capabilities_t::color_level, COLOR_MODE_AUTO, terminal_capabilities_packet_t::colorterm, terminal_capabilities_t::colorterm, tcp_client::connection_active, DEFAULT_MAX_FPS, terminal_capabilities_packet_t::desired_fps, terminal_capabilities_t::desired_fps, detect_terminal_capabilities(), terminal_capabilities_packet_t::detection_reliable, terminal_capabilities_t::detection_reliable, ERROR_INVALID_PARAM, g_max_fps, GET_OPTION, terminal_capabilities_packet_t::height, HOST_TO_NET_U16, HOST_TO_NET_U32, INVALID_SOCKET_VALUE, log_warn, options_get(), PACKET_TYPE_CLIENT_CAPABILITIES, terminal_capabilities_packet_t::palette_custom, PALETTE_CUSTOM, terminal_capabilities_packet_t::palette_type, terminal_capabilities_packet_t::render_mode, terminal_capabilities_t::render_mode, terminal_capabilities_packet_t::reserved, SAFE_MEMSET, SAFE_STRNCPY, SET_ERRNO, tcp_client::sockfd, tcp_client_send_packet(), TERM_COLOR_NONE, terminal_capabilities_packet_t::term_type, terminal_capabilities_t::term_type, terminal_capabilities_packet_t::utf8_support, terminal_capabilities_t::utf8_support, and terminal_capabilities_packet_t::width.
| void tcp_client_shutdown | ( | tcp_client_t * | client | ) |
Shutdown connection forcefully (for signal handlers)
| client | TCP client instance |
Definition at line 302 of file lib/network/tcp/client.c.
References tcp_client::connection_active, socket_is_valid(), socket_shutdown(), and tcp_client::sockfd.
| void tcp_client_signal_lost | ( | tcp_client_t * | client | ) |
Signal that connection was lost (triggers reconnection)
| client | TCP client instance |
Definition at line 266 of file lib/network/tcp/client.c.
References tcp_client::connection_active, tcp_client::connection_lost, and log_info.
Referenced by tcp_client_send_audio_batch(), tcp_client_send_audio_opus(), and tcp_client_send_audio_opus_batch().