From ed848e9de267fcfdbe1cb7294b2e408d85a33575 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Wed, 8 May 2024 16:27:44 +0200 Subject: [PATCH] Refs #20972. Applying changes on CreateInputChannelResource. Signed-off-by: Miguel Company --- .../rtps/transport/UDPTransportInterface.cpp | 55 +++++++++++++++---- src/cpp/rtps/transport/UDPv4Transport.cpp | 4 -- src/cpp/rtps/transport/UDPv6Transport.cpp | 4 -- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/cpp/rtps/transport/UDPTransportInterface.cpp b/src/cpp/rtps/transport/UDPTransportInterface.cpp index ecc5885e578..0151b2976aa 100644 --- a/src/cpp/rtps/transport/UDPTransportInterface.cpp +++ b/src/cpp/rtps/transport/UDPTransportInterface.cpp @@ -146,6 +146,35 @@ static uint32_t try_setting_send_buffer_size( return minimum_buffer_value; } +/** + * @brief Set the receive buffer size of the socket to the highest possible value the system allows. + * + * @param socket The socket on which to set the receive buffer size. + * @param initial_buffer_value The initial value to try to set. + * @param minimum_buffer_value The minimum value to set. + * + * @return The final value set. + */ +static uint32_t try_setting_receive_buffer_size( + asio::ip::udp::socket& socket, + uint32_t initial_buffer_value, + uint32_t minimum_buffer_value) +{ + // Try to set the highest possible value the system allows + for (auto recv_size = initial_buffer_value; recv_size >= minimum_buffer_value; recv_size /= 2) + { + asio::error_code ec; + socket_base::receive_buffer_size option(static_cast(recv_size)); + socket.set_option(option, ec); + if (!ec) + { + return recv_size; + } + } + + return minimum_buffer_value; +} + void UDPTransportInterface::configure_send_buffer_size() { asio::error_code ec; @@ -221,23 +250,14 @@ void UDPTransportInterface::configure_receive_buffer_size() if (receive_buffer_size < minimum_socket_buffer) { receive_buffer_size = minimum_socket_buffer; - set_receive_buffer_size(receive_buffer_size); } // Try to set the highest possible value the system allows - for (; receive_buffer_size >= minimum_socket_buffer; receive_buffer_size /= 2) - { - socket_base::receive_buffer_size option(static_cast(receive_buffer_size)); - socket.set_option(option, ec); - if (!ec) - { - set_receive_buffer_size(receive_buffer_size); - break; - } - } + receive_buffer_size = try_setting_receive_buffer_size(socket, receive_buffer_size, minimum_socket_buffer); + set_receive_buffer_size(receive_buffer_size); // Keep final configuration value - mReceiveBufferSize = configuration()->receiveBufferSize; + mReceiveBufferSize = receive_buffer_size; // Inform the user if the desired value could not be set if (initial_value != 0 && mReceiveBufferSize != initial_value) @@ -334,6 +354,17 @@ UDPChannelResource* UDPTransportInterface::CreateInputChannelResource( { eProsimaUDPSocket unicastSocket = OpenAndBindInputSocket(sInterface, IPLocator::getPhysicalPort(locator), is_multicast); + if (mReceiveBufferSize != 0) + { + uint32_t configured_value; + configured_value = try_setting_receive_buffer_size(unicastSocket, mReceiveBufferSize, maxMsgSize); + if (configured_value != mReceiveBufferSize) + { + EPROSIMA_LOG_WARNING(RTPS_MSG_OUT, "UDPTransport receiveBufferSize could not be set to the desired value. " + << "Using " << configured_value << " instead of " << mReceiveBufferSize); + } + } + UDPChannelResource* p_channel_resource = new UDPChannelResource(this, unicastSocket, maxMsgSize, locator, sInterface, receiver, configuration()->get_thread_config_for_port(locator.port)); return p_channel_resource; diff --git a/src/cpp/rtps/transport/UDPv4Transport.cpp b/src/cpp/rtps/transport/UDPv4Transport.cpp index a941072fe34..7951d2f7607 100644 --- a/src/cpp/rtps/transport/UDPv4Transport.cpp +++ b/src/cpp/rtps/transport/UDPv4Transport.cpp @@ -393,10 +393,6 @@ eProsimaUDPSocket UDPv4Transport::OpenAndBindInputSocket( { eProsimaUDPSocket socket = createUDPSocket(io_service_); getSocketPtr(socket)->open(generate_protocol()); - if (mReceiveBufferSize != 0) - { - getSocketPtr(socket)->set_option(socket_base::receive_buffer_size(mReceiveBufferSize)); - } if (is_multicast) { diff --git a/src/cpp/rtps/transport/UDPv6Transport.cpp b/src/cpp/rtps/transport/UDPv6Transport.cpp index 81ac92b5bac..3e40aa9001c 100644 --- a/src/cpp/rtps/transport/UDPv6Transport.cpp +++ b/src/cpp/rtps/transport/UDPv6Transport.cpp @@ -394,10 +394,6 @@ eProsimaUDPSocket UDPv6Transport::OpenAndBindInputSocket( { eProsimaUDPSocket socket = createUDPSocket(io_service_); getSocketPtr(socket)->open(generate_protocol()); - if (mReceiveBufferSize != 0) - { - getSocketPtr(socket)->set_option(socket_base::receive_buffer_size(mReceiveBufferSize)); - } if (is_multicast) {