ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
socket.c
Go to the documentation of this file.
1
8#include <ascii-chat/platform/socket.h>
9#include <ascii-chat/common.h>
10#include <ascii-chat/util/time.h>
11
12// Platform-specific TCP header (included by socket.h above)
13// Windows: ws2tcpip.h (included by winsock2.h)
14// POSIX: netinet/tcp.h (need to include explicitly)
15#ifndef _WIN32
16#include <netinet/tcp.h>
17#endif
18
19// ============================================================================
20// Common Socket Functions
21// ============================================================================
22
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}
71
82int socket_set_timeout(socket_t sock, uint64_t timeout_ns) {
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}
int socket_t
void socket_optimize_for_streaming(socket_t sock)
Optimize socket for high-throughput video streaming.
Definition socket.c:37
int socket_set_timeout(socket_t sock, uint64_t timeout_ns)
Set socket receive and send timeouts.
Definition socket.c:82