udp_interface.cpp
Go to the documentation of this file.
1 /*
2 * Unpublished Copyright (c) 2009-2017 AutonomouStuff, LLC, All Rights Reserved.
3 *
4 * This file is part of the network_interface ROS 1.0 driver which is released under the MIT license.
5 * See file LICENSE included with this software or go to https://opensource.org/licenses/MIT for full license details.
6 */
7 
8 #include <network_interface.h>
9 
10 using namespace AS::Network; // NOLINT
11 using boost::asio::ip::udp;
12 
13 // Default constructor.
15  socket_(io_service_)
16 {
17 }
18 
19 // Default destructor.
21 {
22 }
23 
24 return_statuses UDPInterface::open(const char *ip_address, const int &port)
25 {
26  if (socket_.is_open())
27  return OK;
28 
29  std::stringstream sPort;
30  sPort << port;
31  udp::resolver res(io_service_);
32  udp::resolver::query query(udp::v4(), ip_address, sPort.str());
33  sender_endpoint_ = *res.resolve(query);
34  boost::system::error_code ec;
35 
36  socket_.connect(sender_endpoint_, ec);
37 
38  if (ec.value() == boost::system::errc::success)
39  {
40  return OK;
41  }
42  else if (ec.value() == boost::asio::error::invalid_argument)
43  {
44  return BAD_PARAM;
45  }
46  else
47  {
48  close();
49  return INIT_FAILED;
50  }
51 }
52 
54 {
55  if (!socket_.is_open())
56  return SOCKET_CLOSED;
57 
58  boost::system::error_code ec;
59  socket_.close(ec);
60 
61  if (ec.value() == boost::system::errc::success)
62  {
63  return OK;
64  }
65  else
66  {
67  return CLOSE_FAILED;
68  }
69 }
70 
72 {
73  return socket_.is_open();
74 }
75 
77  const size_t &buf_size,
78  size_t &bytes_read)
79 {
80  if (!socket_.is_open())
81  return SOCKET_CLOSED;
82 
83  boost::system::error_code ec;
84  bytes_read = socket_.receive_from(boost::asio::buffer(msg, buf_size), sender_endpoint_, 0, ec);
85 
86  if (ec.value() == boost::system::errc::success)
87  {
88  return OK;
89  }
90  else
91  {
92  return READ_FAILED;
93  }
94 }
95 
96 return_statuses UDPInterface::write(unsigned char *msg, const size_t &msg_size)
97 {
98  if (!socket_.is_open())
99  return SOCKET_CLOSED;
100 
101  boost::system::error_code ec;
102  socket_.send_to(boost::asio::buffer(msg, msg_size), sender_endpoint_, 0, ec);
103 
104  if (ec.value() == boost::system::errc::success)
105  {
106  return OK;
107  }
108  else
109  {
110  return WRITE_FAILED;
111  }
112 }
boost::asio::io_service io_service_
return_statuses write(unsigned char *msg, const size_t &msg_size)
return_statuses read(unsigned char *msg, const size_t &buf_size, size_t &bytes_read)
return_statuses close()
boost::asio::ip::udp::endpoint sender_endpoint_
boost::asio::ip::udp::socket socket_
return_statuses open(const char *ip_address, const int &port)


network_interface
Author(s): Joshua Whitley , Daniel Stanek , Joe Kale
autogenerated on Sat Sep 7 2019 03:30:10