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_work_ptr()
45  , m_io_service(io_service)
46 {
47  // Keep io_service busy
48  m_io_work_ptr = std::make_shared<boost::asio::io_service::work>(boost::ref(m_io_service));
49  try
50  {
51  m_socket_ptr = std::make_shared<boost::asio::ip::udp::socket>(
52  boost::ref(m_io_service),
53  boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), local_port));
54  }
55  catch (std::exception& e)
56  {
57  ROS_ERROR("Exception while creating socket: %s", e.what());
58  }
59  ROS_INFO("UDP client is setup");
60 }
61 
63 {
64  m_io_service.stop();
65 }
66 
68 {
69  m_socket_ptr->async_receive_from(boost::asio::buffer(m_recv_buffer),
71  [this](boost::system::error_code ec, std::size_t bytes_recvd) {
72  this->handleReceive(ec, bytes_recvd);
73  });
74 }
75 
76 void AsyncUDPClient::handleReceive(const boost::system::error_code& error,
77  const std::size_t& bytes_transferred)
78 {
79  if (!error)
80  {
81  sick::datastructure::PacketBuffer packet_buffer(m_recv_buffer, bytes_transferred);
82  m_packet_handler(packet_buffer);
83  }
84  else
85  {
86  ROS_ERROR("Error in UDP handle receive: %i", error.value());
87  }
88  startReceive();
89 }
90 
91 
93 {
94  startReceive();
95 }
96 
98 {
99  if (m_socket_ptr)
100  {
101  return m_socket_ptr->local_endpoint().port();
102  }
103  return 0;
104 }
105 
106 } // namespace communication
107 } // namespace sick
AsyncUDPClient(PacketHandler packet_handler, boost::asio::io_service &io_service, const uint16_t &local_port=0)
Constructor of the asynchronous udp client.
A packetbuffer for the raw data from the sensor.
Definition: PacketBuffer.h:61
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(...)
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
unsigned short get_local_port()
Returns the actual port assigned to the local machine.


sick_safetyscanners
Author(s): Lennart Puck
autogenerated on Thu May 9 2019 02:41:08