ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
socket.c File Reference

🌐 Common socket utility functions (cross-platform implementations) More...

Go to the source code of this file.

Functions

void socket_optimize_for_streaming (socket_t sock)
 Optimize socket for high-throughput video streaming.
 
int socket_set_timeout (socket_t sock, uint64_t timeout_ns)
 Set socket receive and send timeouts.
 

Detailed Description

🌐 Common socket utility functions (cross-platform implementations)

Definition in file socket.c.

Function Documentation

◆ socket_optimize_for_streaming()

void socket_optimize_for_streaming ( socket_t  sock)

Optimize socket for high-throughput video streaming.

Consolidates socket configuration for real-time video streaming:

  • Disables Nagle's algorithm (TCP_NODELAY)
  • Sets large send/receive buffers with automatic fallbacks
  • Enables TCP keepalive
  • Sets send/receive timeouts

This common implementation applies to both POSIX and Windows platforms.

Parameters
sockSocket to configure

Definition at line 37 of file socket.c.

37 {
38 // 1. Disable Nagle's algorithm - CRITICAL for real-time video
39 // TCP_NODELAY ensures data is sent immediately without waiting for ACKs
40 int nodelay = 1;
41 if (socket_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) != 0) {
42 log_warn("Failed to disable Nagle's algorithm (TCP_NODELAY) on socket");
43 }
44
45 // 2. Increase send buffer for video streaming (2MB with fallbacks)
46 // Large buffers reduce packet drops during bursty frame transmission
47 int send_buffer = MAX_FRAME_BUFFER_SIZE; // 2MB
48 if (socket_setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &send_buffer, sizeof(send_buffer)) != 0) {
49 send_buffer = 512 * 1024; // 512KB fallback
50 if (socket_setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &send_buffer, sizeof(send_buffer)) != 0) {
51 send_buffer = 128 * 1024; // 128KB fallback
52 socket_setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &send_buffer, sizeof(send_buffer));
53 }
54 }
55
56 // 3. Increase receive buffer (2MB with fallbacks)
57 // Allows buffering of multiple incoming frames before processing
58 int recv_buffer = MAX_FRAME_BUFFER_SIZE; // 2MB
59 if (socket_setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &recv_buffer, sizeof(recv_buffer)) != 0) {
60 recv_buffer = 512 * 1024; // 512KB fallback
61 if (socket_setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &recv_buffer, sizeof(recv_buffer)) != 0) {
62 recv_buffer = 128 * 1024; // 128KB fallback
63 socket_setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &recv_buffer, sizeof(recv_buffer));
64 }
65 }
66
67 // 4. Enable keepalive to detect dead connections
68 int keepalive = 1;
69 socket_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive));
70}

◆ socket_set_timeout()

int socket_set_timeout ( socket_t  sock,
uint64_t  timeout_ns 
)

Set socket receive and send timeouts.

Parameters
sockSocket to configure
timeout_msTimeout in milliseconds
Returns
0 on success, non-zero on error

Cross-platform implementation that sets both SO_RCVTIMEO and SO_SNDTIMEO. Platform-specific socket_setsockopt() handles the differences between Windows (DWORD milliseconds) and POSIX (struct timeval).

Definition at line 82 of file socket.c.

82 {
83#ifdef _WIN32
84 // Windows: SO_RCVTIMEO and SO_SNDTIMEO use DWORD (milliseconds)
85 DWORD timeout_val = (DWORD)time_ns_to_ms(timeout_ns);
86 if (socket_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout_val, sizeof(timeout_val)) != 0) {
87 return -1;
88 }
89 if (socket_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout_val, sizeof(timeout_val)) != 0) {
90 return -1;
91 }
92#else
93 // POSIX: SO_RCVTIMEO and SO_SNDTIMEO use struct timeval (seconds + microseconds)
94 struct timeval tv;
95 uint64_t us = time_ns_to_us(timeout_ns);
96 tv.tv_sec = (time_t)(us / US_PER_SEC_INT);
97 tv.tv_usec = (suseconds_t)(us % US_PER_SEC_INT);
98 if (socket_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) {
99 return -1;
100 }
101 if (socket_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) != 0) {
102 return -1;
103 }
104#endif
105 return 0;
106}

Referenced by discovery_session_process(), and nat_measure_bandwidth().