network.h
Go to the documentation of this file.
1 
41  #pragma once
42 
43 #include <cstdlib>
44 #include <iostream>
45 #include <boost/bind.hpp>
46 #include <boost/asio.hpp>
47 #include <boost/signals2/signal.hpp>
48 #include <boost/thread/mutex.hpp>
49 
50 #include <ros/console.h> //for ROS_ERROR, ROS_INFO, ...
51 
54 {
55 public:
57  typedef boost::signals2::signal<void (const char *data, const size_t size, Any_Session *writer)> SIG_ON_DATA;
58 
60  Any_Session(SIG_ON_DATA &cb)
61  : on_data_(cb)
62  {
63  }
64 
65  virtual ~Any_Session() {}
66 
68  virtual void write(const std::string &buffer)=0;
69 
70 protected:
71 
72  SIG_ON_DATA &on_data_;
73 };
74 
75 
77 class TCP_Session : public Any_Session
78 {
79 public:
80 
81  TCP_Session(boost::asio::io_service& io_service, SIG_ON_DATA &cb)
82  : Any_Session(cb), socket_(io_service)
83  {
84  }
85 
86  virtual ~TCP_Session() {
87  }
88 
90  boost::asio::ip::tcp::socket& socket()
91  {
92  return socket_;
93  }
94 
95  virtual bool connect(const std::string &path, const std::string &service)
96  {
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;
101 
102  while (endpoint_iterator != end) {
103  if(connect(*endpoint_iterator))
104  return true;
105  }
106 
107  return false;
108  }
109 
110  virtual bool connect(const boost::asio::ip::tcp::resolver::endpoint_type &ep)
111  {
112  close();
113 
114  boost::system::error_code error = boost::asio::error::host_not_found;
115  socket_.connect(ep, error);
116  if (error) {
117  ROS_ERROR("connecting failed");
118  return false;//throw boost::system::system_error(error);
119  }
120 
121  socket_.async_read_some(boost::asio::buffer(data_, max_length),
122  boost::bind(&TCP_Session::handle_read, this,
123  boost::asio::placeholders::error,
124  boost::asio::placeholders::bytes_transferred));
125  return true;
126  }
127 
128  virtual void close() {
129  if(socket_.is_open()) socket_.close();
130  boost::mutex::scoped_lock lock(mtx_);
131  }
132 
133 protected:
134 
136  virtual void write(const std::string &buffer) {
137  boost::asio::async_write(socket_,
138  boost::asio::buffer( buffer ),
139  boost::bind(&TCP_Session::handle_write, this,
140  boost::asio::placeholders::error));
141  }
142  virtual void write(const std::vector<char> &buffer) {
143  boost::asio::async_write(socket_,
144  boost::asio::buffer( buffer ),
145  boost::bind(&TCP_Session::handle_write, this,
146  boost::asio::placeholders::error));
147  }
148 
150  virtual void write(const boost::asio::mutable_buffers_1 &buffer) {
151  boost::asio::async_write(socket_,
152  buffer,
153  boost::bind(&TCP_Session::handle_write, this,
154  boost::asio::placeholders::error));
155  }
156 
157 private:
158 
160  void handle_read(const boost::system::error_code& error,
161  size_t bytes_transferred)
162  {
163  //boost::mutex::scoped_lock lock(mtx_);
164 
165  if (!error)
166  {
167  on_data_(data_, bytes_transferred, this);
168  }
169  else
170  {
171  ROS_ERROR("error while reading from socket");
172  delete this;
173  }
174 
175  if(socket_.is_open())
176  socket_.async_read_some(boost::asio::buffer(data_, max_length),
177  boost::bind(&TCP_Session::handle_read, this,
178  boost::asio::placeholders::error,
179  boost::asio::placeholders::bytes_transferred));
180  }
181 
183  void handle_write(const boost::system::error_code& error)
184  {
185  //boost::mutex::scoped_lock lock(mtx_);
186 
187  if (error)
188  {
189  ROS_ERROR("error while writing to socket");
190  delete this;
191  }
192  }
193 
194 protected:
195  boost::asio::ip::tcp::socket socket_;
196  enum { max_length = 4096 };
197  char data_[max_length];
198  boost::mutex mtx_;
199 };
boost::mutex mtx_
lock operations
Definition: network.h:198
virtual ~TCP_Session()
Definition: network.h:86
Any_Session(SIG_ON_DATA &cb)
every session needs a data handler
Definition: network.h:60
abstract connection handler to process incoming data (independent of protocol)
Definition: network.h:53
virtual void write(const boost::asio::mutable_buffers_1 &buffer)
write async. to TCP socket
Definition: network.h:150
virtual bool connect(const boost::asio::ip::tcp::resolver::endpoint_type &ep)
Definition: network.h:110
void handle_write(const boost::system::error_code &error)
called if data is written (error handling)
Definition: network.h:183
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")
Definition: network.h:57
boost::asio::ip::tcp::socket & socket()
getter for socket connection
Definition: network.h:90
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
Definition: network.h:160
TCP_Session(boost::asio::io_service &io_service, SIG_ON_DATA &cb)
Definition: network.h:81
virtual void write(const std::string &buffer)
write async. to TCP socket (implement abstract)
Definition: network.h:136
SIG_ON_DATA & on_data_
signal handler for incoming data
Definition: network.h:72
virtual void write(const std::vector< char > &buffer)
Definition: network.h:142
virtual bool connect(const std::string &path, const std::string &service)
Definition: network.h:95
boost::asio::ip::tcp::socket socket_
TCP socket.
Definition: network.h:195
#define ROS_ERROR(...)
implementation connection handler for TCP
Definition: network.h:77
virtual ~Any_Session()
Definition: network.h:65
virtual void write(const std::string &buffer)=0
write a string to the network connection
virtual void close()
Definition: network.h:128


sick_visionary_t_driver
Author(s): Joshua Hampp
autogenerated on Mon Jun 10 2019 15:09:27