NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
tcp_socket.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #ifndef APPLICATIONS_STORAGE_STORAGE_COMMON_TCP_SOCKET_HPP_
27 #define APPLICATIONS_STORAGE_STORAGE_COMMON_TCP_SOCKET_HPP_
28 
29 #include <array>
30 #include <cstddef>
31 #include <cstdint>
32 #include <vector>
33 
35 
36 namespace storage {
37 
38 /*
39  * TCP socket helper class
40  */
41 class tcp_socket {
42 public:
43  /*
44  * TCP connection status codes
45  */
46  enum class connection_status {
47  connected = 0, /* Connected */
48  establishing, /* Trying to connect but not done yet */
49  refused, /* Remote refused the connection */
50  failed, /* Connection failed for some reason */
51  };
52 
53  /*
54  * Destroy and disconnect the socket
55  */
56  ~tcp_socket();
57 
58  /*
59  * Create a default (invalid) socket
60  */
61  tcp_socket();
62 
63  /*
64  * Create a socket from a given fd number
65  *
66  * @fd [in]: Underlying socket fd number to use
67  *
68  * @throws std::runtime_error: If unable to set socket options
69  */
70  explicit tcp_socket(uint32_t fd);
71 
72  /*
73  * Disabled copy constructor
74  */
75  tcp_socket(tcp_socket const &) = delete;
76 
77  /*
78  * Move constructor
79  *
80  * @other [in]: Object to move from
81  */
82  tcp_socket(tcp_socket &&other) noexcept;
83 
84  /*
85  * Disabled copy assignment operator
86  */
87  tcp_socket &operator=(tcp_socket const &) = delete;
88 
89  /*
90  * Move assignment operator
91  *
92  * @other [in]: Object to move from
93  * @return: Reference to assigned object
94  */
95  tcp_socket &operator=(tcp_socket &&other) noexcept;
96 
97  /*
98  * Set the socket to blocking or non-blocking
99  *
100  * @blocking [in]: Set blocking(true) or non-blocking(false)
101  */
102  void set_blocking(bool blocking);
103 
104  /*
105  * Close the socket
106  */
107  void close(void);
108 
109  /*
110  * Connect the socket to a server at the given address
111  *
112  * @address [in]: Address to connect to
113  */
114  void connect(storage::ip_address const &address);
115 
116  /*
117  * Listen for connections on the specified port
118  *
119  * @port [in]: Port to listen on
120  */
121  void listen(uint16_t port);
122 
123  /*
124  * Pool and check if the socket is fully connected
125  *
126  * @return: The current connection status
127  */
129 
130  /*
131  * Start accepting incoming connections
132  *
133  * @return: A tcp_socket object. The user must check using is_valid to know if the returned socket is connected
134  * to anything or not
135  */
136  tcp_socket accept(void);
137 
138  /*
139  * Write bytes to the socket
140  *
141  * @buffer [in]: Pointer to an array of bytes
142  * @byte_count [in]: The number of bytes to write from the buffer pointed to by buffer
143  *
144  * @return: The number of bytes written to the socket
145  */
146  size_t write(char const *buffer, size_t byte_count);
147 
148  /*
149  * Read bytes from the socket
150  *
151  * @param [in] buffer pointer to an array of bytes
152  * @param [in] buffer_capacity number of bytes available in the array pointed to by buffer
153  *
154  * @return: The number of valid bytes placed into the array pointed to by buffer
155  */
156  size_t read(char *buffer, size_t buffer_capacity);
157 
158  /*
159  * Check if the socket is valid or not, invalid sockets can be simply disposed of at negligible cost
160  *
161  * @return: true if socket is valid (refers to something) otherwise false
162  */
163  bool is_valid(void) const noexcept;
164 
165 private:
166  uint32_t m_fd; /* internal socket number */
167 
168  /*
169  * Set socket options
170  */
171  void set_socket_options(void);
172 };
173 
174 } /* namespace storage */
175 
176 #endif /* APPLICATIONS_STORAGE_STORAGE_COMMON_TCP_SOCKET_HPP_ */
size_t read(char *buffer, size_t buffer_capacity)
Definition: tcp_socket.cpp:305
tcp_socket(tcp_socket const &)=delete
void set_blocking(bool blocking)
Definition: tcp_socket.cpp:102
void listen(uint16_t port)
Definition: tcp_socket.cpp:172
tcp_socket & operator=(tcp_socket const &)=delete
bool is_valid(void) const noexcept
Definition: tcp_socket.cpp:336
size_t write(char const *buffer, size_t byte_count)
Definition: tcp_socket.cpp:274
void connect(storage::ip_address const &address)
Definition: tcp_socket.cpp:138
tcp_socket accept(void)
Definition: tcp_socket.cpp:245
connection_status poll_is_connected(void)
Definition: tcp_socket.cpp:195