Skip to content

Commit

Permalink
protecting udp receive sockets by mutex (should fix #1212)
Browse files Browse the repository at this point in the history
  • Loading branch information
rex-schilasky committed Dec 14, 2023
1 parent 36d8dc7 commit 9073656
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ecal/core/src/io/udp/sendreceive/udp_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,34 @@ namespace IO
bool CUDPReceiver::Destroy()
{
if (!m_socket_impl) return(false);

const std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> lock(m_socket_mtx);
return(m_socket_impl->Receive(buf_, len_, timeout_, address_));
}
}
Expand Down
2 changes: 2 additions & 0 deletions ecal/core/src/io/udp/sendreceive/udp_receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#endif

#include <memory>
#include <mutex>
#include <string>

namespace IO
Expand Down Expand Up @@ -85,6 +86,7 @@ namespace IO

protected:
bool m_use_npcap;
std::mutex m_socket_mtx;
std::shared_ptr<CUDPReceiverImpl> m_socket_impl;
};
}
Expand Down

0 comments on commit 9073656

Please sign in to comment.