41 #include <boost/bind.hpp> 49 message_handler_(message_handler),
52 shutdown_requested_(false),
53 write_in_progress_(false)
127 boost::asio::placeholders::error,
128 boost::asio::placeholders::bytes_transferred));
164 boost::asio::placeholders::error,
165 boost::asio::placeholders::bytes_transferred));
185 buffer.
pos += bytes_transferred;
199 std::list<ReadBuffer> local_queue;
214 local_queue.splice(local_queue.end(),
read_queue_);
221 while (!local_queue.empty())
228 for (std::reference_wrapper<CommListener> listener_ref :
listeners_)
230 listener_ref.get().receive_callback(buffer.
data, buffer.
len);
232 local_queue.pop_front();
std::thread callback_thread_
std::vector< std::reference_wrapper< CommListener > > listeners_
Abstract base class for getting comm events via a listener interface.
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_
uint8_t read_buffer_[READ_BUFFER_SIZE]
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.
MessageHandler & message_handler_
virtual void error(const std::string &message)=0
std::function< void(const uint8_t *, size_t)> receive_callback_
bool init()
Initializes and opens the port.
std::lock_guard< std::recursive_mutex > mutex_lock
static DefaultMessageHandler default_message_handler_
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_
Comm(MessageHandler &message_handler=default_message_handler_)
Set up asynchronous communication base class.
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_
Abstract base class for message handler.
std::list< ReadBuffer > read_queue_
void register_listener(CommListener &listener)
Register a listener for when bytes are received on the port.
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_
virtual void do_close()=0