75 : m_server_adress(server_adress), m_tcp_port(tcp_port), m_ioservice(), m_tcp_socket(m_ioservice),
76 m_receive_timeout(default_receive_timeout), m_receiver_thread_running(false), m_receiver_thread(0)
86 m_receiver_thread_running =
false;
97 return m_tcp_socket.connect(m_ioservice, m_server_adress, m_tcp_port);
108 m_tcp_socket.close();
112 catch(std::exception & exc)
114 ROS_WARN_STREAM(
"ColaTransmitter::closeTcpConnections(): exception " << exc.what() <<
" on closing connection.");
127 return send(m_tcp_socket.socket(),
data, send_timestamp);
142 if (ROS::ok() && socket.is_open())
144 boost::system::error_code errorcode;
146 size_t bytes_written = boost::asio::write(socket, boost::asio::buffer(
data.data(),
data.size()), boost::asio::transfer_exactly(
data.size()), errorcode);
147 if (!errorcode && bytes_written ==
data.size())
149 ROS_WARN_STREAM(
"## ERROR ColaTransmitter::send: tcp socket write error, " << bytes_written <<
" of " <<
data.size() <<
" bytes written, errorcode " << errorcode.value() <<
" \"" << errorcode.message() <<
"\"");
152 catch(std::exception & exc)
154 ROS_WARN_STREAM(
"## ERROR ColaTransmitter::send(): exception " << exc.what());
168 return receive(m_tcp_socket.socket(), telegram,
timeout, receive_timestamp);
182 telegram.reserve(1024);
187 while (ROS::ok() && socket.is_open())
190 uint8_t byte_received = 0;
191 boost::system::error_code errorcode;
193 if (socket.available() > 0 &&
boost::asio::read(socket, boost::asio::buffer(&byte_received, 1), boost::asio::transfer_exactly(1), errorcode) > 0 && !errorcode)
195 if (telegram.empty())
197 telegram.push_back(byte_received);
207 if(telegram_length > 0 && telegram.size() >= telegram_length)
213 if (dataEndWithETX(telegram, binETX))
224 catch(std::exception & exc)
226 ROS_WARN_STREAM(
"## ERROR ColaTransmitter::receive(): exception " << exc.what());
239 if(
data.size() < etx.size())
241 for(
size_t n =
data.size() - etx.size(), m = 0; m < etx.size(); n++, m++)
243 if(
data[n] != etx[m])
256 stopReceiverThread();
257 m_receiver_thread_running =
true;
268 m_receiver_thread_running =
false;
269 if(m_receiver_thread)
271 m_receiver_thread->join();
272 delete(m_receiver_thread);
273 m_receiver_thread = 0;
291 while(ROS::ok() && m_receiver_thread_running && m_response_fifo.empty())
294 m_response_fifo.waitOnceForElement();
298 if(!m_response_fifo.empty())
302 receive_timestamp =
response.receive_timestamp;
313 while(ROS::ok() && m_receiver_thread_running)
316 if(receive(
response.telegram_data, m_receive_timeout,
response.receive_timestamp))
319 m_response_fifo.notify();