Go to the documentation of this file.00001
00002
00003
00004
00024
00025
00026
00033
00034
00035 #include <sick_safetyscanners/communication/AsyncTCPClient.h>
00036
00037 namespace sick {
00038 namespace communication {
00039 AsyncTCPClient::AsyncTCPClient(PacketHandler packet_handler,
00040 boost::asio::io_service& io_service,
00041 const boost::asio::ip::address_v4& server_ip,
00042 const uint16_t& server_port)
00043 : m_packet_handler(packet_handler)
00044 , m_io_work_ptr()
00045 , m_io_service(io_service)
00046
00047 {
00048
00049 m_io_work_ptr = std::make_shared<boost::asio::io_service::work>(boost::ref(m_io_service));
00050 try
00051 {
00052 m_socket_ptr = std::make_shared<boost::asio::ip::tcp::socket>(boost::ref(m_io_service));
00053 }
00054 catch (std::exception& e)
00055 {
00056 ROS_ERROR("Exception while creating socket: %s", e.what());
00057 }
00058 m_remote_endpoint = boost::asio::ip::tcp::endpoint(server_ip, server_port);
00059 ROS_INFO("TCP client is setup");
00060 }
00061
00062 AsyncTCPClient::~AsyncTCPClient() {}
00063
00064 void AsyncTCPClient::doDisconnect()
00065 {
00066 boost::mutex::scoped_lock lock(m_socket_mutex);
00067 boost::system::error_code ec;
00068 m_socket_ptr->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
00069 if (ec != 0)
00070 {
00071 ROS_ERROR("Error shutting socket down: %i", ec.value());
00072 }
00073 else
00074 {
00075 ROS_INFO("TCP Connection successfully shutdown");
00076 }
00077
00078 m_socket_ptr->close(ec);
00079 if (ec != 0)
00080 {
00081 ROS_ERROR("Error closing Socket: %i", ec.value());
00082 }
00083 else
00084 {
00085 ROS_INFO("TCP Socket successfully closed.");
00086 }
00087 }
00088
00089 void AsyncTCPClient::doConnect()
00090 {
00091 boost::mutex::scoped_lock lock(m_socket_mutex);
00092 boost::mutex::scoped_lock lock_connect(m_connect_mutex);
00093 m_socket_ptr->async_connect(m_remote_endpoint, [this](boost::system::error_code ec) {
00094 if (ec != 0)
00095 {
00096 ROS_ERROR("TCP error code: %i", ec.value());
00097 }
00098 else
00099 {
00100 ROS_INFO("TCP connection successfully established.");
00101 }
00102 m_connect_condition.notify_all();
00103 });
00104
00105 m_connect_condition.wait(lock_connect);
00106 }
00107
00108
00109 void AsyncTCPClient::doSendAndReceive(
00110 const sick::datastructure::PacketBuffer::VectorBuffer& sendBuffer)
00111 {
00112 boost::mutex::scoped_lock lock(m_socket_mutex);
00113 if (!m_socket_ptr)
00114 {
00115 return;
00116 }
00117 boost::asio::async_write(*m_socket_ptr,
00118 boost::asio::buffer(sendBuffer),
00119 [this](boost::system::error_code ec, std::size_t bytes_send) {
00120 this->handleSendAndReceive(ec, bytes_send);
00121 });
00122 }
00123
00124 void AsyncTCPClient::initiateReceive()
00125 {
00126 boost::mutex::scoped_lock lock(m_socket_mutex);
00127 if (!m_socket_ptr)
00128 {
00129 return;
00130 }
00131 m_socket_ptr->async_read_some(boost::asio::buffer(m_recv_buffer),
00132 [this](boost::system::error_code ec, std::size_t bytes_recvd) {
00133 this->handleReceive(ec, bytes_recvd);
00134 });
00135 }
00136
00137 void AsyncTCPClient::setPacketHandler(const PacketHandler& packet_handler)
00138 {
00139 m_packet_handler = packet_handler;
00140 }
00141
00142 void AsyncTCPClient::handleSendAndReceive(const boost::system::error_code& error,
00143 const std::size_t& bytes_transferred)
00144 {
00145
00146 if (!error || error == boost::asio::error::message_size)
00147 {
00148 initiateReceive();
00149 }
00150 else
00151 {
00152 ROS_ERROR("Error in tcp handle send and receive: %i", error.value());
00153 }
00154 }
00155
00156
00157 void AsyncTCPClient::startReceive() {}
00158
00159 void AsyncTCPClient::handleReceive(const boost::system::error_code& error,
00160 const std::size_t& bytes_transferred)
00161 {
00162 if (!error)
00163 {
00164 sick::datastructure::PacketBuffer packet_buffer(m_recv_buffer, bytes_transferred);
00165 m_packet_handler(packet_buffer);
00166 }
00167 else
00168 {
00169 ROS_ERROR("Error in tcp handle receive: %i", error.value());
00170 }
00171 }
00172
00173
00174 }
00175 }