23 #ifndef ASIOLineBasedConnection20100715H 24 #define ASIOLineBasedConnection20100715H 26 #include "goby/common/logger.h" 27 #include "goby/common/time.h" 28 #include "interface.h" 44 virtual ASIOAsyncReadStream& socket() = 0;
48 async_read_until(socket(), buffer_, interface_->delimiter(),
49 boost::bind(&LineBasedConnection::read_complete,
this,
50 boost::asio::placeholders::error));
56 if (!(out_.front().has_dest() && out_.front().dest() != remote_endpoint()))
58 boost::asio::async_write(socket(), boost::asio::buffer(out_.front().data()),
59 boost::bind(&LineBasedConnection::write_complete,
this,
60 boost::asio::placeholders::error));
69 void read_complete(
const boost::system::error_code& error)
71 if (error == boost::asio::error::operation_aborted)
79 return socket_close(error);
82 std::istream is(&buffer_);
83 std::string& line = *in_datagram_.mutable_data();
85 if (!remote_endpoint().empty())
86 in_datagram_.set_src(remote_endpoint());
87 if (!local_endpoint().empty())
88 in_datagram_.set_dest(local_endpoint());
91 char last = interface_->delimiter().at(interface_->delimiter().length() - 1);
92 std::getline(is, line, last);
95 boost::mutex::scoped_lock lock(interface_->in_mutex());
96 interface_->in().push_back(in_datagram_);
101 void write_complete(
const boost::system::error_code& error)
103 if (error == boost::asio::error::operation_aborted)
112 return socket_close(error);
120 virtual void socket_close(
const boost::system::error_code& error) = 0;
122 virtual std::string local_endpoint() = 0;
123 virtual std::string remote_endpoint() = 0;
125 std::deque<protobuf::Datagram>& out() {
return out_; }
129 boost::asio::streambuf buffer_;
131 std::deque<protobuf::Datagram> out_;
double goby_time< double >()
Returns current UTC time as seconds and fractional seconds since 1970-01-01 00:00:00.
The global namespace for the Goby project.
basic interface class for all the derived serial (and networking mimics) line-based nodes (serial...