Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions components/rtps_embedded/src/communication/EsppTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,8 @@ EsppTransport::Channel *EsppTransport::createChannel(Ip4Port_t receivePort, bool
// large (fragmented) sample is not dropped before the reactor drains it.
// Best-effort: some stacks clamp SO_RCVBUF, so failure is ignored. Only
// compiled when fragmentation is enabled (never on the ESP32 default build).
{
int rcvbuf = 4 * 1024 * 1024; // request 4 MB (kernel may clamp)
::setsockopt(channel.socket->native_handle(), SOL_SOCKET, SO_RCVBUF,
reinterpret_cast<const char *>(&rcvbuf), sizeof(rcvbuf));
}
// request 4 MB (kernel may clamp)
(void)channel.socket->set_receive_buffer_size(4 * 1024 * 1024);
#endif

if (!allow_reuse && !channel.socket->disable_reuse()) {
Expand Down
2 changes: 1 addition & 1 deletion components/rtsp/include/rtsp_client.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "socket_msvc.hpp"
#include "socket_win32.hpp"

#include <atomic>
#include <chrono>
Expand Down
2 changes: 1 addition & 1 deletion components/rtsp/include/rtsp_server.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "socket_msvc.hpp"
#include "socket_win32.hpp"

#include <chrono>
#include <memory>
Expand Down
2 changes: 1 addition & 1 deletion components/rtsp/include/rtsp_session.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "socket_msvc.hpp"
#include "socket_win32.hpp"

#include <functional>
#include <memory>
Expand Down
77 changes: 74 additions & 3 deletions components/socket/include/socket.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#pragma once

#include "socket_msvc.hpp"
#include "socket_win32.hpp"

#ifdef _MSC_VER
typedef unsigned int sock_type_t;
#ifdef _WIN32
/* Windows SOCKET is UINT_PTR (pointer-sized on 64-bit); use the real type so
* handles aren't truncated. SOCKET comes from <winsock2.h>, included above via
* socket_win32.hpp. */
typedef SOCKET sock_type_t;
#else
/* Assume that any non-Windows platform uses POSIX-style sockets instead. */
#include <arpa/inet.h>
Expand All @@ -18,6 +21,7 @@ typedef int sock_type_t;
#include <functional>
#include <optional>
#include <string>
#include <type_traits>
#include <vector>

#include <math.h>
Expand Down Expand Up @@ -177,6 +181,64 @@ class Socket : public BaseComponent {
*/
bool set_receive_timeout(const std::chrono::duration<float> &timeout);

/**
* @brief Generic wrapper around setsockopt() so callers don't have to touch
* the raw native handle (or worry about the Windows const char* cast).
* @param level protocol level of the option (e.g. SOL_SOCKET, IPPROTO_IP).
* @param option_name option to set (e.g. SO_RCVBUF).
* @param value pointer to the option value.
* @param size size of the option value in bytes.
* @return true if setsockopt() succeeded, false otherwise (logs on failure).
*/
bool set_option(int level, int option_name, const void *value, size_t size);

/**
* @brief Convenience wrapper around set_option() for a trivially-copyable
* option value.
* @param level protocol level of the option (e.g. SOL_SOCKET).
* @param option_name option to set (e.g. SO_RCVBUF).
* @param value option value; its address and size are forwarded to
* setsockopt().
* @return true if setsockopt() succeeded, false otherwise (logs on failure).
*/
template <typename T> bool set_option(int level, int option_name, const T &value) {
static_assert(std::is_trivially_copyable_v<T>,
"set_option forwards the raw object bytes to setsockopt(); T must be "
"trivially copyable");
return set_option(level, option_name, &value, sizeof(value));
}
Comment thread
finger563 marked this conversation as resolved.

/**
* @brief Set the size of the kernel receive buffer (SO_RCVBUF).
* @note The kernel may clamp or double the requested value.
* @param bytes requested receive buffer size in bytes.
* @return true if SO_RCVBUF was successfully set.
*/
bool set_receive_buffer_size(size_t bytes);

/**
* @brief Set the size of the kernel send buffer (SO_SNDBUF).
* @note The kernel may clamp or double the requested value.
* @param bytes requested send buffer size in bytes.
* @return true if SO_SNDBUF was successfully set.
*/
bool set_send_buffer_size(size_t bytes);

/**
* @brief Set (or clear) SO_REUSEADDR on the socket.
* @note Unlike enable_reuse()/disable_reuse(), this only touches SO_REUSEADDR
* (not SO_REUSEPORT / SO_BROADCAST).
* @param enable true to allow address reuse, false to disallow it.
* @return true if SO_REUSEADDR was successfully set.
*/
bool set_reuse_address(bool enable);

/**
* @brief Get the size of the kernel receive buffer (SO_RCVBUF).
* @return the receive buffer size in bytes, or std::nullopt on failure.
*/
std::optional<size_t> get_receive_buffer_size();

/**
* @brief Allow others to use this address/port combination after we're done
* with it.
Expand Down Expand Up @@ -264,6 +326,15 @@ class Socket : public BaseComponent {
*/
void cleanup();

#ifdef _WIN32
/**
* @brief Initialize Winsock (WSAStartup) exactly once for the process.
* @note Thread-safe: uses std::call_once so concurrent Socket construction
* cannot race the one-time initialization.
*/
void initialize_winsock();
#endif

static constexpr int address_family_{AF_INET};
static constexpr int ip_protocol_{IPPROTO_IP};

Expand Down
2 changes: 1 addition & 1 deletion components/socket/include/socket_reactor.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "socket_msvc.hpp"
#include "socket_win32.hpp"

#include <atomic>
#include <chrono>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifdef _MSC_VER
#ifdef _WIN32
Comment thread
finger563 marked this conversation as resolved.
extern "C" {
// if we don't define NOMINMAX, windows.h will define min and max as macros
// which will conflict with std::min and std::max
Expand Down
6 changes: 3 additions & 3 deletions components/socket/include/tcp_socket.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include "socket_msvc.hpp"
#include "socket_win32.hpp"

#ifndef _MSC_VER
#ifndef _WIN32
#include <netinet/tcp.h>
#endif // _MSC_VER
#endif // _WIN32

#include <atomic>
#include <optional>
Expand Down
2 changes: 1 addition & 1 deletion components/socket/include/udp_socket.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "socket_msvc.hpp"
#include "socket_win32.hpp"

#include <optional>
#include <span>
Expand Down
Loading