udp.cc
Go to the documentation of this file.
1 
37 #include <functional>
38 #include <iostream>
39 
40 #include <utility/Exception.hh>
41 
42 #include "details/legacy/udp.hh"
43 
44 namespace multisense{
45 namespace legacy{
46 
48  size_t max_mtu,
49  std::function<void(const std::vector<uint8_t>&)> receive_callback):
50  m_socket(socket.sensor_socket),
51  m_stop(false),
52  m_max_mtu(max_mtu),
53  m_incoming_buffer(max_mtu, 0),
54  m_receive_callback(receive_callback)
55 
56 {
57  m_rx_thread = std::thread(&UdpReceiver::rx_thread, this);
58 }
59 
61 {
62  if(!m_stop.exchange(true))
63  {
64  if (m_rx_thread.joinable())
65  {
66  m_rx_thread.join();
67  }
68  }
69 }
70 
72 {
73  fd_set read_set;
74  while(!m_stop)
75  {
76  FD_ZERO(&read_set);
77  FD_SET(m_socket, &read_set);
78 
79  struct timeval tv = {0, 200000}; // 5Hz
80 #ifdef WIN32
81  // Windows is "special" and doesn't have the same call semantics as posix
82  const int result = select(1, &read_set, NULL, NULL, &tv);
83 #else
84  const int result = select(m_socket + 1, &read_set, NULL, NULL, &tv);
85 #endif
86  if (result <= 0)
87  {
88  continue;
89  }
90 
91  //
92  // Read all the data currently sitting on the socket
93  //
94  while(true)
95  {
96 
97  try
98  {
99 #if defined(WIN32) && !defined(__MINGW64__)
100 #pragma warning (push)
101 #pragma warning (disable : 4267)
102 #endif
103  const int bytes_read = recvfrom(m_socket,
104  reinterpret_cast<char*>(m_incoming_buffer.data()),
105  m_incoming_buffer.size(),
106  0, NULL, NULL);
107 #if defined(WIN32) && !defined(__MINGW64__)
108 #pragma warning (pop)
109 #endif
110  //
111  // Nothing left to read
112  //
113  if (bytes_read < 0)
114  {
115  break;
116  }
117 
118  m_incoming_buffer.resize(bytes_read);
121  }
122  catch (const std::exception& e)
123  {
124 
125  CRL_DEBUG("exception while decoding packet: %s\n", e.what());
126 
127  }
128  catch ( ... )
129  {
130 
131  CRL_DEBUG_RAW("unknown exception while decoding packet\n");
132  }
133  }
134  }
135 }
136 
137 int64_t publish_data(const NetworkSocket &socket, const std::vector<uint8_t> &data)
138 {
139 // disable MSVC warning for narrowing conversion.
140 #if defined(WIN32) && !defined(__MINGW64__)
141 #pragma warning (push)
142 #pragma warning (disable : 4267)
143 #endif
144  sockaddr_in* raw_address = socket.sensor_address.get();
145  const auto ret = sendto(socket.sensor_socket, (char*) data.data(), data.size(), 0,
146  (struct sockaddr *) raw_address,
147  sizeof(*raw_address));
148 #if defined(WIN32) && !defined(__MINGW64__)
149 #pragma warning (pop)
150 #endif
151 
152  if (static_cast<size_t>(ret) != data.size())
153  {
154  CRL_EXCEPTION("error sending data to sensor, %d/%d bytes written: %s",
155  ret, data.size(), strerror(errno));
156  }
157 
158  return ret;
159 }
160 
161 }
162 }
CRL_DEBUG
#define CRL_DEBUG(fmt,...)
Definition: Exception.hh:71
multisense::legacy::NetworkSocket::sensor_address
std::unique_ptr< sockaddr_in > sensor_address
Definition: udp.hh:53
Exception.hh
multisense::legacy::UdpReceiver::m_stop
std::atomic_bool m_stop
Atomic flag to stop the rx_thread on destruction.
Definition: udp.hh:99
multisense::legacy::UdpReceiver::~UdpReceiver
~UdpReceiver()
Definition: udp.cc:60
multisense::legacy::NetworkSocket::sensor_socket
socket_t sensor_socket
Definition: udp.hh:54
multisense::legacy::UdpReceiver::m_rx_thread
std::thread m_rx_thread
The rx_thread object which is spawned on construction.
Definition: udp.hh:94
CRL_DEBUG_RAW
#define CRL_DEBUG_RAW(fmt)
Definition: Exception.hh:78
CRL_EXCEPTION
#define CRL_EXCEPTION(fmt,...)
Definition: Exception.hh:85
udp.hh
multisense::legacy::UdpReceiver::rx_thread
void rx_thread()
The rx thread function which is receives UDP data.
Definition: udp.cc:71
multisense::legacy::UdpReceiver::m_max_mtu
size_t m_max_mtu
The amount of data to read off the socket during each read operation.
Definition: udp.hh:104
multisense::legacy::publish_data
int64_t publish_data(const NetworkSocket &socket, const std::vector< uint8_t > &data)
Convenience function used to user specified data out on the host's UDP socket.
Definition: udp.cc:137
multisense::legacy::NetworkSocket
Convenience network socket object which contains the data corresponding to our connection.
Definition: udp.hh:51
multisense::legacy::UdpReceiver::m_receive_callback
std::function< void(const std::vector< uint8_t > &)> m_receive_callback
User specified callback which is called once UDP data is received.
Definition: udp.hh:114
multisense::legacy::UdpReceiver::m_incoming_buffer
std::vector< uint8_t > m_incoming_buffer
Internal buffer used to write incoming UDP data into.
Definition: udp.hh:109
multisense::legacy::UdpReceiver::UdpReceiver
UdpReceiver(const NetworkSocket &socket, size_t max_mtu, std::function< void(const std::vector< uint8_t > &)> receive_callback)
Construct a UDP receiver for a given socket, and call a user callback whenever there is data availabl...
Definition: udp.cc:47
multisense
Definition: factory.cc:39
multisense::legacy::UdpReceiver::m_socket
socket_t m_socket
The internal socket which UDP data is receive on.
Definition: udp.hh:89


multisense_lib
Author(s):
autogenerated on Thu Apr 17 2025 02:49:09