45 #include <boost/bind.hpp> 46 #include <boost/asio.hpp> 47 #include <boost/signals2/signal.hpp> 48 #include <boost/thread/mutex.hpp> 57 typedef boost::signals2::signal<void (const char *data, const size_t size, Any_Session *writer)>
SIG_ON_DATA;
68 virtual void write(
const std::string &buffer)=0;
90 boost::asio::ip::tcp::socket&
socket()
95 virtual bool connect(
const std::string &path,
const std::string &service)
97 boost::asio::ip::tcp::resolver resolver(socket_.get_io_service());
98 boost::asio::ip::tcp::resolver::query query(path, service);
99 boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
100 boost::asio::ip::tcp::resolver::iterator end;
102 while (endpoint_iterator != end) {
103 if(connect(*endpoint_iterator))
110 virtual bool connect(
const boost::asio::ip::tcp::resolver::endpoint_type &ep)
114 boost::system::error_code error = boost::asio::error::host_not_found;
115 socket_.connect(ep, error);
121 socket_.async_read_some(boost::asio::buffer(data_, max_length),
123 boost::asio::placeholders::error,
124 boost::asio::placeholders::bytes_transferred));
129 if(socket_.is_open()) socket_.close();
130 boost::mutex::scoped_lock lock(mtx_);
136 virtual void write(
const std::string &buffer) {
137 boost::asio::async_write(socket_,
138 boost::asio::buffer( buffer ),
140 boost::asio::placeholders::error));
142 virtual void write(
const std::vector<char> &buffer) {
143 boost::asio::async_write(socket_,
144 boost::asio::buffer( buffer ),
146 boost::asio::placeholders::error));
150 virtual void write(
const boost::asio::mutable_buffers_1 &buffer) {
151 boost::asio::async_write(socket_,
154 boost::asio::placeholders::error));
161 size_t bytes_transferred)
167 on_data_(data_, bytes_transferred,
this);
171 ROS_ERROR(
"error while reading from socket");
175 if(socket_.is_open())
176 socket_.async_read_some(boost::asio::buffer(data_, max_length),
178 boost::asio::placeholders::error,
179 boost::asio::placeholders::bytes_transferred));
189 ROS_ERROR(
"error while writing to socket");
196 enum { max_length = 4096 };
197 char data_[max_length];
boost::mutex mtx_
lock operations
Any_Session(SIG_ON_DATA &cb)
every session needs a data handler
abstract connection handler to process incoming data (independent of protocol)
virtual void write(const boost::asio::mutable_buffers_1 &buffer)
write async. to TCP socket
virtual bool connect(const boost::asio::ip::tcp::resolver::endpoint_type &ep)
void handle_write(const boost::system::error_code &error)
called if data is written (error handling)
boost::signals2::signal< void(const char *data, const size_t size, Any_Session *writer)> SIG_ON_DATA
incoming data with "data" of size "size" and handler to write back ("writer")
boost::asio::ip::tcp::socket & socket()
getter for socket connection
void handle_read(const boost::system::error_code &error, size_t bytes_transferred)
called on incoming data –> decides if an error occured or calls handler
TCP_Session(boost::asio::io_service &io_service, SIG_ON_DATA &cb)
virtual void write(const std::string &buffer)
write async. to TCP socket (implement abstract)
SIG_ON_DATA & on_data_
signal handler for incoming data
virtual void write(const std::vector< char > &buffer)
virtual bool connect(const std::string &path, const std::string &service)
boost::asio::ip::tcp::socket socket_
TCP socket.
implementation connection handler for TCP
virtual void write(const std::string &buffer)=0
write a string to the network connection