network_interface.h
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 // Define a class that supports a basic network interface that's independent of the hardware and driver library used
00009 // Different libraries can be created to define all these functions for a specific driver library
00010 
00011 #ifndef NETWORK_INTERFACE_NETWORK_INTERFACE_H
00012 #define NETWORK_INTERFACE_NETWORK_INTERFACE_H
00013 
00014 // C++ Includes
00015 #include <cstdio>
00016 #include <iostream>
00017 #include <string>
00018 #include <boost/asio.hpp>
00019 #include <boost/bind.hpp>
00020 
00021 // OS Includes
00022 #include <unistd.h>
00023 
00024 namespace AS
00025 {
00026 namespace Network
00027 {
00028 enum return_statuses
00029 {
00030   OK = 0,
00031   INIT_FAILED = -1,
00032   BAD_PARAM = -2,
00033   SOCKET_ERROR = -3,
00034   SOCKET_CLOSED = -4,
00035   NO_MESSAGES_RECEIVED = -5,
00036   READ_FAILED = -6,
00037   WRITE_FAILED = -7,
00038   CLOSE_FAILED = -8,
00039   SOCKET_TIMEOUT = -9
00040 };
00041 
00042 class UDPInterface
00043 {
00044 public:
00045   UDPInterface();
00046 
00047   ~UDPInterface();
00048 
00049   // Called to pass in parameters and open ethernet link
00050   return_statuses open(const char *ip_address, const int &port);
00051 
00052   // Close the ethernet link
00053   return_statuses close();
00054 
00055   // Check on the status of the link
00056   bool is_open();
00057 
00058   // Read a message - UDP is datagram-based so you cannot read exactly x bytes.
00059   return_statuses read(unsigned char *msg,
00060                        const size_t &buf_size,
00061                        size_t &bytes_read);
00062 
00063   // Send a message
00064   return_statuses write(unsigned char *msg, const size_t &msg_size);
00065 
00066 private:
00067   boost::asio::io_service io_service_;
00068   boost::asio::ip::udp::socket socket_;
00069   boost::asio::ip::udp::endpoint sender_endpoint_;
00070 };
00071 
00072 class TCPInterface
00073 {
00074 public:
00075   TCPInterface();
00076 
00077   ~TCPInterface();
00078 
00079   // Called to pass in parameters and open ethernet link
00080   return_statuses open(const char *ip_address,
00081                        const int &port);
00082 
00083   // Close the ethernet link
00084   return_statuses close();
00085 
00086   // Check on the status of the link
00087   bool is_open();
00088 
00089   // Read a message
00090   return_statuses read(unsigned char *msg,
00091                        const size_t &buf_size,
00092                        size_t &bytes_read,
00093                        int timeout_ms = 0);  // Optional timeout argument, in milliseconds
00094   return_statuses read_exactly(unsigned char *msg,
00095                                const size_t &buf_size,
00096                                const size_t &bytes_to_read,
00097                                int timeout_ms = 0);  // Optional timeout argument, in milliseconds
00098 
00099   // Send a message
00100   return_statuses write(unsigned char *msg, const size_t &msg_size);
00101 private:
00102   boost::asio::io_service io_service_;
00103   boost::asio::ip::tcp::socket socket_;
00104   boost::system::error_code error_;
00105   size_t bytes_read_;
00106 
00107   void timeout_handler(const boost::system::error_code& error);
00108   void read_handler(const boost::system::error_code& error, size_t bytes_read);
00109 };
00110 
00111 // Utility Functions
00112 std::string return_status_desc(const return_statuses &ret);
00113 }  // namespace Network
00114 }  // namespace AS
00115 #endif  // NETWORK_INTERFACE_NETWORK_INTERFACE_H


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