blob: ebe0c8f2f292c697734e6ac60e5b29195cfe522b [file] [log] [blame]
Tom Josephaaeb29e2016-08-10 06:36:33 -05001#pragma once
2
Vernon Mauery7a0142c2018-11-09 08:38:16 -08003#include <boost/asio/ip/udp.hpp>
4#include <memory>
Tom Josephaaeb29e2016-08-10 06:36:33 -05005#include <string>
6#include <tuple>
7#include <vector>
8
9namespace udpsocket
10{
11
Tom Josephaaeb29e2016-08-10 06:36:33 -050012/** @class Channel
13 *
14 * @brief Provides encapsulation for UDP socket operations like Read, Peek,
15 * Write, Remote peer's IP Address and Port.
16 */
17class Channel
18{
Vernon Mauery9e801a22018-10-12 13:20:49 -070019 public:
Vernon Mauery7a0142c2018-11-09 08:38:16 -080020 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 Josephaaeb29e2016-08-10 06:36:33 -050026
Vernon Mauery9e801a22018-10-12 13:20:49 -070027 /**
28 * @brief Constructor
29 *
30 * Initialize the IPMI socket object with the socket descriptor
31 *
Vernon Mauery7a0142c2018-11-09 08:38:16 -080032 * @param [in] pointer to a boost::asio udp socket object
Vernon Mauery9e801a22018-10-12 13:20:49 -070033 *
34 * @return None
35 */
Vernon Mauery7a0142c2018-11-09 08:38:16 -080036 explicit Channel(std::shared_ptr<boost::asio::ip::udp::socket> socket) :
37 socket(socket)
Vernon Mauery9e801a22018-10-12 13:20:49 -070038 {
Vernon Mauery9e801a22018-10-12 13:20:49 -070039 }
Tom Josephaaeb29e2016-08-10 06:36:33 -050040
Vernon Mauery9e801a22018-10-12 13:20:49 -070041 /**
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 Mauery7a0142c2018-11-09 08:38:16 -080049 std::string getRemoteAddress() const
50 {
51 return endpoint.address().to_string();
52 }
Tom Josephaaeb29e2016-08-10 06:36:33 -050053
Vernon Mauery9e801a22018-10-12 13:20:49 -070054 /**
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 Mauery7a0142c2018-11-09 08:38:16 -080064 return endpoint.port();
Vernon Mauery9e801a22018-10-12 13:20:49 -070065 }
Tom Josephaaeb29e2016-08-10 06:36:33 -050066
Vernon Mauery9e801a22018-10-12 13:20:49 -070067 /**
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 Mauery7a0142c2018-11-09 08:38:16 -080078 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 Josephaaeb29e2016-08-10 06:36:33 -050091
Vernon Mauery9e801a22018-10-12 13:20:49 -070092 /**
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 Mauery7a0142c2018-11-09 08:38:16 -0800103 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 Josephaaeb29e2016-08-10 06:36:33 -0500115
Vernon Mauery9e801a22018-10-12 13:20:49 -0700116 /**
117 * @brief Returns file descriptor for the socket
118 */
119 auto getHandle(void) const
120 {
Vernon Mauery7a0142c2018-11-09 08:38:16 -0800121 return socket->native_handle();
Vernon Mauery9e801a22018-10-12 13:20:49 -0700122 }
Tom Josephaaeb29e2016-08-10 06:36:33 -0500123
Vernon Mauery9e801a22018-10-12 13:20:49 -0700124 private:
Vernon Mauery7a0142c2018-11-09 08:38:16 -0800125 std::shared_ptr<boost::asio::ip::udp::socket> socket;
126 boost::asio::ip::udp::endpoint endpoint{};
Tom Josephaaeb29e2016-08-10 06:36:33 -0500127};
128
129} // namespace udpsocket