38 #ifndef ASYNC_COMM_COMM_H 39 #define ASYNC_COMM_COMM_H 41 #include <condition_variable> 49 #include <boost/asio.hpp> 50 #include <boost/function.hpp> 81 void send_bytes(
const uint8_t * src,
size_t len);
112 virtual void do_async_read(
const boost::asio::mutable_buffers_1 &buffer,
113 boost::function<
void(
const boost::system::error_code&,
size_t)> handler) = 0;
114 virtual void do_async_write(
const boost::asio::const_buffers_1 &buffer,
115 boost::function<
void(
const boost::system::error_code&,
size_t)> handler) = 0;
128 assert(len <= READ_BUFFER_SIZE);
129 memcpy(data, buf, len);
143 assert(len <= WRITE_BUFFER_SIZE);
144 memcpy(data, buf, len);
147 const uint8_t *
dpos()
const {
return data + pos; }
149 size_t nbytes()
const {
return len - pos; }
155 void async_read_end(
const boost::system::error_code& error,
size_t bytes_transferred);
158 void async_write_end(
const boost::system::error_code& error,
size_t bytes_transferred);
181 #endif // ASYNC_COMM_COMM_H std::thread callback_thread_
virtual void do_async_read(const boost::asio::mutable_buffers_1 &buffer, boost::function< void(const boost::system::error_code &, size_t)> handler)=0
static constexpr size_t WRITE_BUFFER_SIZE
boost::asio::io_service io_service_
WriteBuffer(const uint8_t *buf, size_t len)
uint8_t read_buffer_[READ_BUFFER_SIZE]
Abstract base class for an asynchronous communication port.
void async_read_end(const boost::system::error_code &error, size_t bytes_transferred)
void send_bytes(const uint8_t *src, size_t len)
Send bytes from a buffer over the port.
bool init()
Initializes and opens the port.
std::lock_guard< std::recursive_mutex > mutex_lock
void send_byte(uint8_t data)
Send a single byte over the port.
void register_receive_callback(std::function< void(const uint8_t *, size_t)> fun)
Register a callback function for when bytes are received on the port.
std::list< WriteBuffer > write_queue_
std::recursive_mutex write_mutex_
void async_write(bool check_write_state)
void close()
Closes the port.
const uint8_t * dpos() const
static constexpr size_t READ_BUFFER_SIZE
void async_write_end(const boost::system::error_code &error, size_t bytes_transferred)
std::condition_variable condition_variable_
ReadBuffer(const uint8_t *buf, size_t len)
std::list< ReadBuffer > read_queue_
std::mutex mutex
mutex for synchronization between the main thread and callback thread
std::function< void(const uint8_t *, size_t)> receive_callback_
virtual void do_async_write(const boost::asio::const_buffers_1 &buffer, boost::function< void(const boost::system::error_code &, size_t)> handler)=0
uint8_t data[READ_BUFFER_SIZE]
std::mutex callback_mutex_
std::condition_variable condition_variable
condition variable used to suspend main thread until all messages have been received back ...
virtual void do_close()=0