connection.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The urg_stamped Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SCIP2_CONNECTION_H
18 #define SCIP2_CONNECTION_H
19 
20 #include <cstdint>
21 #include <memory>
22 #include <string>
23 
24 #include <boost/asio.hpp>
25 #include <boost/bind/bind.hpp>
26 #include <boost/function.hpp>
27 
28 #include <scip2/logger.h>
29 
30 namespace scip2
31 {
32 class Protocol;
34 {
35  friend class Protocol;
36 
37 protected:
38  using CallbackConnect = boost::function<void(void)>;
39  using CallbackClose = boost::function<void(void)>;
40  using CallbackReceive = boost::function<void(
41  boost::asio::streambuf&, const boost::posix_time::ptime&)>;
42  using CallbackSend = boost::function<void(
43  const boost::posix_time::ptime&)>;
44 
48 
49  void close()
50  {
51  if (cb_close_)
52  cb_close_();
53  }
54  void connect()
55  {
56  if (cb_connect_)
57  cb_connect_();
58  }
59  void receive(
60  boost::asio::streambuf& buf,
61  const boost::posix_time::ptime& time_read)
62  {
63  if (cb_receive_)
64  cb_receive_(buf, time_read);
65  }
66 
67 public:
68  using Ptr = std::shared_ptr<Connection>;
69 
70  virtual void spin() = 0;
71  virtual void stop() = 0;
72  virtual void send(const std::string&, CallbackSend = CallbackSend()) = 0;
73  virtual void startWatchdog(const boost::posix_time::time_duration&) = 0;
74 
76  {
77  cb_close_ = cb;
78  }
80  {
81  cb_receive_ = cb;
82  }
84  {
85  cb_connect_ = cb;
86  }
88  {
89  }
90 };
91 
92 class ConnectionTcp : public Connection
93 {
94 protected:
95  boost::asio::io_service io_;
96  boost::asio::ip::tcp::socket socket_;
97  boost::asio::streambuf buf_;
98  boost::asio::deadline_timer timeout_;
99  boost::asio::deadline_timer watchdog_;
100  boost::posix_time::time_duration watchdog_duration_;
101 
103  {
104  if (watchdog_duration_ == boost::posix_time::time_duration())
105  return;
106  watchdog_.cancel();
107 
108  watchdog_.expires_from_now(watchdog_duration_);
109  watchdog_.async_wait(
111  }
112  void onWatchdog(const boost::system::error_code& error)
113  {
114  if (!error)
115  {
116  logger::fatal() << "Watchdog timeout" << std::endl;
117  close();
118  }
119  }
120 
121  void onReceive(const boost::system::error_code& error)
122  {
123  const auto time_read = boost::posix_time::microsec_clock::universal_time();
124  if (error)
125  {
126  logger::fatal() << "Receive error" << std::endl;
127  close();
128  return;
129  }
130  clearWatchdog();
131  receive(buf_, time_read);
132  asyncRead();
133  }
134  void onSend(const boost::system::error_code& error, CallbackSend cb)
135  {
136  const auto time_send = boost::posix_time::microsec_clock::universal_time();
137  if (error)
138  {
139  logger::fatal() << "Send error" << std::endl;
140  close();
141  return;
142  }
143  if (cb)
144  cb(time_send);
145  }
146 
147  void asyncRead()
148  {
149  boost::asio::async_read_until(
150  socket_, buf_, "\n\n",
152  }
153  void onConnect(const boost::system::error_code& error)
154  {
155  if (error)
156  {
157  logger::fatal() << "Connection error" << std::endl;
158  close();
159  return;
160  }
161  socket_.set_option(boost::asio::ip::tcp::no_delay(true));
162  timeout_.cancel();
163  connect();
164  asyncRead();
165  }
166  void onConnectTimeout(const boost::system::error_code& error)
167  {
168  if (!error)
169  {
170  logger::fatal() << "Connection timeout" << std::endl;
171  close();
172  return;
173  }
174  }
175 
176 public:
177  using Ptr = std::shared_ptr<ConnectionTcp>;
178 
179  ConnectionTcp(const std::string& ip, const uint16_t port)
180  : socket_(io_)
181  , timeout_(io_)
182  , watchdog_(io_)
183  {
184  boost::asio::ip::tcp::endpoint endpoint(
185  boost::asio::ip::address::from_string(ip), port);
186  socket_.async_connect(
187  endpoint,
188  boost::bind(
191 
192  timeout_.expires_from_now(boost::posix_time::seconds(2));
193  timeout_.async_wait(
194  boost::bind(
197  }
198  void spin()
199  {
200  io_.run();
201  }
202  void stop()
203  {
204  socket_.close();
205  io_.stop();
206  }
207  void send(const std::string& data, CallbackSend cb = CallbackSend())
208  {
209  boost::shared_ptr<std::string> buf(new std::string(data));
210  boost::asio::async_write(
211  socket_, boost::asio::buffer(*buf),
212  boost::bind(
215  }
216  void startWatchdog(const boost::posix_time::time_duration& duration)
217  {
218  watchdog_duration_ = duration;
219  clearWatchdog();
220  }
221 };
222 
223 } // namespace scip2
224 
225 #endif // SCIP2_CONNECTION_H
scip2::Connection::registerCloseCallback
void registerCloseCallback(CallbackClose cb)
Definition: connection.h:75
scip2::ConnectionTcp::startWatchdog
void startWatchdog(const boost::posix_time::time_duration &duration)
Definition: connection.h:216
scip2::ConnectionTcp::watchdog_
boost::asio::deadline_timer watchdog_
Definition: connection.h:99
scip2::ConnectionTcp::onSend
void onSend(const boost::system::error_code &error, CallbackSend cb)
Definition: connection.h:134
scip2::ConnectionTcp::send
void send(const std::string &data, CallbackSend cb=CallbackSend())
Definition: connection.h:207
boost::shared_ptr
scip2::ConnectionTcp::onConnect
void onConnect(const boost::system::error_code &error)
Definition: connection.h:153
logger.h
scip2::Connection::cb_close_
CallbackClose cb_close_
Definition: connection.h:46
scip2::Connection::registerReceiveCallback
void registerReceiveCallback(CallbackReceive cb)
Definition: connection.h:79
scip2::Connection::spin
virtual void spin()=0
scip2::ConnectionTcp::watchdog_duration_
boost::posix_time::time_duration watchdog_duration_
Definition: connection.h:100
scip2::ConnectionTcp::io_
boost::asio::io_service io_
Definition: connection.h:95
scip2::Connection::CallbackClose
boost::function< void(void)> CallbackClose
Definition: connection.h:39
scip2::ConnectionTcp::onReceive
void onReceive(const boost::system::error_code &error)
Definition: connection.h:121
scip2::ConnectionTcp::ConnectionTcp
ConnectionTcp(const std::string &ip, const uint16_t port)
Definition: connection.h:179
scip2::Connection::CallbackConnect
boost::function< void(void)> CallbackConnect
Definition: connection.h:38
scip2::Connection
Definition: connection.h:33
scip2::ConnectionTcp::stop
void stop()
Definition: connection.h:202
scip2::Connection::close
void close()
Definition: connection.h:49
scip2::logger::fatal
std::ostream & fatal()
Definition: logger.cpp:109
scip2
Definition: connection.h:30
scip2::ConnectionTcp::asyncRead
void asyncRead()
Definition: connection.h:147
scip2::Connection::stop
virtual void stop()=0
scip2::ConnectionTcp::socket_
boost::asio::ip::tcp::socket socket_
Definition: connection.h:96
scip2::ConnectionTcp::timeout_
boost::asio::deadline_timer timeout_
Definition: connection.h:98
scip2::Connection::CallbackReceive
boost::function< void(boost::asio::streambuf &, const boost::posix_time::ptime &)> CallbackReceive
Definition: connection.h:41
scip2::Connection::receive
void receive(boost::asio::streambuf &buf, const boost::posix_time::ptime &time_read)
Definition: connection.h:59
scip2::Connection::cb_receive_
CallbackReceive cb_receive_
Definition: connection.h:47
scip2::Connection::send
virtual void send(const std::string &, CallbackSend=CallbackSend())=0
scip2::ConnectionTcp
Definition: connection.h:92
scip2::Connection::registerConnectCallback
void registerConnectCallback(CallbackConnect cb)
Definition: connection.h:83
scip2::Connection::Ptr
std::shared_ptr< Connection > Ptr
Definition: connection.h:68
scip2::ConnectionTcp::onConnectTimeout
void onConnectTimeout(const boost::system::error_code &error)
Definition: connection.h:166
scip2::Connection::Connection
Connection()
Definition: connection.h:87
scip2::Connection::connect
void connect()
Definition: connection.h:54
scip2::ConnectionTcp::clearWatchdog
void clearWatchdog()
Definition: connection.h:102
scip2::Protocol
Definition: protocol.h:33
scip2::Connection::cb_connect_
CallbackConnect cb_connect_
Definition: connection.h:45
scip2::Connection::startWatchdog
virtual void startWatchdog(const boost::posix_time::time_duration &)=0
scip2::logger::error
std::ostream & error()
Definition: logger.cpp:105
scip2::Connection::CallbackSend
boost::function< void(const boost::posix_time::ptime &)> CallbackSend
Definition: connection.h:43
scip2::ConnectionTcp::spin
void spin()
Definition: connection.h:198
scip2::ConnectionTcp::onWatchdog
void onWatchdog(const boost::system::error_code &error)
Definition: connection.h:112
scip2::ConnectionTcp::buf_
boost::asio::streambuf buf_
Definition: connection.h:97


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Wed Dec 18 2024 03:10:57