Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 3 | #include <boost/asio/ip/udp.hpp> |
| 4 | #include <memory> |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 5 | #include <string> |
| 6 | #include <tuple> |
| 7 | #include <vector> |
| 8 | |
| 9 | namespace udpsocket |
| 10 | { |
| 11 | |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 12 | /** @class Channel |
| 13 | * |
| 14 | * @brief Provides encapsulation for UDP socket operations like Read, Peek, |
| 15 | * Write, Remote peer's IP Address and Port. |
| 16 | */ |
| 17 | class Channel |
| 18 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 19 | public: |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 20 | Channel() = delete; |
| 21 | ~Channel() = default; |
| 22 | Channel(const Channel& right) = delete; |
| 23 | Channel& operator=(const Channel& right) = delete; |
| 24 | Channel(Channel&&) = delete; |
| 25 | Channel& operator=(Channel&&) = delete; |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 26 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 27 | /** |
| 28 | * @brief Constructor |
| 29 | * |
| 30 | * Initialize the IPMI socket object with the socket descriptor |
| 31 | * |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 32 | * @param [in] pointer to a boost::asio udp socket object |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 33 | * |
| 34 | * @return None |
| 35 | */ |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 36 | explicit Channel(std::shared_ptr<boost::asio::ip::udp::socket> socket) : |
| 37 | socket(socket) |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 38 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 39 | } |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 40 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 41 | /** |
| 42 | * @brief Fetch the IP address of the remote peer |
| 43 | * |
| 44 | * Returns the IP address of the remote peer which is connected to this |
| 45 | * socket |
| 46 | * |
| 47 | * @return IP address of the remote peer |
| 48 | */ |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 49 | std::string getRemoteAddress() const |
| 50 | { |
| 51 | return endpoint.address().to_string(); |
| 52 | } |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 53 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 54 | /** |
| 55 | * @brief Fetch the port number of the remote peer |
| 56 | * |
| 57 | * Returns the port number of the remote peer |
| 58 | * |
| 59 | * @return Port number |
| 60 | * |
| 61 | */ |
| 62 | auto getPort() const |
| 63 | { |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 64 | return endpoint.port(); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 65 | } |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 66 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 67 | /** |
| 68 | * @brief Read the incoming packet |
| 69 | * |
| 70 | * Reads the data available on the socket |
| 71 | * |
| 72 | * @return A tuple with return code and vector with the buffer |
| 73 | * In case of success, the vector is populated with the data |
| 74 | * available on the socket and return code is 0. |
| 75 | * In case of error, the return code is < 0 and vector is set |
| 76 | * to size 0. |
| 77 | */ |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 78 | std::tuple<int, std::vector<uint8_t>> read() |
| 79 | { |
| 80 | std::vector<uint8_t> packet(socket->available()); |
| 81 | try |
| 82 | { |
| 83 | socket->receive_from(boost::asio::buffer(packet), endpoint); |
| 84 | } |
| 85 | catch (const boost::system::system_error& e) |
| 86 | { |
| 87 | return std::make_tuple(e.code().value(), std::vector<uint8_t>()); |
| 88 | } |
| 89 | return std::make_tuple(0, packet); |
| 90 | } |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 91 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 92 | /** |
| 93 | * @brief Write the outgoing packet |
| 94 | * |
| 95 | * Writes the data in the vector to the socket |
| 96 | * |
| 97 | * @param [in] inBuffer |
| 98 | * The vector would be the buffer of data to write to the socket. |
| 99 | * |
| 100 | * @return In case of success the return code is 0 and return code is |
| 101 | * < 0 in case of failure. |
| 102 | */ |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 103 | int write(const std::vector<uint8_t>& inBuffer) |
| 104 | { |
| 105 | try |
| 106 | { |
| 107 | socket->send_to(boost::asio::buffer(inBuffer), endpoint); |
| 108 | } |
| 109 | catch (const boost::system::system_error& e) |
| 110 | { |
| 111 | return e.code().value(); |
| 112 | } |
| 113 | return 0; |
| 114 | } |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 115 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 116 | /** |
| 117 | * @brief Returns file descriptor for the socket |
| 118 | */ |
| 119 | auto getHandle(void) const |
| 120 | { |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 121 | return socket->native_handle(); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 122 | } |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 123 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 124 | private: |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame^] | 125 | std::shared_ptr<boost::asio::ip::udp::socket> socket; |
| 126 | boost::asio::ip::udp::endpoint endpoint{}; |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | } // namespace udpsocket |