17 #ifndef SCIP2_CONNECTION_H 18 #define SCIP2_CONNECTION_H 23 #include <boost/asio.hpp> 24 #include <boost/bind/bind.hpp> 39 boost::asio::streambuf&,
const boost::posix_time::ptime&)>;
41 const boost::posix_time::ptime&)>;
58 boost::asio::streambuf& buf,
59 const boost::posix_time::ptime& time_read)
66 using Ptr = std::shared_ptr<Connection>;
68 virtual void spin() = 0;
69 virtual void stop() = 0;
71 virtual void startWatchdog(
const boost::posix_time::time_duration&) = 0;
93 boost::asio::io_service
io_;
95 boost::asio::streambuf
buf_;
102 if (watchdog_duration_ == boost::posix_time::time_duration())
106 watchdog_.expires_from_now(watchdog_duration_);
107 watchdog_.async_wait(
121 const auto time_read = boost::posix_time::microsec_clock::universal_time();
134 const auto time_send = boost::posix_time::microsec_clock::universal_time();
147 boost::asio::async_read_until(
148 socket_, buf_,
"\n\n",
174 using Ptr = std::shared_ptr<ConnectionTcp>;
181 boost::asio::ip::tcp::endpoint endpoint(
182 boost::asio::ip::address::from_string(ip), port);
183 socket_.async_connect(
189 timeout_.expires_from_now(boost::posix_time::seconds(2));
206 boost::asio::async_write(
207 socket_, boost::asio::buffer(*buf),
214 watchdog_duration_ = duration;
221 #endif // SCIP2_CONNECTION_H virtual void send(const std::string &, CallbackSend=CallbackSend())=0
boost::asio::deadline_timer watchdog_
std::shared_ptr< Connection > Ptr
void receive(boost::asio::streambuf &buf, const boost::posix_time::ptime &time_read)
void onConnectTimeout(const boost::system::error_code &error)
void onConnect(const boost::system::error_code &error)
void onSend(const boost::system::error_code &error, CallbackSend cb)
void registerConnectCallback(CallbackConnect cb)
ConnectionTcp(const std::string &ip, const uint16_t port)
void startWatchdog(const boost::posix_time::time_duration &duration)
virtual void startWatchdog(const boost::posix_time::time_duration &)=0
void send(const std::string &data, CallbackSend cb=CallbackSend())
boost::function< void(const boost::posix_time::ptime &)> CallbackSend
void registerReceiveCallback(CallbackReceive cb)
void onReceive(const boost::system::error_code &error)
boost::function< void(boost::asio::streambuf &, const boost::posix_time::ptime &)> CallbackReceive
boost::function< void(void)> CallbackConnect
boost::asio::ip::tcp::socket socket_
boost::function< void(void)> CallbackClose
void registerCloseCallback(CallbackClose cb)
void onWatchdog(const boost::system::error_code &error)
boost::asio::streambuf buf_
CallbackConnect cb_connect_
boost::asio::deadline_timer timeout_
boost::asio::io_service io_
boost::posix_time::time_duration watchdog_duration_
CallbackReceive cb_receive_