tcp_transport.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
4 
6 
7 TCPTransport::TCPTransport(std::string address) : Transport(address, transport_type::tcp)
8 {
9  io_service_ = std::make_shared<boost::asio::io_service>();
10  socket_ = std::make_unique<tcp::socket>(*io_service_);
11  timer_ = std::make_shared<boost::asio::deadline_timer>(*io_service_.get());
12 }
13 
15 {
16  disconnect();
17 }
18 
20 {
21  try
22  {
23  tcp::resolver resolver(*io_service_);
24  tcp::resolver::query query(address_, port_);
25  tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
26  tcp::resolver::iterator end;
27 
28  boost::system::error_code error = boost::asio::error::host_not_found;
29  while (error && endpoint_iterator != end)
30  {
31  socket_->close();
32  socket_->connect(*endpoint_iterator++, error);
33  }
34  if (error)
35  {
36  throw boost::system::system_error(error);
37  }
38  }
39  catch (std::exception& e)
40  {
41  std::cerr << e.what() << std::endl;
42  return false;
43  }
44  is_connected_ = true;
45  return true;
46 }
47 
49 {
50  io_service_->stop();
51  socket_->close();
52  return true;
53 }
54 
55 bool TCPTransport::read(boost::array<uint8_t, 4096>& buf, size_t& len)
56 {
57  boost::system::error_code error;
58  len = socket_->read_some(boost::asio::buffer(buf), error);
59  if (error == boost::asio::error::eof)
60  return false; // Connection closed cleanly by peer.
61  else if (error)
62  return false;
63  return true;
64 }
65 
66 bool TCPTransport::readWithTimeout(boost::array<uint8_t, 4096>& buf, size_t& len, const uint32_t expiry_time)
67 {
68  timer_->expires_from_now(boost::posix_time::seconds(expiry_time));
69  timer_->async_wait([this, &expiry_time](const boost::system::error_code& error) {
70  timer_result_.reset(error);
71  if (error.message() == "Success")
72  {
73  std::cout << "Time out: No packets received in " << expiry_time << " seconds" << std::endl;
74  }
75  });
76 
77  boost::optional<boost::system::error_code> read_result;
78 
79  socket_->async_read_some(boost::asio::buffer(buf),
80  [&len, &read_result](const boost::system::error_code& error, size_t received) {
81  len = received;
82  read_result.reset(error);
83  });
84  bool success = false;
85  while (io_service_->run_one())
86  {
87  if (read_result && read_result->value() == 0)
88  {
89  // packets received so cancel timer
90  timer_->cancel();
91  success = true;
92  }
93  else if (timer_result_)
94  {
95  // timeout
96  socket_->cancel();
97  success = false;
98  }
99  }
100  io_service_->reset();
101  return success;
102 }
bool is_connected_
Definition: transport.h:69
boost::optional< boost::system::error_code > timer_result_
Definition: transport.h:73
std::shared_ptr< boost::asio::deadline_timer > timer_
Definition: transport.h:72
virtual bool readWithTimeout(boost::array< uint8_t, 4096 > &buf, size_t &len, const uint32_t expiry_time)
std::string address_
Definition: transport.h:66
transport_type
def error(args, kwargs)
std::string port_
Definition: transport.h:68
virtual bool read(boost::array< uint8_t, 4096 > &buf, size_t &len)
TCPTransport(std::string address)
std::shared_ptr< boost::asio::io_service > io_service_
Definition: transport.h:71
virtual bool connect()
virtual bool disconnect()
std::unique_ptr< boost::asio::ip::tcp::socket > socket_
Definition: tcp_transport.h:32


pf_driver
Author(s): Harsh Deshpande
autogenerated on Fri Feb 24 2023 03:59:35