udp_interface.cpp
Go to the documentation of this file.
00001 /*
00002 * Unpublished Copyright (c) 2009-2017 AutonomouStuff, LLC, All Rights Reserved.
00003 *
00004 * This file is part of the network_interface ROS 1.0 driver which is released under the MIT license.
00005 * See file LICENSE included with this software or go to https://opensource.org/licenses/MIT for full license details.
00006 */
00007 
00008 #include <network_interface.h>
00009 
00010 using namespace AS::Network;  // NOLINT
00011 using boost::asio::ip::udp;
00012 
00013 // Default constructor.
00014 UDPInterface::UDPInterface() :
00015   socket_(io_service_)
00016 {
00017 }
00018 
00019 // Default destructor.
00020 UDPInterface::~UDPInterface()
00021 {
00022 }
00023 
00024 return_statuses UDPInterface::open(const char *ip_address, const int &port)
00025 {
00026   if (socket_.is_open())
00027     return OK;
00028 
00029   std::stringstream sPort;
00030   sPort << port;
00031   udp::resolver res(io_service_);
00032   udp::resolver::query query(udp::v4(), ip_address, sPort.str());
00033   sender_endpoint_ = *res.resolve(query);
00034   boost::system::error_code ec;
00035 
00036   socket_.connect(sender_endpoint_, ec);
00037 
00038   if (ec.value() == boost::system::errc::success)
00039   {
00040     return OK;
00041   }
00042   else if (ec.value() == boost::asio::error::invalid_argument)
00043   {
00044     return BAD_PARAM;
00045   }
00046   else
00047   {
00048     close();
00049     return INIT_FAILED;
00050   }
00051 }
00052 
00053 return_statuses UDPInterface::close()
00054 {
00055   if (!socket_.is_open())
00056     return SOCKET_CLOSED;
00057 
00058   boost::system::error_code ec;
00059   socket_.close(ec);
00060 
00061   if (ec.value() == boost::system::errc::success)
00062   {
00063     return OK;
00064   }
00065   else
00066   {
00067     return CLOSE_FAILED;
00068   }
00069 }
00070 
00071 bool UDPInterface::is_open()
00072 {
00073   return socket_.is_open();
00074 }
00075 
00076 return_statuses UDPInterface::read(unsigned char *msg,
00077                                    const size_t &buf_size,
00078                                    size_t &bytes_read)
00079 {
00080   if (!socket_.is_open())
00081     return SOCKET_CLOSED;
00082 
00083   boost::system::error_code ec;
00084   bytes_read = socket_.receive_from(boost::asio::buffer(msg, buf_size), sender_endpoint_, 0, ec);
00085 
00086   if (ec.value() == boost::system::errc::success)
00087   {
00088     return OK;
00089   }
00090   else
00091   {
00092     return READ_FAILED;
00093   }
00094 }
00095 
00096 return_statuses UDPInterface::write(unsigned char *msg, const size_t &msg_size)
00097 {
00098   if (!socket_.is_open())
00099     return SOCKET_CLOSED;
00100 
00101   boost::system::error_code ec;
00102   socket_.send_to(boost::asio::buffer(msg, msg_size), sender_endpoint_, 0, ec);
00103 
00104   if (ec.value() == boost::system::errc::success)
00105   {
00106     return OK;
00107   }
00108   else
00109   {
00110     return WRITE_FAILED;
00111   }
00112 }


network_interface
Author(s): Joshua Whitley , Daniel Stanek , Joe Kale
autogenerated on Thu Jun 6 2019 21:43:30