ascii-chat 0.6.0
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 "socket.h"
9#include "../common.h"
10
11// Platform-specific TCP header (included by socket.h above)
12// Windows: ws2tcpip.h (included by winsock2.h)
13// POSIX: netinet/tcp.h (need to include explicitly)
14#ifndef _WIN32
15#include <netinet/tcp.h>
16#endif
17
18// ============================================================================
19// Common Socket Functions
20// ============================================================================
21
37 // 1. Disable Nagle's algorithm - CRITICAL for real-time video
38 // TCP_NODELAY ensures data is sent immediately without waiting for ACKs
39 int nodelay = 1;
40 if (socket_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) != 0) {
41 log_warn("Failed to disable Nagle's algorithm (TCP_NODELAY) on socket");
42 }
43
44 // 2. Increase send buffer for video streaming (2MB with fallbacks)
45 // Large buffers reduce packet drops during bursty frame transmission
46 int send_buffer = 2 * 1024 * 1024; // 2MB
47 if (socket_setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &send_buffer, sizeof(send_buffer)) != 0) {
48 send_buffer = 512 * 1024; // 512KB fallback
49 if (socket_setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &send_buffer, sizeof(send_buffer)) != 0) {
50 send_buffer = 128 * 1024; // 128KB fallback
51 socket_setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &send_buffer, sizeof(send_buffer));
52 }
53 }
54
55 // 3. Increase receive buffer (2MB with fallbacks)
56 // Allows buffering of multiple incoming frames before processing
57 int recv_buffer = 2 * 1024 * 1024; // 2MB
58 if (socket_setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &recv_buffer, sizeof(recv_buffer)) != 0) {
59 recv_buffer = 512 * 1024; // 512KB fallback
60 if (socket_setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &recv_buffer, sizeof(recv_buffer)) != 0) {
61 recv_buffer = 128 * 1024; // 128KB fallback
62 socket_setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &recv_buffer, sizeof(recv_buffer));
63 }
64 }
65
66 // 4. Enable keepalive to detect dead connections
67 int keepalive = 1;
68 socket_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive));
69}
70
81int socket_set_timeout(socket_t sock, uint32_t timeout_ms) {
82#ifdef _WIN32
83 // Windows: SO_RCVTIMEO and SO_SNDTIMEO use DWORD (milliseconds)
84 DWORD timeout_val = (DWORD)timeout_ms;
85 if (socket_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout_val, sizeof(timeout_val)) != 0) {
86 return -1;
87 }
88 if (socket_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout_val, sizeof(timeout_val)) != 0) {
89 return -1;
90 }
91#else
92 // POSIX: SO_RCVTIMEO and SO_SNDTIMEO use struct timeval (seconds + microseconds)
93 struct timeval tv;
94 tv.tv_sec = timeout_ms / 1000;
95 tv.tv_usec = (timeout_ms % 1000) * 1000;
96 if (socket_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) {
97 return -1;
98 }
99 if (socket_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) != 0) {
100 return -1;
101 }
102#endif
103 return 0;
104}
unsigned int uint32_t
Definition common.h:58
#define log_warn(...)
Log a WARN message.
void socket_optimize_for_streaming(socket_t sock)
Optimize socket for high-throughput video streaming.
Definition socket.c:36
int socket_setsockopt(socket_t sock, int level, int optname, const void *optval, socklen_t optlen)
Set socket option.
int socket_t
Socket handle type (POSIX: int)
Definition socket.h:50
int socket_set_timeout(socket_t sock, uint32_t timeout_ms)
Set socket receive and send timeouts.
Definition socket.c:81
Cross-platform socket interface for ascii-chat.