00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*- 00002 00003 // -- BEGIN LICENSE BLOCK ---------------------------------------------- 00004 00024 // -- END LICENSE BLOCK ------------------------------------------------ 00025 00026 //---------------------------------------------------------------------- 00033 //---------------------------------------------------------------------- 00034 00035 #ifndef SICK_SAFETYSCANNERS_COMMUNICATION_ASYNCTCPCLIENT_H 00036 #define SICK_SAFETYSCANNERS_COMMUNICATION_ASYNCTCPCLIENT_H 00037 00038 #include <ros/ros.h> 00039 00040 #include <functional> 00041 #include <iostream> 00042 #include <thread> 00043 00044 #include <boost/array.hpp> 00045 #include <boost/asio.hpp> 00046 #include <boost/cstdint.hpp> 00047 #include <boost/function.hpp> 00048 #include <boost/thread/condition.hpp> 00049 #include <boost/thread/mutex.hpp> 00050 00051 #include <sick_safetyscanners/datastructure/PacketBuffer.h> 00052 00053 namespace sick { 00054 namespace communication { 00055 00062 class AsyncTCPClient 00063 { 00064 public: 00069 typedef boost::function<void(const sick::datastructure::PacketBuffer&)> PacketHandler; 00070 00079 AsyncTCPClient(PacketHandler packet_handler, 00080 boost::asio::io_service& io_service, 00081 const boost::asio::ip::address_v4& server_ip, 00082 const uint16_t& server_port); 00083 00087 virtual ~AsyncTCPClient(); 00088 00092 void doConnect(); 00093 00097 void doDisconnect(); 00098 00104 void doSendAndReceive(const sick::datastructure::PacketBuffer::VectorBuffer& sendBuffer); 00105 00109 void initiateReceive(); 00110 00116 void setPacketHandler(const PacketHandler& packet_handler); 00117 00118 private: 00119 datastructure::PacketBuffer::ArrayBuffer m_recv_buffer; 00120 00121 PacketHandler m_packet_handler; 00122 00123 std::shared_ptr<boost::asio::io_service::work> m_io_work_ptr; 00124 boost::asio::io_service& m_io_service; 00125 std::shared_ptr<boost::asio::ip::tcp::socket> m_socket_ptr; 00126 boost::asio::ip::tcp::endpoint m_remote_endpoint; 00127 std::thread m_service_thread; 00128 00129 boost::condition m_connect_condition; 00130 boost::mutex m_connect_mutex; 00131 boost::mutex m_socket_mutex; 00132 00133 void startReceive(); 00134 void handleReceive(const boost::system::error_code& error, const std::size_t& bytes_transferred); 00135 00136 00137 AsyncTCPClient(AsyncTCPClient&); // block default copy constructor 00138 00139 void handleSendAndReceive(const boost::system::error_code& error, 00140 const std::size_t& bytes_transferred); 00141 }; 00142 } // namespace communication 00143 } // namespace sick 00144 00145 #endif // SICK_SAFETYSCANNERS_COMMUNICATION_ASYNCTCPCLIENT_H