From e5838edf9f59a47f2bed97122addb6b8fcec07f1 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 7 Aug 2025 21:16:15 +0200 Subject: [PATCH] MacOS: fix(network): fixes ArticBase UDP buffer handling on macOS (#1263) * MacOS: fix(network): fixes ArticBase UDP buffer handling on macOS - Set minimum 8KB UDP receive buffer size on macOS for reliable operation - Add platform-specific buffer size handling with conditional compilation - Enhance error logging with errno details and buffer size verification - Maintain backward compatibility with existing buffer sizes on other platforms Fixes ArticBase controller input reliability issues on macOS by ensuring adequate UDP buffer capacity for real-time controller data transmission. * Remove redundant log --------- Co-authored-by: Alex Molina Co-authored-by: PabloMK7 --- src/network/artic_base/artic_base_client.cpp | 29 +++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/network/artic_base/artic_base_client.cpp b/src/network/artic_base/artic_base_client.cpp index 861e23477..b38416cc4 100644 --- a/src/network/artic_base/artic_base_client.cpp +++ b/src/network/artic_base/artic_base_client.cpp @@ -6,6 +6,7 @@ #include "common/assert.h" #include "common/logging/log.h" +#include "algorithm" #include "chrono" #include "limits.h" #include "memory" @@ -143,15 +144,35 @@ void Client::UDPStream::Handle() { } // Limit receive buffer so that packets don't get qeued and are dropped instead. - int buffer_size_int = static_cast(buffer_size); - if (::setsockopt(main_socket, SOL_SOCKET, SO_RCVBUF, reinterpret_cast(&buffer_size_int), - sizeof(buffer_size_int)) || + // macOS requires larger UDP buffer sizes for reliable operation +#ifdef __APPLE__ + const int min_macos_buffer_size = 8192; // 8KB minimum for macOS + int effective_buffer_size = std::max(static_cast(buffer_size), min_macos_buffer_size); +#else + int effective_buffer_size = static_cast(buffer_size); +#endif + + if (::setsockopt(main_socket, SOL_SOCKET, SO_RCVBUF, + reinterpret_cast(&effective_buffer_size), + sizeof(effective_buffer_size)) < 0 || !thread_run) { + LOG_ERROR(Network, "Cannot change receive buffer size: {} (errno: {})", strerror(GET_ERRNO), + GET_ERRNO); closesocket(main_socket); - LOG_ERROR(Network, "Cannot change receive buffer size"); return; } + // Verify the buffer size was actually set + socklen_t actual_size_len = sizeof(int); + int actual_buffer_size; + if (::getsockopt(main_socket, SOL_SOCKET, SO_RCVBUF, + reinterpret_cast(&actual_buffer_size), &actual_size_len) == 0) { + LOG_INFO(Network, "UDP buffer size set to: {} (requested: {})", actual_buffer_size, + effective_buffer_size); + } else { + LOG_WARNING(Network, "Could not verify UDP buffer size setting"); + } + // Send data to server so that it knows client address. char zero = '\0'; int send_res =