AsyncUDPClient.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 
24 // -- END LICENSE BLOCK ------------------------------------------------
25 
26 //----------------------------------------------------------------------
33 //----------------------------------------------------------------------
34 
35 
37 
38 namespace sick {
39 namespace communication {
41  boost::asio::io_service& io_service,
42  const uint16_t& local_port)
43  : m_packet_handler(packet_handler)
44  , m_io_service(io_service)
45 {
46  // Keep io_service busy
47  m_io_work_ptr = std::make_shared<boost::asio::io_service::work>(m_io_service);
48  try
49  {
50  auto endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), local_port);
51  m_socket_ptr = std::make_shared<boost::asio::ip::udp::socket>(m_io_service, endpoint);
52  }
53  catch (const std::exception& e)
54  {
55  ROS_ERROR("Exception while creating socket: %s", e.what());
56  }
57  ROS_INFO("UDP client is setup");
58 }
59 
61  boost::asio::io_service& io_service,
62  const boost::asio::ip::address_v4 host_ip,
63  const boost::asio::ip::address_v4 interface_ip,
64  const uint16_t& local_port)
65  : m_packet_handler(packet_handler)
66  , m_io_service(io_service)
67 {
68  if (!host_ip.is_multicast())
69  {
70  ROS_ERROR("Provided Host IP is not in the Multicast range!");
71  exit(-1);
72  }
73  if (interface_ip.is_unspecified())
74  {
75  // TODO better error handling
76  ROS_ERROR("Provided Interface IP is unspecified! Set it to the Interface IP which receives the "
77  "multicast packages.");
78  ROS_ERROR("Shutting down node");
79  exit(-1);
80  }
81  // Keep io_service busy
82  m_io_work_ptr = std::make_shared<boost::asio::io_service::work>(m_io_service);
83  try
84  {
85  auto endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), local_port);
86  m_socket_ptr = std::make_shared<boost::asio::ip::udp::socket>(m_io_service, endpoint);
87  m_socket_ptr->set_option(boost::asio::ip::multicast::join_group(host_ip, interface_ip));
88  }
89  catch (const std::exception& e)
90  {
91  ROS_ERROR("Exception while creating socket: %s", e.what());
92  }
93  ROS_INFO("UDP client is setup");
94 }
95 
97 {
98  m_io_service.stop();
99 }
100 
102 {
103  m_socket_ptr->async_receive_from(boost::asio::buffer(m_recv_buffer),
105  [this](boost::system::error_code ec, std::size_t bytes_recvd) {
106  this->handleReceive(ec, bytes_recvd);
107  });
108 }
109 
110 void AsyncUDPClient::handleReceive(const boost::system::error_code& error,
111  const std::size_t& bytes_transferred)
112 {
113  if (!error)
114  {
115  sick::datastructure::PacketBuffer packet_buffer(m_recv_buffer, bytes_transferred);
116  m_packet_handler(packet_buffer);
117  }
118  else
119  {
120  ROS_ERROR("Error in UDP handle receive: %i", error.value());
121  }
122  startReceive();
123 }
124 
125 
127 {
128  startReceive();
129 }
130 
132 {
133  if (m_socket_ptr)
134  {
135  return m_socket_ptr->local_endpoint().port();
136  }
137  return 0;
138 }
139 
140 } // namespace communication
141 } // namespace sick
A packetbuffer for the raw data from the sensor.
Definition: PacketBuffer.h:61
unsigned short getLocalPort()
Returns the actual port assigned to the local machine.
void handleReceive(const boost::system::error_code &error, const std::size_t &bytes_transferred)
boost::asio::ip::udp::endpoint m_remote_endpoint
void runService()
Start the listening loop for the udp data packets.
std::shared_ptr< boost::asio::ip::udp::socket > m_socket_ptr
boost::asio::io_service & m_io_service
#define ROS_INFO(...)
AsyncUDPClient(const PacketHandler &packet_handler, boost::asio::io_service &io_service, const uint16_t &local_port=0)
Constructor of the asynchronous udp client.
datastructure::PacketBuffer::ArrayBuffer m_recv_buffer
boost::function< void(const sick::datastructure::PacketBuffer &)> PacketHandler
Typedef to a reference to a function. Will be used to process the incoming packets.
virtual ~AsyncUDPClient()
The destructor of the asynchronous udp client.
#define ROS_ERROR(...)
std::shared_ptr< boost::asio::io_service::work > m_io_work_ptr


sick_safetyscanners
Author(s): Lennart Puck
autogenerated on Fri Apr 2 2021 02:45:41