Skip to content

Commit

Permalink
feat(udp_socket): allow user to set internal socket buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
mojomex committed Aug 27, 2024
1 parent abf8aa8 commit f86566a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
17 changes: 14 additions & 3 deletions udp_driver/include/boost_udp_driver/udp_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
#ifndef UDP_DRIVER__UDP_SOCKET_HPP_
#define UDP_DRIVER__UDP_SOCKET_HPP_

#include "boost_io_context/io_context.hpp"

#include <array>
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>

#include "boost_io_context/io_context.hpp"
#include "boost_msg_converters/converters.hpp"

namespace drivers
{
namespace udp_driver
Expand Down Expand Up @@ -67,6 +68,16 @@ class UdpSocket
void bind();
void setMulticast(bool value);

/**
* @brief Set the socket's internal receive buffer size to `n_bytes`. See `SO_RCVBUF` in `man 7
* socket` for more information.
*
* @param n_bytes The number of bytes to allocate.
* @return true If the buffer has been resizes successfully.
* @return false If there was an error, such as the `net.core.rmem_max` value being exceeded.
*/
bool setKernelBufferSize(int32_t n_bytes);

/*
* Blocking Send Operation
*/
Expand Down
14 changes: 14 additions & 0 deletions udp_driver/src/udp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <vector>

#include "boost/asio.hpp"
#include <boost/system/system_error.hpp>
#include "rclcpp/logging.hpp"

namespace drivers
Expand Down Expand Up @@ -263,5 +264,18 @@ void UdpSocket::setMulticast(bool value)
m_use_multicast = value;
}

bool UdpSocket::setKernelBufferSize(int32_t n_bytes) {
boost::asio::socket_base::receive_buffer_size buffer_size_option{ n_bytes };

try {
m_udp_socket.set_option(buffer_size_option);
} catch (boost::system::system_error & error) {
RCLCPP_ERROR_STREAM(rclcpp::get_logger("UdpSocket::setkernelBufferSize"), error.what());
return false;
}

return true;
}

} // namespace udp_driver
} // namespace drivers

0 comments on commit f86566a

Please sign in to comment.