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

udp receive logic redesign #1271

Merged
merged 25 commits into from
Dec 14, 2023
Merged

Conversation

rex-schilasky
Copy link
Contributor

@rex-schilasky rex-schilasky commented Nov 26, 2023

Description

UDP logic redesigned:

  • separate message udp receiver for:
    • rcv_sample.cpp: udp payload and registration (eCAL::pb::Sample)
    • rcv_logging.cpp: udp logging messages (eCAL::pb::LogMessage)
  • UDP defragmentation logic separated to rcv_sample_slot.cpp
  • all receive threads now spawned inside udp receiver implementation
  • eCAL::CThread fully replaced by std::thread
  • shm monitoring logic integration into registration receiver simplified

Related issues

Preparation for solving #1236
Fixes #1276

Cherry-pick to

  • none

@rex-schilasky rex-schilasky marked this pull request as draft November 26, 2023 19:04
@rex-schilasky rex-schilasky marked this pull request as ready for review November 30, 2023 10:15
Copy link
Contributor

@KerstinKeller KerstinKeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rex-schilasky I don't think I can review this just by looking at it, I guess it needs to be reviewed in a 1:1 session.

@@ -107,7 +107,7 @@ namespace eCAL
*
* @return Zero if succeeded.
**/
ECAL_DEPRECATE_SINCE_5_12("use GetMonitoring and publish yourself")
ECAL_DEPRECATE_SINCE_5_12("No more implemented")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is missleading. How about "The function PubMonitoring is no longer implemented. Instead use GetMonitoring and publish yourself"?

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 20 out of 34. Check the log or trigger a new build to see more.

