Skip to content

Commit

Permalink
core: cleanup udp configuration and sender/receiver attributes (#1253)
Browse files Browse the repository at this point in the history
  • Loading branch information
rex-schilasky committed Nov 17, 2023
1 parent 6e3ab76 commit 0f41b3a
Show file tree
Hide file tree
Showing 18 changed files with 152 additions and 130 deletions.
10 changes: 3 additions & 7 deletions ecal/core/src/ecal_log_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
#include <ecal/ecal_os.h>
#include <ecal/ecal_config.h>

#include "ecal_def.h"

#include "ecal_log_impl.h"

#include "io/udp_configurations.h"

#include <mutex>
Expand Down Expand Up @@ -152,12 +149,11 @@ namespace eCAL
if(m_filter_mask_udp != 0)
{
SSenderAttr attr;
// for local only communication we switch to local broadcasting to bypass vpn's or firewalls
attr.address = UDP::GetLoggingMulticastAddress();
attr.port = UDP::GetLoggingPort();
attr.broadcast = !Config::IsNetworkEnabled();
attr.ipaddr = UDP::GetLoggingMulticastAddress();
attr.port = Config::GetUdpMulticastPort() + NET_UDP_MULTICAST_PORT_LOG_OFF;
attr.loopback = true;
attr.ttl = Config::GetUdpMulticastTtl();
attr.ttl = UDP::GetMulticastTtl();
attr.sndbuf = Config::GetUdpMulticastSndBufSizeBytes();

m_udp_sender = std::make_unique<CUDPSender>(attr);
Expand Down
10 changes: 5 additions & 5 deletions ecal/core/src/ecal_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

#include "ecal_def.h"
#include "ecal_config_reader_hlp.h"
#include "ecal_registration_provider.h"
#include "ecal_registration_receiver.h"
#include "ecal_globals.h"
#include "ecal_process.h"
#include "io/udp_configurations.h"

#include <array>
#include <chrono>
Expand All @@ -42,8 +42,8 @@

#include "sys_usage.h"

#include <stdlib.h>
#include <stdio.h>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <atomic>
Expand Down Expand Up @@ -229,7 +229,7 @@ namespace eCAL
{
sstream << "Network mode : local" << std::endl;
}
sstream << "Network ttl : " << Config::GetUdpMulticastTtl() << std::endl;
sstream << "Network ttl : " << UDP::GetMulticastTtl() << std::endl;
sstream << "Network sndbuf : " << GetBufferStr(Config::GetUdpMulticastSndBufSizeBytes()) << std::endl;
sstream << "Network rcvbuf : " << GetBufferStr(Config::GetUdpMulticastRcvBufSizeBytes()) << std::endl;
sstream << "Multicast cfg version : v" << static_cast<uint32_t>(Config::GetUdpMulticastConfigVersion()) << std::endl;
Expand Down Expand Up @@ -549,7 +549,7 @@ namespace eCAL
creation_flag = CREATE_NEW_CONSOLE;
}

short win_state;
short win_state = 0;
switch (process_mode_)
{
case 0:
Expand Down
7 changes: 3 additions & 4 deletions ecal/core/src/ecal_registration_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ namespace eCAL
{
// set network attributes
SSenderAttr attr;
attr.ipaddr = UDP::GetRegistrationMulticastAddress();
attr.port = Config::GetUdpMulticastPort() + NET_UDP_MULTICAST_PORT_REG_OFF;
attr.ttl = Config::GetUdpMulticastTtl();
// for local only communication we switch to local broadcasting to bypass vpn's or firewalls
attr.address = UDP::GetRegistrationMulticastAddress();
attr.port = UDP::GetRegistrationPort();
attr.ttl = UDP::GetMulticastTtl();
attr.broadcast = !Config::IsNetworkEnabled();
attr.loopback = true;
attr.sndbuf = Config::GetUdpMulticastSndBufSizeBytes();
Expand Down
5 changes: 2 additions & 3 deletions ecal/core/src/ecal_registration_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,9 @@ namespace eCAL
{
// start registration receive thread
SReceiverAttr attr;
// for local only communication we switch to local broadcasting to bypass vpn's or firewalls
attr.address = UDP::GetRegistrationMulticastAddress();
attr.port = UDP::GetRegistrationPort();
attr.broadcast = !Config::IsNetworkEnabled();
attr.ipaddr = UDP::GetRegistrationMulticastAddress();
attr.port = Config::GetUdpMulticastPort() + NET_UDP_MULTICAST_PORT_REG_OFF;
attr.loopback = true;
attr.rcvbuf = Config::GetUdpMulticastRcvBufSizeBytes();

Expand Down
19 changes: 5 additions & 14 deletions ecal/core/src/io/ecal_receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,11 @@ namespace eCAL
////////////////////////////////////////////////////////
struct SReceiverAttr
{
SReceiverAttr() :
port(0),
broadcast(false),
unicast(false),
loopback(true),
rcvbuf(1024 * 1024)
{};

std::string ipaddr;
int port;
bool broadcast;
bool unicast;
bool loopback;
int rcvbuf;
std::string address;
int port = 0;
bool broadcast = false;
bool loopback = true;
int rcvbuf = 1024 * 1024;
};

class CReceiver
Expand Down
2 changes: 1 addition & 1 deletion ecal/core/src/io/snd_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace eCAL
if (data_size > 0)
{
// and send it
sent_sum = SendSampleBuffer(m_payload.data(), data_size, bandwidth_, std::bind(TransmitToUDP, std::placeholders::_1, std::placeholders::_2, m_udp_sender, m_attr.ipaddr));
sent_sum = SendSampleBuffer(m_payload.data(), data_size, bandwidth_, std::bind(TransmitToUDP, std::placeholders::_1, std::placeholders::_2, m_udp_sender, m_attr.address));

#ifndef NDEBUG
// log it
Expand Down
113 changes: 87 additions & 26 deletions ecal/core/src/io/udp_configurations.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,97 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ========================= eCAL LICENSE =================================
*/

#include "io/udp_configurations.h"

#include <ecal/ecal_config.h>
#include "topic2mcast.h"

const std::string localhost_udp_address{ "127.255.255.255" };

#include "ecal_def.h"
#include "topic2mcast.h"

std::string eCAL::UDP::GetRegistrationMulticastAddress()
namespace eCAL
{
const bool local_only = !Config::IsNetworkEnabled();
if (local_only)
namespace UDP
{
return localhost_udp_address;
}
else
{
// both in v1 and v2, the mulicast group is returned as the adress for the registration layer
return Config::GetUdpMulticastGroup();
}
}
std::string LocalBroadcastAddress()
{
// the specific address 127.255.255.255 is the broadcast address within the loopback range (127.0.0.0 to 127.255.255.255)
return "127.255.255.255";
}

std::string eCAL::UDP::GetLoggingMulticastAddress()
{
//TODO: At the moment, both logging and monitoring addresses seem to be the same
// Should it be kept or changed?
return GetRegistrationMulticastAddress();
}
std::string GetRegistrationMulticastAddress()
{
const bool local_only = !Config::IsNetworkEnabled();
if (local_only)
{
return LocalBroadcastAddress();
}
else
{
// both in v1 and v2, the mulicast group is returned as the adress for the registration layer
return Config::GetUdpMulticastGroup();
}
}

std::string eCAL::UDP::GetTopicMulticastAddress(const std::string& topic_name)
{
if (Config::GetUdpMulticastConfigVersion() == Config::UdpConfigVersion::V1)
return UDP::V1::topic2mcast(topic_name, Config::GetUdpMulticastGroup(), Config::GetUdpMulticastMask());
// v2
return UDP::V2::topic2mcast(topic_name, Config::GetUdpMulticastGroup(), Config::GetUdpMulticastMask());
int GetRegistrationPort()
{
return Config::GetUdpMulticastPort() + NET_UDP_MULTICAST_PORT_REG_OFF;
}

std::string GetLoggingMulticastAddress()
{
// both logging and monitoring use the same addresses but different ports
return GetRegistrationMulticastAddress();
}

int GetLoggingPort()
{
return Config::GetUdpMulticastPort() + NET_UDP_MULTICAST_PORT_LOG_OFF;
}

std::string GetPayloadMulticastAddress(const std::string& topic_name)
{
// v1
if (Config::GetUdpMulticastConfigVersion() == Config::UdpConfigVersion::V1)
{
return UDP::V1::topic2mcast(topic_name, Config::GetUdpMulticastGroup(), Config::GetUdpMulticastMask());
}

// v2
return UDP::V2::topic2mcast(topic_name, Config::GetUdpMulticastGroup(), Config::GetUdpMulticastMask());
}

int GetPayloadPort()
{
return Config::GetUdpMulticastPort() + NET_UDP_MULTICAST_PORT_SAMPLE_OFF;
}

int GetMulticastTtl()
{
const bool local_only = !Config::IsNetworkEnabled();
if (local_only)
{
return 1;
}
else
{
return Config::GetUdpMulticastTtl();
}
}
}
}
18 changes: 14 additions & 4 deletions ecal/core/src/io/udp_configurations.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ namespace eCAL
{
namespace UDP
{
// Return the Multicast Adress used for sending Registration information
// return local broadcast address
std::string LocalBroadcastAddress();

// return the multicast adress/port used for sending/receiving the registration information
std::string GetRegistrationMulticastAddress();
int GetRegistrationPort();

// return the multicast adress/port used for sending/receiving the logging information
std::string GetLoggingMulticastAddress();
int GetLoggingPort();

std::string GetTopicMulticastAddress(const std::string& topic_name);
}
// return the multicast adress/port used for sending/receiving the topic payload
std::string GetPayloadMulticastAddress(const std::string& topic_name);
int GetPayloadPort();

}
// return multicast udp package time to live setting
int GetMulticastTtl();
}
}
16 changes: 4 additions & 12 deletions ecal/core/src/io/udp_receiver_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,8 @@ namespace eCAL
CUDPReceiverBase(attr_),
m_created(false),
m_broadcast(attr_.broadcast),
m_unicast(attr_.unicast),
m_socket(m_iocontext)
{
if (m_broadcast && m_unicast)
{
std::cerr << "CUDPReceiverAsio: Setting broadcast and unicast option true is not allowed." << std::endl;
return;
}

// create socket
const asio::ip::udp::endpoint listen_endpoint(asio::ip::udp::v4(), static_cast<unsigned short>(attr_.port));
{
Expand Down Expand Up @@ -75,9 +68,8 @@ namespace eCAL
}
}

if (!m_unicast)
// set loopback option
{
// set loopback option
const asio::ip::multicast::enable_loopback loopback(attr_.loopback);
asio::error_code ec;
m_socket.set_option(loopback, ec);
Expand All @@ -101,15 +93,15 @@ namespace eCAL
}

// join multicast group
AddMultiCastGroup(attr_.ipaddr.c_str());
AddMultiCastGroup(attr_.address.c_str());

// state successful creation
m_created = true;
}

bool CUDPReceiverAsio::AddMultiCastGroup(const char* ipaddr_)
{
if (!m_broadcast && !m_unicast)
if (!m_broadcast)
{
// join multicast group
#ifdef __linux__
Expand Down Expand Up @@ -145,7 +137,7 @@ namespace eCAL

bool CUDPReceiverAsio::RemMultiCastGroup(const char* ipaddr_)
{
if (!m_broadcast && !m_unicast)
if (!m_broadcast)
{
// Leave multicast group
#ifdef __linux__
Expand Down
1 change: 0 additions & 1 deletion ecal/core/src/io/udp_receiver_asio.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ namespace eCAL

bool m_created;
bool m_broadcast;
bool m_unicast;
asio::io_context m_iocontext;
asio::ip::udp::socket m_socket;
asio::ip::udp::endpoint m_sender_endpoint;
Expand Down
12 changes: 6 additions & 6 deletions ecal/core/src/io/udp_receiver_npcap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace eCAL
CUDPReceiverPcap::CUDPReceiverPcap(const SReceiverAttr& attr_)
: CUDPReceiverBase(attr_)
, m_created(false)
, m_unicast(attr_.unicast)
, m_broadcast(attr_.broadcast)
{
// set receive buffer size (default = 1 MB)
int rcvbuf = 1024 * 1024;
Expand All @@ -46,22 +46,22 @@ namespace eCAL
return;
}

if (!m_unicast)
// set loopback option
if (!m_broadcast)
{
// set loopback option
m_socket.setMulticastLoopbackEnabled(attr_.loopback);
}

// join multicast group
AddMultiCastGroup(attr_.ipaddr.c_str());
AddMultiCastGroup(attr_.address.c_str());

// state successful creation
m_created = true;
}

bool CUDPReceiverPcap::AddMultiCastGroup(const char* ipaddr_)
{
if (!m_unicast)
if (!m_broadcast)
{
// join multicast group
if (!m_socket.joinMulticastGroup(Udpcap::HostAddress(ipaddr_)))
Expand All @@ -75,7 +75,7 @@ namespace eCAL

bool CUDPReceiverPcap::RemMultiCastGroup(const char* ipaddr_)
{
if (!m_unicast)
if (!m_broadcast)
{
// leave multicast group
if (!m_socket.leaveMulticastGroup(Udpcap::HostAddress(ipaddr_)))
Expand Down
2 changes: 1 addition & 1 deletion ecal/core/src/io/udp_receiver_npcap.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace eCAL

protected:
bool m_created;
bool m_unicast;
bool m_broadcast;
Udpcap::UdpcapSocket m_socket;
};

Expand Down
Loading

0 comments on commit 0f41b3a

Please sign in to comment.