Skip to content

feat(socket): generalize Windows guards + add socket-option API - #714

Open
finger563 wants to merge 2 commits into
mainfrom
feat/socket-options
Open

feat(socket): generalize Windows guards + add socket-option API#714
finger563 wants to merge 2 commits into
mainfrom
feat/socket-options

Conversation

@finger563

Copy link
Copy Markdown
Contributor

What & why

Two improvements to the socket component:

(a) _MSC_VER_WIN32 guards. The Windows-specific code (Winsock2, closesocket vs close, WSAStartup, INVALID_SOCKET, ioctlsocket, SO_*/char* casts, WSAGetLastError) was guarded on _MSC_VER, which only covers the MSVC compiler. Switched every such guard to _WIN32 so 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, setting SO_RCVBUF/SO_SNDBUF meant calling setsockopt() on the raw native handle (as our RTPS transport did). Added to espp::Socket:

bool set_option(int level, int option_name, const void *value, size_t size);
template <typename T> bool set_option(int level, int option_name, const T &value);
bool set_receive_buffer_size(size_t bytes);      // SO_RCVBUF
bool set_send_buffer_size(size_t bytes);          // SO_SNDBUF
bool set_reuse_address(bool enable);              // SO_REUSEADDR
std::optional<size_t> get_receive_buffer_size();  // getsockopt (bonus)

set_option handles the Windows const char* setsockopt cast and logs on failure. Then migrated the RTPS transport's manual 4 MB SO_RCVBUF setsockopt (EsppTransport.cpp) to socket->set_receive_buffer_size(...) — the only such call, and the Socket object 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

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>
Copilot AI lite review requested due to automatic review settings August 15, 2026 01:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_VER guards with _WIN32 across 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 a get_receive_buffer_size() accessor.
  • Migrated RTPS transport receive-buffer tuning to the new Socket API.

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.

Comment thread components/socket/include/socket.hpp Outdated
Comment thread components/socket/src/socket.cpp
Comment thread components/socket/src/socket.cpp
Comment thread components/socket/include/socket.hpp
Comment thread components/socket/src/socket.cpp Outdated
Comment thread components/socket/include/socket_win32.hpp
@github-actions

Copy link
Copy Markdown

✅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>
@finger563
finger563 requested a balanced review from Copilot August 15, 2026 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants