29 #ifndef UBLOX_GPS_ASYNC_WORKER_H 30 #define UBLOX_GPS_ASYNC_WORKER_H 34 #include <boost/asio.hpp> 35 #include <boost/bind.hpp> 36 #include <boost/format.hpp> 37 #include <boost/thread.hpp> 38 #include <boost/thread/condition.hpp> 50 template <
typename StreamT>
64 std::size_t buffer_size = 8192);
84 bool send(
const unsigned char*
data,
const unsigned int size);
89 void wait(
const boost::posix_time::time_duration& timeout);
104 void readEnd(
const boost::system::error_code&, std::size_t);
121 std::vector<unsigned char>
in_;
127 std::vector<unsigned char>
out_;
137 template <
typename StreamT>
140 std::size_t buffer_size)
144 in_.resize(buffer_size);
147 out_.reserve(buffer_size);
151 boost::bind(&boost::asio::io_service::run,
io_service_)));
154 template <
typename StreamT>
161 template <
typename StreamT>
163 const unsigned int size) {
166 ROS_ERROR(
"Ublox AsyncWorker::send: Size of message to send is 0");
170 if (
out_.capacity() -
out_.size() < size) {
171 ROS_ERROR(
"Ublox AsyncWorker::send: Output buffer too full to send message");
174 out_.insert(
out_.end(), data, data + size);
180 template <
typename StreamT>
184 if (
out_.size() == 0) {
188 boost::asio::write(*
stream_, boost::asio::buffer(
out_.data(),
out_.size()));
192 std::ostringstream oss;
193 for (std::vector<unsigned char>::iterator it =
out_.begin();
194 it !=
out_.end(); ++it)
195 oss << boost::format(
"%02x") %
static_cast<unsigned int>(*it) <<
" ";
196 ROS_DEBUG(
"U-Blox sent %li bytes: \n%s",
out_.size(), oss.str().c_str());
203 template <
typename StreamT>
210 boost::asio::placeholders::error,
211 boost::asio::placeholders::bytes_transferred));
214 template <
typename StreamT>
216 std::size_t bytes_transfered) {
219 ROS_ERROR(
"U-Blox ASIO input buffer read error: %s, %li",
220 error.message().c_str(),
222 }
else if (bytes_transfered > 0) {
225 unsigned char *pRawDataStart = &(*(
in_.begin() + (
in_buffer_size_ - bytes_transfered)));
226 std::size_t raw_data_stream_size = bytes_transfered;
232 std::ostringstream oss;
233 for (std::vector<unsigned char>::iterator it =
236 oss << boost::format(
"%02x") %
static_cast<unsigned int>(*it) <<
" ";
237 ROS_DEBUG(
"U-Blox received %li bytes \n%s", bytes_transfered,
251 template <
typename StreamT>
255 boost::system::error_code error;
259 "Error while closing the AsyncWorker stream: " << error.message());
262 template <
typename StreamT>
264 const boost::posix_time::time_duration& timeout) {
271 #endif // UBLOX_GPS_ASYNC_WORKER_H int debug
Used to determine which debug messages to display.
Mutex write_mutex_
Lock for the output buffer.
bool send(const unsigned char *data, const unsigned int size)
Send the data bytes via the I/O stream.
Callback write_callback_
Callback function to handle raw data.
boost::mutex::scoped_lock ScopedLock
boost::function< void(unsigned char *, std::size_t &)> Callback
boost::shared_ptr< boost::asio::io_service > io_service_
The I/O service.
void readEnd(const boost::system::error_code &, std::size_t)
Process messages read from the input stream.
bool stopping_
Whether or not the I/O service is closed.
boost::shared_ptr< StreamT > stream_
The I/O stream.
bool isOpen() const
Whether or not the I/O stream is open.
void doWrite()
Send all the data in the output buffer.
Handles I/O reading and writing.
boost::shared_ptr< boost::thread > background_thread_
void doRead()
Read the input stream.
boost::condition read_condition_
std::size_t in_buffer_size_
AsyncWorker(boost::shared_ptr< StreamT > stream, boost::shared_ptr< boost::asio::io_service > io_service, std::size_t buffer_size=8192)
Construct an Asynchronous I/O worker.
Handles Asynchronous I/O reading and writing.
void doClose()
Close the I/O stream.
std::vector< unsigned char > in_
The input buffer.
void setRawDataCallback(const Callback &callback)
Set the callback function which handles raw data.
void setCallback(const Callback &callback)
Set the callback function which handles input messages.
boost::condition write_condition_
Mutex read_mutex_
Lock for the input buffer.
#define ROS_ERROR_STREAM(args)
Callback read_callback_
Callback function to handle received messages.
void wait(const boost::posix_time::time_duration &timeout)
Wait for incoming messages.
std::vector< unsigned char > out_
The output buffer.