AsyncTCPClient.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 
36 
37 namespace sick {
38 namespace communication {
40  boost::asio::io_service& io_service,
41  const boost::asio::ip::address_v4& server_ip,
42  const uint16_t& server_port)
43  : m_packet_handler(packet_handler)
44  , m_io_work_ptr()
45  , m_io_service(io_service)
46 
47 {
48  // Keep io_service busy
49  m_io_work_ptr = std::make_shared<boost::asio::io_service::work>(boost::ref(m_io_service));
50  try
51  {
52  m_socket_ptr = std::make_shared<boost::asio::ip::tcp::socket>(boost::ref(m_io_service));
53  }
54  catch (std::exception& e)
55  {
56  ROS_ERROR("Exception while creating socket: %s", e.what());
57  }
58  m_remote_endpoint = boost::asio::ip::tcp::endpoint(server_ip, server_port);
59  ROS_INFO("TCP client is setup");
60 }
61 
63 
65 {
66  boost::mutex::scoped_lock lock(m_socket_mutex);
67  boost::system::error_code ec;
68  m_socket_ptr->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
69  if (ec != 0)
70  {
71  ROS_ERROR("Error shutting socket down: %i", ec.value());
72  }
73  else
74  {
75  ROS_INFO("TCP Connection successfully shutdown");
76  }
77 
78  m_socket_ptr->close(ec);
79  if (ec != 0)
80  {
81  ROS_ERROR("Error closing Socket: %i", ec.value());
82  }
83  else
84  {
85  ROS_INFO("TCP Socket successfully closed.");
86  }
87 }
88 
90 {
91  boost::mutex::scoped_lock lock(m_socket_mutex);
92  boost::mutex::scoped_lock lock_connect(m_connect_mutex);
93  m_socket_ptr->async_connect(m_remote_endpoint, [this](boost::system::error_code ec) {
94  if (ec != 0)
95  {
96  ROS_ERROR("TCP error code: %i", ec.value());
97  }
98  else
99  {
100  ROS_INFO("TCP connection successfully established.");
101  }
102  m_connect_condition.notify_all();
103  });
104 
105  m_connect_condition.wait(lock_connect);
106 }
107 
108 
111 {
112  boost::mutex::scoped_lock lock(m_socket_mutex);
113  if (!m_socket_ptr)
114  {
115  return;
116  }
117  boost::asio::async_write(*m_socket_ptr,
118  boost::asio::buffer(sendBuffer),
119  [this](boost::system::error_code ec, std::size_t bytes_send) {
120  this->handleSendAndReceive(ec, bytes_send);
121  });
122 }
123 
125 {
126  boost::mutex::scoped_lock lock(m_socket_mutex);
127  if (!m_socket_ptr)
128  {
129  return;
130  }
131  m_socket_ptr->async_read_some(boost::asio::buffer(m_recv_buffer),
132  [this](boost::system::error_code ec, std::size_t bytes_recvd) {
133  this->handleReceive(ec, bytes_recvd);
134  });
135 }
136 
138 {
139  m_packet_handler = packet_handler;
140 }
141 
142 void AsyncTCPClient::handleSendAndReceive(const boost::system::error_code& error,
143  const std::size_t& bytes_transferred)
144 {
145  // Check for errors
146  if (!error || error == boost::asio::error::message_size)
147  {
148  initiateReceive();
149  }
150  else
151  {
152  ROS_ERROR("Error in tcp handle send and receive: %i", error.value());
153  }
154 }
155 
156 
158 
159 void AsyncTCPClient::handleReceive(const boost::system::error_code& error,
160  const std::size_t& bytes_transferred)
161 {
162  if (!error)
163  {
164  sick::datastructure::PacketBuffer packet_buffer(m_recv_buffer, bytes_transferred);
165  m_packet_handler(packet_buffer);
166  }
167  else
168  {
169  ROS_ERROR("Error in tcp handle receive: %i", error.value());
170  }
171 }
172 
173 
174 } // namespace communication
175 } // namespace sick
boost::function< void(const sick::datastructure::PacketBuffer &)> PacketHandler
Typedef to a function referencing a packet handler. This can be passed to the class and therefore be ...
A packetbuffer for the raw data from the sensor.
Definition: PacketBuffer.h:61
void initiateReceive()
Initiates the listening for a message from the server.
datastructure::PacketBuffer::ArrayBuffer m_recv_buffer
AsyncTCPClient(PacketHandler packet_handler, boost::asio::io_service &io_service, const boost::asio::ip::address_v4 &server_ip, const uint16_t &server_port)
Constructor of the asynchronous tcp client.
void handleReceive(const boost::system::error_code &error, const std::size_t &bytes_transferred)
void doConnect()
Establishes a connection from the host to the sensor.
void handleSendAndReceive(const boost::system::error_code &error, const std::size_t &bytes_transferred)
#define ROS_INFO(...)
boost::asio::io_service & m_io_service
virtual ~AsyncTCPClient()
The destructor of the asynchronous tcp client.
void doDisconnect()
Disconnects the host from the sensor.
boost::asio::ip::tcp::endpoint m_remote_endpoint
std::shared_ptr< boost::asio::ip::tcp::socket > m_socket_ptr
void setPacketHandler(const PacketHandler &packet_handler)
Sets the packet handler function.
void doSendAndReceive(const sick::datastructure::PacketBuffer::VectorBuffer &sendBuffer)
Start a cycle of sensing a command and waiting got the return.
std::shared_ptr< boost::asio::io_service::work > m_io_work_ptr
std::vector< uint8_t > VectorBuffer
Typedef for a vector buffer, to sort the incoming packets.
Definition: PacketBuffer.h:71
#define ROS_ERROR(...)


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