|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
π Server connection establishment, packet transmission, and state tracking More...
Files | |
| file | server.c |
| π Client connection manager: TCP connection, reconnection with exponential backoff, and thread-safe transmission | |
| file | server.h |
| ascii-chat Client Server Connection Management Interface | |
Functions | |
| int | server_connection_init () |
| Initialize the server connection management subsystem. | |
| int | server_connection_establish (const char *address, int port, int reconnect_attempt, bool first_connection, bool has_ever_connected) |
| Establish connection to ascii-chat server. | |
| bool | server_connection_is_active () |
| Check if server connection is currently active. | |
| socket_t | server_connection_get_socket () |
| Get current socket file descriptor. | |
| acip_transport_t * | server_connection_get_transport (void) |
| Get ACIP transport instance. | |
| void | server_connection_set_transport (acip_transport_t *transport) |
| Set ACIP transport instance from connection fallback. | |
| uint32_t | server_connection_get_client_id () |
| Get client ID assigned by server. | |
| const char * | server_connection_get_ip () |
| Get resolved server IP address. | |
| void | server_connection_set_ip (const char *ip) |
| Set the server IP address. | |
| void | server_connection_close () |
| Close the server connection gracefully. | |
| void | server_connection_shutdown () |
| Emergency connection shutdown for signal handlers. | |
| void | server_connection_lost () |
| Signal that connection has been lost. | |
| bool | server_connection_is_lost () |
| Check if connection loss has been detected. | |
| void | server_connection_cleanup () |
| Cleanup connection management subsystem. | |
| asciichat_error_t | threaded_send_packet (packet_type_t type, const void *data, size_t len) |
| Thread-safe packet transmission. | |
| int | threaded_send_audio_batch_packet (const float *samples, int num_samples, int batch_count) |
| Thread-safe batched audio packet transmission. | |
| asciichat_error_t | threaded_send_audio_opus (const uint8_t *opus_data, size_t opus_size, int sample_rate, int frame_duration) |
| Thread-safe Opus audio frame transmission. | |
| asciichat_error_t | threaded_send_audio_opus_batch (const uint8_t *opus_data, size_t opus_size, const uint16_t *frame_sizes, int frame_count) |
| Thread-safe Opus audio batch packet transmission. | |
| int | threaded_send_ping_packet (void) |
| Thread-safe ping packet transmission. | |
| int | threaded_send_pong_packet (void) |
| Thread-safe pong packet transmission. | |
| int | threaded_send_stream_start_packet (uint32_t stream_type) |
| Thread-safe stream start packet transmission. | |
| int | threaded_send_terminal_size_with_auto_detect (unsigned short width, unsigned short height) |
| Thread-safe terminal size packet transmission with auto-detection. | |
| int | threaded_send_client_join_packet (const char *display_name, uint32_t capabilities) |
| Thread-safe client join packet transmission. | |
| int | server_send_packet (packet_type_t type, const void *data, size_t len) |
| Send general packet to server. | |
| int | server_send_audio (const float *samples, int num_samples) |
| Send audio data to server. | |
| int | server_send_audio_batch (const float *samples, int num_samples, int batch_count) |
| Send batched audio data to server. | |
| int | server_send_terminal_capabilities (unsigned short width, unsigned short height) |
| Send terminal capabilities to server. | |
| int | server_send_ping () |
| Send ping keepalive packet. | |
| int | server_send_pong () |
| Send pong response packet. | |
| int | server_send_stream_start (uint32_t stream_type) |
| Send stream start notification. | |
| int | server_send_stream_stop (uint32_t stream_type) |
| Send stream stop notification. | |
Variables | |
| crypto_handshake_context_t | g_crypto_ctx = {0} |
| Per-connection crypto handshake context. | |
π Server connection establishment, packet transmission, and state tracking
The connection management subsystem handles TCP socket creation, connection establishment, cryptographic handshake coordination, and thread-safe packet transmission to the server.
Implementation: src/client/server.c, src/client/server.h
Global State Variables:
g_sockfd: Socket file descriptorg_client_id: Server-assigned client IDg_server_ip: Resolved server IP addressg_connection_active: Atomic boolean for connection statusg_connection_lost: Atomic boolean for connection loss detectiong_send_mutex: Mutex protecting all packet sendsError Types:
CONNECTION_SUCCESS (0): Connection established successfullyCONNECTION_WARNING_NO_CLIENT_AUTH (1): Connected but server not verifying clientCONNECTION_ERROR_GENERIC (-1): Network error (allow retry)CONNECTION_ERROR_AUTH_FAILED (-2): Client authentication failed (no retry)CONNECTION_ERROR_HOST_KEY_FAILED (-3): Server host key verification failed (no retry)Thread Safety:
send_packet_to_server()g_send_mutex prevents interleaved packetsConnection loss detected by:
send_packet_to_server()receive_packet() in protocol threadDO:
send_packet_to_server() for packet transmissionserver_connection_is_active() before operationsDON'T:
g_send_mutexg_sockfd directly (use provided functions)| void server_connection_cleanup | ( | ) |
#include <server.c>
Cleanup connection management subsystem.
Closes any active connection and destroys synchronization objects. Called during client shutdown.
Definition at line 858 of file src/client/server.c.
References log_set_terminal_output(), mutex_destroy(), and server_connection_close().
| void server_connection_close | ( | ) |
#include <server.c>
Close the server connection gracefully.
Close server connection gracefully.
Marks connection as inactive and closes the socket. This function is safe to call multiple times and from multiple threads.
Definition at line 765 of file src/client/server.c.
References acip_transport_destroy(), close_socket, crypto_handshake_cleanup(), g_crypto_ctx, INVALID_SOCKET_VALUE, and log_set_terminal_output().
Referenced by client_main(), and server_connection_cleanup().
| int server_connection_establish | ( | const char * | address, |
| int | port, | ||
| int | reconnect_attempt, | ||
| bool | first_connection, | ||
| bool | has_ever_connected | ||
| ) |
#include <server.c>
Establish connection to ascii-chat server.
Attempts to connect to the specified server with full capability negotiation. Implements reconnection logic with exponential backoff for failed attempts. On successful connection, performs initial handshake including terminal capabilities and client join protocol.
| address | Server IP address or hostname |
| port | Server port number |
| reconnect_attempt | Current reconnection attempt number (0 for first) |
| first_connection | True if this is the initial connection attempt |
| has_ever_connected | True if a connection was ever successfully established |
| address | Server IP address or hostname |
| port | Server port number |
| reconnect_attempt | Current reconnection attempt (0 for first) |
| first_connection | True if this is the initial connection |
| has_ever_connected | True if a connection was ever successfully established |
Definition at line 322 of file src/client/server.c.
References acip_tcp_transport_create(), ASCIICHAT_OK, CLIENT_CAP_AUDIO, CLIENT_CAP_COLOR, CLIENT_CAP_STRETCH, CLIENT_CAP_VIDEO, client_crypto_handshake(), client_crypto_init(), close_socket, COLOR_MODE_NONE, CONNECT_TIMEOUT, connect_with_timeout(), CONNECTION_ERROR_AUTH_FAILED, crypto_client_get_context(), crypto_client_is_ready(), ERROR_CRYPTO_HANDSHAKE, FATAL, format_ip_address(), GET_OPTION, INVALID_SOCKET_VALUE, log_debug, log_error, log_info, log_set_terminal_output(), log_warn, MAX_DISPLAY_NAME_LEN, NET_TO_HOST_U16, network_error_string(), platform_get_username(), platform_sleep_usec(), SAFE_SNPRINTF, SAFE_STRNCPY, should_exit(), socket_configure_buffers(), socket_create(), socket_get_error(), socket_set_keepalive(), threaded_send_client_join_packet(), and threaded_send_terminal_size_with_auto_detect().
| uint32_t server_connection_get_client_id | ( | ) |
#include <server.c>
Get client ID assigned by server.
Definition at line 719 of file src/client/server.c.
| const char * server_connection_get_ip | ( | ) |
#include <server.c>
Get resolved server IP address.
Returns the server's IP address that was resolved during connection establishment. Used for known_hosts verification and logging.
Definition at line 733 of file src/client/server.c.
Referenced by client_crypto_init().
| socket_t server_connection_get_socket | ( | ) |
#include <server.c>
Get current socket file descriptor.
Definition at line 657 of file src/client/server.c.
Referenced by crypto_client_initiate_rekey(), crypto_client_send_rekey_complete(), crypto_client_send_rekey_response(), threaded_send_audio_batch_packet(), threaded_send_client_join_packet(), threaded_send_stream_start_packet(), and threaded_send_terminal_size_with_auto_detect().
| struct acip_transport * server_connection_get_transport | ( | void | ) |
#include <server.c>
Get ACIP transport instance.
Definition at line 668 of file src/client/server.c.
| int server_connection_init | ( | ) |
#include <server.c>
Initialize the server connection management subsystem.
Initialize server connection management subsystem.
Sets up the send mutex and initializes connection state variables. Must be called once during client startup before any connection attempts.
Definition at line 287 of file src/client/server.c.
References INVALID_SOCKET_VALUE, log_error, and mutex_init().
| bool server_connection_is_active | ( | ) |
#include <server.c>
Check if server connection is currently active.
Definition at line 646 of file src/client/server.c.
References INVALID_SOCKET_VALUE.
Referenced by __attribute__(), and client_main().
| bool server_connection_is_lost | ( | ) |
#include <server.c>
Check if connection loss has been detected.
Check if connection loss was detected.
Definition at line 846 of file src/client/server.c.
Referenced by __attribute__(), and protocol_connection_lost().
| void server_connection_lost | ( | ) |
#include <server.c>
Signal that connection has been lost.
Called by other modules (typically protocol handlers) when they detect connection failure. Triggers reconnection logic in main loop.
Definition at line 830 of file src/client/server.c.
References display_full_reset(), and log_set_terminal_output().
Referenced by __attribute__(), threaded_send_audio_batch_packet(), threaded_send_audio_opus(), threaded_send_audio_opus_batch(), and threaded_send_packet().
| void server_connection_set_ip | ( | const char * | ip | ) |
#include <server.c>
Set the server IP address.
Updates the global server IP address. Used by new connection code paths that don't use the legacy server_connect() function.
| ip | Server IP address string |
| ip | Server IP address string |
Definition at line 747 of file src/client/server.c.
References log_debug, and SAFE_STRNCPY.
| void server_connection_set_transport | ( | acip_transport_t * | transport | ) |
#include <server.c>
Set ACIP transport instance from connection fallback.
Used to integrate the transport from the 3-stage connection fallback orchestrator (TCP β STUN β TURN) into the server connection management layer.
| transport | Transport instance created by connection_attempt_with_fallback() |
| transport | Transport instance created by connection_attempt_with_fallback() |
Used to integrate the transport from the 3-stage connection fallback orchestrator (TCP β STUN β TURN) into the server connection management layer.
Definition at line 682 of file src/client/server.c.
References acip_transport_destroy(), INVALID_SOCKET_VALUE, log_debug, and log_warn.
Referenced by client_main().
| void server_connection_shutdown | ( | ) |
#include <server.c>
Emergency connection shutdown for signal handlers.
Emergency shutdown for signal handlers.
Performs immediate connection shutdown without waiting for graceful close procedures. Uses socket shutdown to interrupt any blocking recv() operations in other threads.
Definition at line 800 of file src/client/server.c.
References INVALID_SOCKET_VALUE, and socket_shutdown().
Referenced by protocol_stop_connection().
| int server_send_audio | ( | const float * | samples, |
| int | num_samples | ||
| ) |
#include <server.h>
Send audio data to server.
| samples | Audio sample buffer |
| num_samples | Number of samples |
| int server_send_audio_batch | ( | const float * | samples, |
| int | num_samples, | ||
| int | batch_count | ||
| ) |
#include <server.h>
Send batched audio data to server.
| samples | Batched audio samples |
| num_samples | Total sample count |
| batch_count | Number of packets in batch |
| int server_send_packet | ( | packet_type_t | type, |
| const void * | data, | ||
| size_t | len | ||
| ) |
#include <server.h>
Send general packet to server.
| type | Packet type identifier |
| data | Packet payload |
| len | Payload length |
| int server_send_ping | ( | ) |
| int server_send_pong | ( | ) |
| int server_send_stream_start | ( | uint32_t | stream_type | ) |
#include <server.h>
Send stream start notification.
| stream_type | Type of stream (audio/video) |
| int server_send_stream_stop | ( | uint32_t | stream_type | ) |
#include <server.h>
Send stream stop notification.
| stream_type | Type of stream (audio/video) |
| int server_send_terminal_capabilities | ( | unsigned short | width, |
| unsigned short | height | ||
| ) |
#include <server.h>
Send terminal capabilities to server.
| width | Terminal width in characters |
| height | Terminal height in characters |
| int threaded_send_audio_batch_packet | ( | const float * | samples, |
| int | num_samples, | ||
| int | batch_count | ||
| ) |
#include <server.c>
Thread-safe batched audio packet transmission.
Sends a batched audio packet to the server with proper mutex protection and connection state checking. Automatically handles encryption if crypto is ready.
| samples | Audio sample buffer containing batched samples |
| num_samples | Total number of samples in the batch |
| batch_count | Number of audio chunks in this batch |
| samples | Audio sample buffer containing batched samples |
| num_samples | Total number of samples in the batch |
| batch_count | Number of audio chunks in this batch |
Definition at line 922 of file src/client/server.c.
References crypto_client_get_context(), crypto_client_is_ready(), INVALID_SOCKET_VALUE, mutex_lock, mutex_unlock, send_audio_batch_packet(), server_connection_get_socket(), and server_connection_lost().
| asciichat_error_t threaded_send_audio_opus | ( | const uint8_t * | opus_data, |
| size_t | opus_size, | ||
| int | sample_rate, | ||
| int | frame_duration | ||
| ) |
#include <server.c>
Thread-safe Opus audio frame transmission.
Sends a single Opus-encoded audio frame to the server with proper synchronization and encryption support.
| opus_data | Opus-encoded audio data |
| opus_size | Size of encoded frame |
| sample_rate | Sample rate in Hz |
| frame_duration | Frame duration in milliseconds |
Sends a single Opus-encoded audio frame to the server with proper synchronization and encryption support.
| 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 960 of file src/client/server.c.
References ASCIICHAT_OK, buffer_pool_alloc(), buffer_pool_free(), ERROR_MEMORY, ERROR_NETWORK, HOST_TO_NET_U32, mutex_lock, mutex_unlock, packet_send_via_transport(), PACKET_TYPE_AUDIO_OPUS, server_connection_lost(), and SET_ERRNO.
| asciichat_error_t threaded_send_audio_opus_batch | ( | const uint8_t * | opus_data, |
| size_t | opus_size, | ||
| const uint16_t * | frame_sizes, | ||
| int | frame_count | ||
| ) |
#include <server.c>
Thread-safe Opus audio batch packet transmission.
Sends a batch of Opus-encoded audio frames to the server with proper synchronization and encryption support.
| opus_data | Opus-encoded audio data (multiple frames concatenated) |
| opus_size | Total size of Opus data in bytes |
| frame_sizes | Array of individual frame sizes (variable-length frames) |
| frame_count | Number of Opus frames in the batch |
Sends a batch of Opus-encoded audio frames to the server with proper synchronization and encryption support.
| opus_data | Opus-encoded audio data (multiple frames concatenated) |
| opus_size | Total size of Opus data in bytes |
| frame_sizes | Array of individual frame sizes (variable-length frames) |
| frame_count | Number of Opus frames in the batch |
Definition at line 1021 of file src/client/server.c.
References acip_send_audio_opus_batch(), ASCIICHAT_OK, ERROR_NETWORK, mutex_lock, mutex_unlock, server_connection_lost(), and SET_ERRNO.
| int threaded_send_client_join_packet | ( | const char * | display_name, |
| uint32_t | capabilities | ||
| ) |
#include <server.c>
Thread-safe client join packet transmission.
Sends client join packet with display name and capabilities to the server.
| display_name | Client display name |
| capabilities | Client capability flags |
| display_name | Client display name |
| capabilities | Client capability flags |
Definition at line 1187 of file src/client/server.c.
References client_info_packet_t::capabilities, client_info_packet_t::client_id, crypto_client_get_context(), crypto_client_is_ready(), client_info_packet_t::display_name, 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, server_connection_get_socket(), and threaded_send_packet().
Referenced by server_connection_establish().
| asciichat_error_t threaded_send_packet | ( | packet_type_t | type, |
| const void * | data, | ||
| size_t | len | ||
| ) |
#include <server.c>
Thread-safe packet transmission.
Sends a packet to the server with proper mutex protection and connection state checking. Automatically handles encryption if crypto is ready.
| type | Packet type identifier |
| data | Packet payload |
| len | Payload length |
Sends a packet to the server with proper mutex protection and connection state checking. Automatically handles encryption if crypto is ready.
| type | Packet type identifier |
| data | Packet payload |
| len | Payload length |
Definition at line 885 of file src/client/server.c.
References ASCIICHAT_OK, ERROR_NETWORK, mutex_lock, mutex_unlock, packet_send_via_transport(), server_connection_lost(), and SET_ERRNO.
Referenced by threaded_send_client_join_packet(), threaded_send_ping_packet(), threaded_send_pong_packet(), threaded_send_stream_start_packet(), and threaded_send_terminal_size_with_auto_detect().
| int threaded_send_ping_packet | ( | void | ) |
#include <server.c>
Thread-safe ping packet transmission.
Definition at line 1053 of file src/client/server.c.
References PACKET_TYPE_PING, and threaded_send_packet().
Referenced by __attribute__().
| int threaded_send_pong_packet | ( | void | ) |
#include <server.c>
Thread-safe pong packet transmission.
Definition at line 1065 of file src/client/server.c.
References PACKET_TYPE_PONG, and threaded_send_packet().
| int threaded_send_stream_start_packet | ( | uint32_t | stream_type | ) |
#include <server.c>
Thread-safe stream start packet transmission.
| stream_type | Type of stream (audio/video) |
Definition at line 1078 of file src/client/server.c.
References HOST_TO_NET_U32, INVALID_SOCKET_VALUE, PACKET_TYPE_STREAM_START, server_connection_get_socket(), and threaded_send_packet().
Referenced by audio_start_thread(), capture_start_thread(), and protocol_start_connection().
| int threaded_send_terminal_size_with_auto_detect | ( | unsigned short | width, |
| unsigned short | height | ||
| ) |
#include <server.c>
Thread-safe terminal size packet transmission with auto-detection.
Sends terminal capabilities packet to the server including terminal size, color capabilities, and rendering preferences. Auto-detects terminal capabilities if not explicitly specified.
| width | Terminal width in characters |
| height | Terminal height in characters |
| width | Terminal width in characters |
| height | Terminal height in characters |
Definition at line 1104 of file src/client/server.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, 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, 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, server_connection_get_socket(), TERM_COLOR_NONE, terminal_capabilities_packet_t::term_type, terminal_capabilities_t::term_type, threaded_send_packet(), terminal_capabilities_packet_t::utf8_support, terminal_capabilities_t::utf8_support, and terminal_capabilities_packet_t::width.
Referenced by protocol_start_connection(), and server_connection_establish().
| crypto_handshake_context_t g_crypto_ctx = {0} |
#include <server.c>
Per-connection crypto handshake context.
Maintains the cryptographic state for the current connection, including key exchange state, encryption keys, and handshake progress.
Definition at line 198 of file src/client/server.c.
Referenced by client_crypto_handshake(), client_crypto_init(), crypto_client_cleanup(), crypto_client_decrypt_packet(), crypto_client_encrypt_packet(), crypto_client_get_context(), crypto_client_initiate_rekey(), crypto_client_is_ready(), crypto_client_process_rekey_request(), crypto_client_process_rekey_response(), crypto_client_send_rekey_complete(), crypto_client_send_rekey_response(), crypto_client_should_rekey(), and server_connection_close().