|
ascii-chat 0.8.38
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. | |
| asciichat_error_t | threaded_send_stream_start_packet (uint32_t stream_type) |
| Thread-safe stream start packet transmission. | |
| asciichat_error_t | 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. | |
| void | server_connection_set_transport (struct acip_transport *transport) |
| Set ACIP transport instance from connection fallback. | |
| 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 949 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 854 of file src/client/server.c.
References acip_transport_destroy(), close_socket, crypto_handshake_destroy(), g_crypto_ctx, and log_set_terminal_output().
Referenced by 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 320 of file src/client/server.c.
References acip_tcp_transport_create(), acip_transport_destroy(), acip_websocket_client_transport_create(), client_crypto_handshake(), client_crypto_init(), close_socket, connect_with_timeout(), CONNECTION_ERROR_AUTH_FAILED, crypto_client_get_context(), crypto_client_is_ready(), format_ip_address(), is_localhost_ipv4(), is_localhost_ipv6(), log_set_terminal_output(), network_error_string(), platform_sleep_us(), should_exit(), socket_configure_buffers(), threaded_send_client_join_packet(), threaded_send_terminal_size_with_auto_detect(), url_is_websocket(), url_parse(), and url_parts_destroy().
| uint32_t server_connection_get_client_id | ( | ) |
#include <server.c>
Get client ID assigned by server.
Definition at line 808 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 822 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 745 of file src/client/server.c.
Referenced by crypto_client_initiate_rekey(), crypto_client_send_rekey_complete(), and crypto_client_send_rekey_response().
| struct acip_transport * server_connection_get_transport | ( | void | ) |
#include <server.c>
Get ACIP transport instance.
Definition at line 756 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 285 of file src/client/server.c.
References mutex_init().
| bool server_connection_is_active | ( | ) |
#include <server.c>
Check if server connection is currently active.
Definition at line 732 of file src/client/server.c.
| bool server_connection_is_lost | ( | ) |
#include <server.c>
Check if connection loss has been detected.
Check if connection loss was detected.
Definition at line 937 of file src/client/server.c.
Referenced by 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 921 of file src/client/server.c.
References display_full_reset().
Referenced by 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 836 of file src/client/server.c.
Referenced by connection_attempt_tcp().
| 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() |
Definition at line 770 of file src/client/server.c.
References acip_transport_destroy().
| void server_connection_set_transport | ( | struct acip_transport * | transport | ) |
#include <server.h>
Set ACIP transport instance from connection 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.
| 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 891 of file src/client/server.c.
Referenced by client_main(), and 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 1018 of file src/client/server.c.
References acip_send_audio_batch(), 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 1058 of file src/client/server.c.
References buffer_pool_alloc(), buffer_pool_free(), packet_send_via_transport(), and server_connection_lost().
| 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 1122 of file src/client/server.c.
References acip_send_audio_opus_batch(), and server_connection_lost().
| 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 1306 of file src/client/server.c.
References crypto_client_get_context(), crypto_client_is_ready(), log_network_message(), 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 978 of file src/client/server.c.
References packet_send_via_transport(), and server_connection_lost().
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 1158 of file src/client/server.c.
References threaded_send_packet().
| int threaded_send_pong_packet | ( | void | ) |
#include <server.c>
Thread-safe pong packet transmission.
Definition at line 1170 of file src/client/server.c.
References threaded_send_packet().
| asciichat_error_t 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 1183 of file src/client/server.c.
References threaded_send_packet().
Referenced by audio_start_thread(), capture_start_thread(), and protocol_start_connection().
| asciichat_error_t 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 1206 of file src/client/server.c.
References auto_height, auto_width, detect_terminal_capabilities(), options_get(), terminal_is_interactive(), terminal_is_stdin_tty(), terminal_is_stdout_tty(), and threaded_send_packet().
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.
Global crypto handshake context for this client connection.
Maintains the cryptographic state for the current connection, including key exchange state, encryption keys, and handshake progress.
Definition at line 196 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().