Skip to content

Commit

Permalink
Started implementing multicast mode
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianReimold committed Apr 19, 2024
1 parent 7d34fef commit 8e658ac
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 13 deletions.
25 changes: 25 additions & 0 deletions samples/ecaludp_perftool/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ecaludp-perftool

ecaludp_perftool is a simple tool for sending and receiving data using the eCAL protocol.

```
Usage:
ecaludp_perftool <IMPLEMENTATION> [PARAMETERS]
With IMPLEMENTATION one of:
send Asio-based sender using send_to in a while-loop
sendasync Asio-based sender using async_send_to
receive Asio-based receiver using receive_from in a while-loop
receiveasync Asio-based receiver using async_receive_from
receivenpcap Npcap-based receiver using receive_from in a while-loop
receivenpcapasync Npcap-based receiver using async_receive_from
Options:
-h, --help Show this help message and exit
-i, --ip <IP> IP address to send to / receive from. Default to 127.0.0.1
-p, --port <PORT> Port to send to / receive from. Default to 14000
-s, --size <SIZE> Message size to send. Default to 0 (-> empty messages)
-m, --max-udp-datagram-size <SIZE> Maximum UDP datagram size
-b, --buffer-size <SIZE> Buffer size for sending & receiving messages
```
1 change: 1 addition & 0 deletions samples/ecaludp_perftool/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void printUsage(const std::string& arg0)
{
std::cout << "Usage:\n";
std::cout << " " << arg0 << " <IMPLEMENTATION> [PARAMETERS]\n";
std::cout << '\n';
std::cout << "With IMPLEMENTATION one of:\n";
std::cout << " send Asio-based sender using send_to in a while-loop\n";
std::cout << " sendasync Asio-based sender using async_send_to\n";
Expand Down
3 changes: 1 addition & 2 deletions samples/ecaludp_perftool/src/receiver_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ void ReceiverSync::receive_loop()
exit(1);
}

asio::ip::udp::endpoint destination(asio::ip::address::from_string(parameters_.ip), parameters_.port);
asio::ip::udp::endpoint destination;

while (true)
{
{

asio::error_code ec;
auto payload_buffer = receive_socket->receive_from(destination, 0, ec);

Expand Down
64 changes: 53 additions & 11 deletions samples/ecaludp_perftool/src/socket_builder_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,37 +98,79 @@ namespace SocketBuilderAsio
}
}

// Set reuse address
{
const asio::ip::udp::socket::reuse_address option(true);

asio::error_code ec;
socket->bind(destination, ec); // NOLINT(bugprone-unused-return-value) The function also returns the error_code, but we already got it via the parameter
socket->set_option(option, ec); // NOLINT(bugprone-unused-return-value) The function also returns the error_code, but we already got it via the parameter
if (ec)
{
throw std::runtime_error("Failed to bind socket: " + ec.message());
throw std::runtime_error("Failed to set reuse address: " + ec.message());
}
}

// Set receive buffer size
if (parameters.buffer_size > 0)
if (destination.address().is_multicast())
{
{
// Set multicast loopback
asio::error_code ec;
const asio::ip::multicast::enable_loopback option(true);
socket->set_option(option, ec); // NOLINT(bugprone-unused-return-value) The function also returns the error_code, but we already got it via the parameter
if (ec)
{
throw std::runtime_error("Failed to set multicast loopback: " + ec.message());
}
}
{
// "Bind" multicast address
const asio::ip::udp::endpoint bind_endpoint;
if (ip_address.is_v4())
{
asio::ip::udp::endpoint bind_endpoint(asio::ip::address_v4(), destination.port());
}
else
{
asio::ip::udp::endpoint bind_endpoint(asio::ip::address_v6(), destination.port());
}

asio::error_code ec;
socket->bind(bind_endpoint, ec); // NOLINT(bugprone-unused-return-value) The function also returns the error_code, but we already got it via the parameter
if (ec)
{
throw std::runtime_error("Failed to bind socket: " + ec.message());
}
}
{
// Join multicast group
asio::error_code ec;
socket->set_option(asio::ip::multicast::join_group(destination.address()), ec); // NOLINT(bugprone-unused-return-value) The function also returns the error_code, but we already got it via the parameter
if (ec)
{
throw std::runtime_error("Failed to join multicast group: " + ec.message());
}
}
}
else
{
const asio::socket_base::receive_buffer_size option(parameters.buffer_size);

asio::error_code ec;
socket->set_option(option, ec); // NOLINT(bugprone-unused-return-value) The function also returns the error_code, but we already got it via the parameter
socket->bind(destination, ec); // NOLINT(bugprone-unused-return-value) The function also returns the error_code, but we already got it via the parameter
if (ec)
{
throw std::runtime_error("Failed to set receive buffer size: " + ec.message());
throw std::runtime_error("Failed to bind socket: " + ec.message());
}
}

// Set reuse address
// Set receive buffer size
if (parameters.buffer_size > 0)
{
const asio::socket_base::reuse_address option(true);
const asio::socket_base::receive_buffer_size option(parameters.buffer_size);

asio::error_code ec;
socket->set_option(option, ec); // NOLINT(bugprone-unused-return-value) The function also returns the error_code, but we already got it via the parameter
if (ec)
{
throw std::runtime_error("Failed to set reuse address: " + ec.message());
throw std::runtime_error("Failed to set receive buffer size: " + ec.message());
}
}

Expand Down

0 comments on commit 8e658ac

Please sign in to comment.