{
std::this_thread::sleep_for(std::chrono::milliseconds(500));

eCAL::pb::rec_server::RecServerConfig config_pb;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'config_pb' is not initialized [cppcoreguidelines-init-variables]

Suggested change
eCAL::pb::rec_server::RecServerConfig config_pb;
eCAL::pb::rec_server::RecServerConfig config_pb = 0;

Comment on lines +178 to 179
#define CMN_UDP_RECEIVE_THREAD_CYCLE_TIME_MS 1000

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: replace macro with enum [modernize-macro-to-enum]

Suggested change
#define CMN_UDP_RECEIVE_THREAD_CYCLE_TIME_MS 1000
enum {
CMN_UDP_RECEIVE_THREAD_CYCLE_TIME_MS = 1000};

/* cylce time udp paylaod receive thread in ms */
#define CMN_PAYLOAD_RECEIVE_THREAD_CYCLE_TIME_MS 1000
/* cylce time udp receive threads in ms */
#define CMN_UDP_RECEIVE_THREAD_CYCLE_TIME_MS 1000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: macro 'CMN_UDP_RECEIVE_THREAD_CYCLE_TIME_MS' defines an integral constant; prefer an enum instead [modernize-macro-to-enum]

#define CMN_UDP_RECEIVE_THREAD_CYCLE_TIME_MS           1000
        ^

{
namespace UDP
{
class CLoggingReceiver
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: class 'CLoggingReceiver' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator [cppcoreguidelines-special-member-functions]

    class CLoggingReceiver
          ^

if (m_sample_receiver == nullptr) return(0);

// read sample_name size
const unsigned short sample_name_size = ((unsigned short*)(msg_buffer_.data()))[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

      const unsigned short sample_name_size = ((unsigned short*)(msg_buffer_.data()))[0];
                                               ^

size_t sent_sum(0);

int32_t total_packet_num = int32_t(buf_len_ / MSG_PAYLOAD_SIZE);
if (buf_len_ % MSG_PAYLOAD_SIZE) total_packet_num++;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: implicit conversion 'unsigned long' -> bool [readability-implicit-bool-conversion]

Suggested change
if (buf_len_ % MSG_PAYLOAD_SIZE) total_packet_num++;
if ((buf_len_ % MSG_PAYLOAD_SIZE) != 0u) total_packet_num++;

getifaddrs(&ifap);

// create a list of network interfaces indexes
for (ifa = ifap; ifa; ifa = ifa->ifa_next)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: implicit conversion 'ifaddrs *' -> bool [readability-implicit-bool-conversion]

Suggested change
for (ifa = ifap; ifa; ifa = ifa->ifa_next)
for (ifa = ifap; ifa != nullptr; ifa = ifa->ifa_next)

// create a list of network interfaces indexes
for (ifa = ifap; ifa; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_PACKET)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: implicit conversion 'struct sockaddr *' -> bool [readability-implicit-bool-conversion]

Suggested change
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_PACKET)
if ((ifa->ifa_addr != nullptr) && ifa->ifa_addr->sa_family == AF_PACKET)

{
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_PACKET)
{
int index = if_nametoindex(ifa->ifa_name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'index' of type 'int' can be declared 'const' [misc-const-correctness]

Suggested change
int index = if_nametoindex(ifa->ifa_name);
int const index = if_nametoindex(ifa->ifa_name);

if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_PACKET)
{
int index = if_nametoindex(ifa->ifa_name);
if (index)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: implicit conversion 'int' -> bool [readability-implicit-bool-conversion]

Suggested change
if (index)
if (index != 0)

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

}

CSampleReceiver::CSampleReceiver(const IO::UDP::SReceiverAttr& attr_, HasSampleCallbackT has_sample_callback_, ApplySampleCallbackT apply_sample_callback_) :
m_has_sample_callback(has_sample_callback_), m_apply_sample_callback(apply_sample_callback_)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: parameter 'has_sample_callback_' is passed by value and only copied once; consider moving it to avoid unnecessary copies [performance-unnecessary-value-param]

ecal/core/src/io/udp/ecal_udp_sample_receiver.cpp:27:

+ 
+ #include <utility>
Suggested change
m_has_sample_callback(has_sample_callback_), m_apply_sample_callback(apply_sample_callback_)
m_has_sample_callback(std::move(has_sample_callback_)), m_apply_sample_callback(apply_sample_callback_)

inline static bool set_socket_mcast_group_option(int socket, const char* ipaddr_, int option)
{
// set the multicast socket option on all interfaces
for (int iface : get_interface_index_list())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'iface' of type 'int' can be declared 'const' [misc-const-correctness]

Suggested change
for (int iface : get_interface_index_list())
for (int const iface : get_interface_index_list())

group->sin_addr.s_addr = inet_addr(ipaddr_);
group->sin_port = 0;

int rc = setsockopt(socket, IPPROTO_IP, option, &group_req, sizeof(group_source_req));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'rc' of type 'int' can be declared 'const' [misc-const-correctness]

Suggested change
int rc = setsockopt(socket, IPPROTO_IP, option, &group_req, sizeof(group_source_req));
int const rc = setsockopt(socket, IPPROTO_IP, option, &group_req, sizeof(group_source_req));

AddMultiCastGroup(attr_.address.c_str());

// state successful creation
m_created = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'm_created' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]

ecal/core/src/io/udp/sendreceive/udp_receiver_asio.cpp:39:

-       m_created(false),
+       m_created(true),
Suggested change
m_created = true;


log_ = logging.SerializeAsString();
return((int)log_.size());
}

int PubMonitoring(bool state_, std::string name_ /* = "ecal.monitoring"*/)
int PubMonitoring(bool /*state_*/, std::string /*name_*/)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: the parameter #2 is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

Suggested change
int PubMonitoring(bool /*state_*/, std::string /*name_*/)
int PubMonitoring(bool /*state_*/, const std::string& /*name_*/)

ecal/core/include/ecal/ecal_monitoring.h:110:

-     ECAL_API int PubMonitoring(bool state_, std::string name_ = "ecal.monitoring");
+     ECAL_API int PubMonitoring(bool state_, const std::string& name_ = "ecal.monitoring");

bool m_reg_topics;
bool m_reg_services;
bool m_reg_process;
static std::atomic<bool> m_created;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'm_created' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

    static std::atomic<bool>            m_created;
                                        ^

return_value &= ApplySample(sample);
for (const auto& sample : sample_list.samples())
{
if (g_registration_receiver()) g_registration_receiver()->ApplySample(sample);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: implicit conversion 'CRegistrationReceiver *' -> bool [readability-implicit-bool-conversion]

Suggested change
if (g_registration_receiver()) g_registration_receiver()->ApplySample(sample);
if (g_registration_receiver() != nullptr) g_registration_receiver()->ApplySample(sample);

@@ -84,6 +80,7 @@ namespace eCAL
void EnableLoopback(bool state_);
bool LoopBackEnabled() const { return m_loopback; };

bool HasSample(const std::string& /*sample_name_*/) { return(true); };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: method 'HasSample' can be made static [readability-convert-member-functions-to-static]

Suggested change
bool HasSample(const std::string& /*sample_name_*/) { return(true); };
static bool HasSample(const std::string& /*sample_name_*/) { return(true); };

static std::atomic<bool> m_created;
bool m_network;
bool m_loopback;
static std::atomic<bool> m_created;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'm_created' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

    static std::atomic<bool>              m_created;
                                          ^

* @param callback A callback function to be executed in the CallbackThread thread.
*/
CCallbackThread(std::function<void()> callback)
: callback_(callback) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: parameter 'callback' is passed by value and only copied once; consider moving it to avoid unnecessary copies [performance-unnecessary-value-param]

ecal/core/src/util/ecal_thread.h:28:

+ #include <utility>
Suggested change
: callback_(callback) {}
: callback_(std::move(callback)) {}

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

m_created = true;
}

CUDPReceiverAsio::~CUDPReceiverAsio()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: an exception may be thrown in function '~CUDPReceiverAsio' which should not throw exceptions [bugprone-exception-escape]

    CUDPReceiverAsio::~CUDPReceiverAsio()
                      ^

Copy link
Contributor

@KerstinKeller KerstinKeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that you know what you are doing 😄

@rex-schilasky rex-schilasky merged commit 28ef1a1 into master Dec 14, 2023
14 checks passed
@rex-schilasky rex-schilasky deleted the feature/udp-receive-threading branch December 14, 2023 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ttl 0 is not a valid ttl for unicast
2 participants