Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(udp_socket): allow user to set internal socket buffer size #1

Merged
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
18 changes: 14 additions & 4 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,13 @@
#ifndef UDP_DRIVER__UDP_SOCKET_HPP_
#define UDP_DRIVER__UDP_SOCKET_HPP_

#include <array>
#include "boost_io_context/io_context.hpp"

#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 +67,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 resized 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
23 changes: 19 additions & 4 deletions udp_driver/src/udp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@

#include "boost_udp_driver/udp_socket.hpp"

#include <rclcpp/logging.hpp>

#include <boost/asio.hpp>
#include <boost/system/system_error.hpp>

#include <iostream>
#include <utility>
#include <string>
#include <system_error>
#include <utility>
#include <vector>

#include "boost/asio.hpp"
#include "rclcpp/logging.hpp"

namespace drivers
{
namespace udp_driver
Expand Down Expand Up @@ -263,5 +265,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