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_service(io_service)
53 catch (
const std::exception& e)
55 ROS_ERROR(
"Exception while creating socket: %s", e.what());
66 boost::system::error_code ec;
67 m_socket_ptr->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
68 if (ec != boost::system::errc::success)
70 ROS_ERROR(
"Error shutting socket down: %i", ec.value());
74 ROS_INFO(
"TCP Connection successfully shutdown");
78 if (ec != boost::system::errc::success)
80 ROS_ERROR(
"Error closing Socket: %i", ec.value());
84 ROS_INFO(
"TCP Socket successfully closed.");
93 if (ec != boost::system::errc::success)
95 ROS_ERROR(
"TCP error code: %i", ec.value());
99 ROS_INFO(
"TCP connection successfully established.");
116 boost::asio::buffer(sendBuffer),
117 [
this](boost::system::error_code ec, std::size_t bytes_send) {
130 [
this](boost::system::error_code ec, std::size_t bytes_recvd) {
141 const std::size_t& bytes_transferred)
144 if (!error || error == boost::asio::error::message_size)
150 ROS_ERROR(
"Error in tcp handle send and receive: %i", error.value());
158 const std::size_t& bytes_transferred)
167 ROS_ERROR(
"Error in tcp handle receive: %i", error.value());
boost::mutex m_connect_mutex
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.
void initiateReceive()
Initiates the listening for a message from the server.
datastructure::PacketBuffer::ArrayBuffer m_recv_buffer
void doSendAndReceive(const std::vector< uint8_t > &sendBuffer)
Start a cycle of sensing a command and waiting got the return.
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.
AsyncTCPClient(const 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.
boost::condition m_connect_condition
void handleSendAndReceive(const boost::system::error_code &error, const std::size_t &bytes_transferred)
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
boost::mutex m_socket_mutex
void setPacketHandler(const PacketHandler &packet_handler)
Sets the packet handler function.
std::shared_ptr< boost::asio::io_service::work > m_io_work_ptr
PacketHandler m_packet_handler