udp_transport.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
4 
6 
7 UDPTransport::UDPTransport(std::string address, std::string port) : Transport(address, transport_type::udp)
8 {
9  io_service_ = std::make_shared<boost::asio::io_service>();
10  udp::endpoint local_endpoint = local_endpoint = udp::endpoint(udp::v4(), stoi(port));
11 
12  socket_ = std::make_unique<udp::socket>(*io_service_, local_endpoint);
13  timer_ = std::make_shared<boost::asio::deadline_timer>(*io_service_.get());
14 }
15 
17 {
18  disconnect();
19 }
20 
22 {
23  udp::endpoint udp_endpoint = udp::endpoint(boost::asio::ip::address::from_string(address_), 0);
24  socket_->connect(udp_endpoint);
25  port_ = std::to_string(socket_->local_endpoint().port());
26  host_ip_ = socket_->local_endpoint().address().to_string();
27 
28  is_connected_ = true;
29  return true;
30 }
31 
33 {
34  socket_->close();
35  return true;
36 }
37 
38 bool UDPTransport::read(boost::array<uint8_t, 4096>& buf, size_t& len)
39 {
40  boost::system::error_code error;
41  udp::endpoint sender_endpoint;
42  len = socket_->receive_from(boost::asio::buffer(buf), sender_endpoint);
43  if (error == boost::asio::error::eof)
44  return false; // Connection closed cleanly by peer.
45  else if (error)
46  return false;
47  return true;
48 }
49 
50 // https://stackoverflow.com/questions/13126776/asioread-with-timeout
51 bool UDPTransport::readWithTimeout(boost::array<uint8_t, 4096>& buf, size_t& len, const uint32_t expiry_time)
52 {
53  timer_->expires_from_now(boost::posix_time::seconds(expiry_time));
54  timer_->async_wait([this, &expiry_time](const boost::system::error_code& error) {
55  timer_result_.reset(error);
56  if (error.message() == "Success")
57  {
58  std::cout << "Time out: No packets received in " << expiry_time << " seconds" << std::endl;
59  }
60  });
61 
62  boost::optional<boost::system::error_code> read_result;
63  udp::endpoint sender_endpoint;
64 
65  socket_->async_receive_from(boost::asio::buffer(buf), sender_endpoint,
66  [&len, &read_result](const boost::system::error_code& error, size_t received) {
67  len = received;
68  read_result.reset(error);
69  });
70  bool success = false;
71  while (io_service_->run_one())
72  {
73  if (read_result && read_result->value() == 0)
74  {
75  // packets received so cancel timer
76  timer_->cancel();
77  success = true;
78  }
79  else if (timer_result_)
80  {
81  // timeout
82  socket_->cancel();
83  success = false;
84  }
85  }
86  io_service_->reset();
87  return success;
88 }
bool is_connected_
Definition: transport.h:69
virtual bool read(boost::array< uint8_t, 4096 > &buf, size_t &len)
virtual bool connect()
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 disconnect()
std::string host_ip_
Definition: transport.h:67
UDPTransport(std::string address, std::string port="0")
std::string address_
Definition: transport.h:66
transport_type
def error(args, kwargs)
std::string port_
Definition: transport.h:68
std::shared_ptr< boost::asio::io_service > io_service_
Definition: transport.h:71
std::unique_ptr< boost::asio::ip::udp::socket > socket_
Definition: udp_transport.h:32
virtual bool readWithTimeout(boost::array< uint8_t, 4096 > &buf, size_t &len, const uint32_t expiry_time)


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