feat(socket): generalize Windows guards + add socket-option API - #714
feat(socket): generalize Windows guards + add socket-option API#714finger563 wants to merge 2 commits into
Conversation
Replace all MSVC-specific preprocessor guards (_MSC_VER) in the socket component with the Windows-platform guard _WIN32, so MinGW / clang-cl Windows builds are covered too. Every guard was Windows-platform intent (Winsock2, closesocket vs close, WSAStartup, INVALID_SOCKET, ioctlsocket, SO_* char* casts) - none were MSVC-compiler-only pragmas. Add socket-option-setting support to the base espp::Socket so callers no longer reach for the raw native handle: - set_option(int level, int option_name, const void *value, size_t size) - template <typename T> set_option(int level, int option_name, const T&) - set_receive_buffer_size(size_t) / set_send_buffer_size(size_t) - set_reuse_address(bool) - get_receive_buffer_size() -> std::optional<size_t> The Windows const char* setsockopt cast is handled under _WIN32. Migrate the RTPS EsppTransport SO_RCVBUF setup to use Socket::set_receive_buffer_size() instead of a raw setsockopt call. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
This PR broadens Windows platform support in the socket layer (MSVC-only → Windows-wide guards) and adds a safer, higher-level socket-option API to avoid direct native-handle setsockopt() usage in consumers.
Changes:
- Replaced
_MSC_VERguards with_WIN32across socket sources/headers to cover MinGW / clang-cl Windows builds. - Added
Socket::set_option(...)plus convenience helpers for common options (SO_RCVBUF/SO_SNDBUF/SO_REUSEADDR) and aget_receive_buffer_size()accessor. - Migrated RTPS transport receive-buffer tuning to the new
SocketAPI.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| components/socket/src/udp_socket.cpp | Windows guard generalized to _WIN32 for error handling paths. |
| components/socket/src/socket_reactor.cpp | Windows guard generalized to _WIN32 for close/nonblocking/select behavior. |
| components/socket/src/socket.cpp | Added socket-option API and generalized Winsock initialization/utility guards. |
| components/socket/include/tcp_socket.hpp | Generalized non-Windows include guard to _WIN32. |
| components/socket/include/socket_msvc.hpp | Header now enabled for all _WIN32 builds. |
| components/socket/include/socket.hpp | Introduced new Socket option APIs; generalized Windows typedef guard. |
| components/rtps_embedded/src/communication/EsppTransport.cpp | Replaced raw setsockopt call with Socket::set_receive_buffer_size. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
✅Static analysis result - no issues found! ✅ |
…ards, WSAStartup race, header rename - socket.hpp: sock_type_t is now `typedef SOCKET sock_type_t` under _WIN32 (was `unsigned int`, which truncates the pointer-sized SOCKET/UINT_PTR on 64-bit Windows). INVALID_SOCKET sentinel usage lines up unchanged. - socket.hpp: template set_option now static_asserts std::is_trivially_copyable_v<T> since it forwards raw object bytes to setsockopt(); added <type_traits>. - socket.cpp: guard size_t->int narrowing in set_option (reject size > INT_MAX) and in set_receive_buffer_size / set_send_buffer_size (reject bytes > INT_MAX), logging + returning false; added <climits>. - socket.cpp: replace the non-atomic _socket_initialized check-then-act WSAStartup init with a std::once_flag + std::call_once helper (initialize_winsock); added <mutex>. Fixes the race when Sockets are constructed concurrently. - rename socket_msvc.hpp -> socket_win32.hpp (guard is _WIN32, applies to any Windows toolchain) and update all #include references (socket, rtsp, lib umbrella). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
What & why
Two improvements to the
socketcomponent:(a)
_MSC_VER→_WIN32guards. The Windows-specific code (Winsock2,closesocketvsclose,WSAStartup,INVALID_SOCKET,ioctlsocket,SO_*/char*casts,WSAGetLastError) was guarded on_MSC_VER, which only covers the MSVC compiler. Switched every such guard to_WIN32so MinGW / clang-cl Windows builds are covered too. Each guard was verified to be Windows-platform intent — none were MSVC-compiler-only pragmas.Files:
socket.hpp/socket.cpp,tcp_socket.hpp,socket_reactor.cpp,udp_socket.cpp,socket_msvc.hpp.(b) Socket-option setters on the base
Socket. Previously, settingSO_RCVBUF/SO_SNDBUFmeant callingsetsockopt()on the raw native handle (as our RTPS transport did). Added toespp::Socket:set_optionhandles the Windowsconst char*setsockoptcast and logs on failure. Then migrated the RTPS transport's manual 4 MBSO_RCVBUFsetsockopt(EsppTransport.cpp) tosocket->set_receive_buffer_size(...)— the only such call, and theSocketobject was in scope.Verification
Host lib builds clean (
cmake -S lib ... --target install, exit 0) — this compiles both the socket sources and the RTPS transport (fragmentation on in the host profile, so the migrated path is exercised). The socket example is ESP-IDF-only, so the host lib is the compile check.🤖 Generated with Claude Code