From 771280e603494e57d6f817374d087b0a38a049e2 Mon Sep 17 00:00:00 2001 From: Rex Schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Fri, 12 Jan 2024 13:29:04 +0100 Subject: [PATCH] ensure correct udp socket concurrent handling by mutex (#1289) --- ecal/core/src/io/udp_receiver.cpp | 9 +++++++++ ecal/core/src/io/udp_receiver.h | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ecal/core/src/io/udp_receiver.cpp b/ecal/core/src/io/udp_receiver.cpp index d7e6925fb6..012f596006 100644 --- a/ecal/core/src/io/udp_receiver.cpp +++ b/ecal/core/src/io/udp_receiver.cpp @@ -73,25 +73,34 @@ namespace eCAL bool CUDPReceiver::Destroy() { if (!m_socket_impl) return(false); + + const std::lock_guard lock(m_socket_mtx); m_socket_impl.reset(); + return(true); } bool CUDPReceiver::AddMultiCastGroup(const char* ipaddr_) { if (!m_socket_impl) return(false); + + const std::lock_guard lock(m_socket_mtx); return(m_socket_impl->AddMultiCastGroup(ipaddr_)); } bool CUDPReceiver::RemMultiCastGroup(const char* ipaddr_) { if (!m_socket_impl) return(false); + + const std::lock_guard lock(m_socket_mtx); return(m_socket_impl->RemMultiCastGroup(ipaddr_)); } size_t CUDPReceiver::Receive(char* buf_, size_t len_, int timeout_, ::sockaddr_in* address_ /* = nullptr */) { if (!m_socket_impl) return(0); + + const std::lock_guard lock(m_socket_mtx); return(m_socket_impl->Receive(buf_, len_, timeout_, address_)); } } diff --git a/ecal/core/src/io/udp_receiver.h b/ecal/core/src/io/udp_receiver.h index 500aaeec6c..6a976bde6f 100644 --- a/ecal/core/src/io/udp_receiver.h +++ b/ecal/core/src/io/udp_receiver.h @@ -24,6 +24,7 @@ #pragma once #include +#include #include "ecal_receiver.h" namespace eCAL @@ -44,7 +45,8 @@ namespace eCAL size_t Receive(char* buf_, size_t len_, int timeout_, ::sockaddr_in* address_ = nullptr) override; protected: - bool m_use_npcap; + bool m_use_npcap; + std::mutex m_socket_mtx; std::shared_ptr m_socket_impl; }; }