Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef NETWORK_INTERFACE_NETWORK_INTERFACE_H
00012 #define NETWORK_INTERFACE_NETWORK_INTERFACE_H
00013
00014
00015 #include <cstdio>
00016 #include <iostream>
00017 #include <string>
00018 #include <boost/asio.hpp>
00019 #include <boost/bind.hpp>
00020
00021
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
00050 return_statuses open(const char *ip_address, const int &port);
00051
00052
00053 return_statuses close();
00054
00055
00056 bool is_open();
00057
00058
00059 return_statuses read(unsigned char *msg,
00060 const size_t &buf_size,
00061 size_t &bytes_read);
00062
00063
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
00080 return_statuses open(const char *ip_address,
00081 const int &port);
00082
00083
00084 return_statuses close();
00085
00086
00087 bool is_open();
00088
00089
00090 return_statuses read(unsigned char *msg,
00091 const size_t &buf_size,
00092 size_t &bytes_read,
00093 int timeout_ms = 0);
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);
00098
00099
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
00112 std::string return_status_desc(const return_statuses &ret);
00113 }
00114 }
00115 #endif // NETWORK_INTERFACE_NETWORK_INTERFACE_H