Skip to content

Commit

Permalink
Allow configuration of UDP receive buffer size (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Moshe Cikk <[email protected]>
  • Loading branch information
moshecikk and Moshe Cikk authored Sep 14, 2023
1 parent 5e8edf1 commit 5b007e2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions udp_driver/include/boost_udp_driver/udp_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class UdpDriver
const std::string & remote_ip, uint16_t remote_port,
const std::string & host_ip, uint16_t host_port);
void init_receiver(const std::string & ip, uint16_t port);
void init_receiver(const std::string & ip, uint16_t port, size_t buffer_size);


std::shared_ptr<UdpSocket> sender() const;
std::shared_ptr<UdpSocket> receiver() const;
Expand Down
12 changes: 11 additions & 1 deletion udp_driver/include/boost_udp_driver/udp_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,18 @@ class UdpSocket
const drivers::common::IoContext & ctx,
const std::string & remote_ip, uint16_t remote_port,
const std::string & host_ip, uint16_t host_port);
UdpSocket(
const drivers::common::IoContext & ctx,
const std::string & remote_ip, uint16_t remote_port,
const std::string & host_ip, uint16_t host_port,
size_t recv_buffer_size);
UdpSocket(
const drivers::common::IoContext & ctx,
const std::string & ip, uint16_t port);
UdpSocket(
const drivers::common::IoContext & ctx,
const std::string & ip, uint16_t port,
size_t recv_buffer_size);
~UdpSocket();

UdpSocket(const UdpSocket &) = delete;
Expand Down Expand Up @@ -91,7 +100,8 @@ class UdpSocket
boost::asio::ip::udp::endpoint m_remote_endpoint;
boost::asio::ip::udp::endpoint m_host_endpoint;
Functor m_func;
static const size_t m_recv_buffer_size{2048};
static constexpr size_t m_default_recv_buffer_size{2048};
size_t m_recv_buffer_size;
std::vector<uint8_t> m_recv_buffer;
};

Expand Down
6 changes: 6 additions & 0 deletions udp_driver/src/udp_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ void UdpDriver::init_receiver(const std::string & ip, uint16_t port)
m_receiver.reset(new UdpSocket(m_ctx, ip, port));
}

void UdpDriver::init_receiver(const std::string & ip, uint16_t port, size_t buffer_size)
{
m_receiver.reset(new UdpSocket(m_ctx, ip, port, buffer_size));
}


std::shared_ptr<UdpSocket> UdpDriver::sender() const
{
return m_sender;
Expand Down
25 changes: 22 additions & 3 deletions udp_driver/src/udp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,40 @@ UdpSocket::UdpSocket(
const std::string & remote_ip,
const uint16_t remote_port,
const std::string & host_ip,
const uint16_t host_port)
const uint16_t host_port,
const size_t recv_buffer_size)
: m_ctx(ctx),
m_udp_socket(ctx.ios()),
m_remote_endpoint(boost::asio::ip::address::from_string(remote_ip), remote_port),
m_host_endpoint(boost::asio::ip::address::from_string(host_ip), host_port)
m_host_endpoint(boost::asio::ip::address::from_string(host_ip), host_port),
m_recv_buffer_size(recv_buffer_size)
{
m_remote_endpoint = remote_ip.empty() ?
boost::asio::ip::udp::endpoint{boost::asio::ip::udp::v4(), remote_port} :
boost::asio::ip::udp::endpoint{boost::asio::ip::address::from_string(remote_ip), remote_port};
m_host_endpoint = host_ip.empty() ?
boost::asio::ip::udp::endpoint{boost::asio::ip::udp::v4(), host_port} :
boost::asio::ip::udp::endpoint{boost::asio::ip::address::from_string(host_ip), host_port};
m_recv_buffer.resize(m_recv_buffer_size);
m_recv_buffer.resize(recv_buffer_size);
}

UdpSocket::UdpSocket(
const drivers::common::IoContext & ctx,
const std::string & remote_ip,
const uint16_t remote_port,
const std::string & host_ip,
const uint16_t host_port)
: UdpSocket(ctx, remote_ip, remote_port, host_ip, host_port, m_default_recv_buffer_size)
{}

UdpSocket::UdpSocket(
const drivers::common::IoContext & ctx,
const std::string & ip,
const uint16_t port,
const size_t recv_buffer_size)
: UdpSocket{ctx, ip, port, ip, port, recv_buffer_size}
{}

UdpSocket::UdpSocket(
const drivers::common::IoContext & ctx,
const std::string & ip,
Expand Down

0 comments on commit 5b007e2

Please sign in to comment.