1 | #ifndef VWS_SOCKET_DECLARE |
2 | #define VWS_SOCKET_DECLARE |
3 | |
4 | #include <stdint.h> |
5 | #include <stddef.h> |
6 | |
7 | #include <openssl/ssl.h> |
8 | #include <openssl/err.h> |
9 | #include <openssl/crypto.h> |
10 | |
11 | #include <stdbool.h> |
12 | |
13 | #include "vws.h" |
14 | |
15 | #ifdef __cplusplus |
16 | extern "C" { |
17 | #endif |
18 | |
19 | struct vws_socket; |
20 | |
21 | /** |
22 | * @brief Callback for handshake on connect. This is called after socket |
23 | * connection but before set to non-blocking. This provides a way to do basic |
24 | * socket handshake before going into poll() mode. |
25 | |
26 | * @param s The socket |
27 | * @return Returns true if handshake succeeded, false otherwise |
28 | */ |
29 | typedef bool (*vws_socket_hs)(struct vws_socket* s); |
30 | |
31 | /** |
32 | * @brief Callback for abnormal disconnect, as a result of error or exceptional |
33 | * condition (remote peer drops connection, etc.) This takes place only on |
34 | * read()/write() operations. When this happens, this callback can be used to |
35 | * invoke an exception mechanism for languages that support it. |
36 | * |
37 | * If the handler does throw an exception, the handler MUST first call |
38 | * vws_socket_close(s) to clean up the socket appropriately. |
39 | * |
40 | * @param s The socket instance |
41 | * @return Returns true on successful reconnect, false otherwise. |
42 | */ |
43 | typedef void (*vws_socket_dh)(struct vws_socket* s); |
44 | |
45 | /** |
46 | * @brief A socket |
47 | */ |
48 | typedef struct vws_socket |
49 | { |
50 | /**< The socket file descriptor. */ |
51 | int sockfd; |
52 | |
53 | /**< The SSL connection instance. */ |
54 | SSL* ssl; |
55 | |
56 | /**< Socket receive buffer. */ |
57 | vws_buffer* buffer; |
58 | |
59 | /**< Socket timeout in milliseconds. */ |
60 | int timeout; |
61 | |
62 | /**< User-defined data associated with the connection */ |
63 | char* data; |
64 | |
65 | /**< User-defined handshake function to be called on connect */ |
66 | vws_socket_hs hs; |
67 | |
68 | /**< User-defined handler for abnormal disconnect (read/write error) */ |
69 | vws_socket_dh disconnect; |
70 | |
71 | /** Flag to force writes to poll() until all data flushed. Default true. */ |
72 | bool flush; |
73 | |
74 | } vws_socket; |
75 | |
76 | /** |
77 | * @defgroup SocketFunctions |
78 | * |
79 | * @brief Functions that manage sockets |
80 | * |
81 | */ |
82 | |
83 | /** |
84 | * @brief Allocates a new socket connection. |
85 | * |
86 | * @return A pointer to the new connection instance. |
87 | * |
88 | * @ingroup SocketFunctions |
89 | */ |
90 | vws_socket* vws_socket_new(); |
91 | |
92 | /** |
93 | * @brief Deallocates a socket connection. |
94 | * |
95 | * @param c The socket connection. |
96 | */ |
97 | void vws_socket_free(vws_socket* s); |
98 | |
99 | /** |
100 | * @brief Server instance constructor |
101 | * |
102 | * Constructs a new server instance. This takes a new, empty vws_socket instance |
103 | * and initializes all of its members. |
104 | * |
105 | * @param s The socket instance to be initialized |
106 | * @return The initialized socket instance |
107 | * |
108 | * @ingroup SocketFunctions |
109 | */ |
110 | vws_socket* vws_socket_ctor(vws_socket* s); |
111 | |
112 | /** |
113 | * @brief Socket instance destructor |
114 | * |
115 | * Destructs an initialized socket instance. |
116 | * |
117 | * @param s The socket instance to be destructed |
118 | * |
119 | * @ingroup SocketFunctions |
120 | */ |
121 | void vws_socket_dtor(vws_socket* s); |
122 | |
123 | /** |
124 | * @brief Connects to a specified host URL. |
125 | * |
126 | * @param s The socket instance to be destructed |
127 | * @param host The host to connect to |
128 | * @param port The port to connect to |
129 | * @param ssl Flag to enable/disable SSL |
130 | * @return Returns true if the connection is successful, false otherwise. |
131 | * |
132 | * @ingroup SocketFunctions |
133 | */ |
134 | bool vws_socket_connect(vws_socket* s, cstr host, int port, bool ssl); |
135 | |
136 | /** |
137 | * @brief Sets a timeout on a socket read/write operations. The default |
138 | * timeout is 10 seconds. |
139 | * |
140 | * @param s The socket instance to be destructed |
141 | * @param sec The timeout value in seconds. |
142 | * @return True if successful, false otherwise. |
143 | * |
144 | * @ingroup SocketFunctions |
145 | */ |
146 | bool vws_socket_set_timeout(vws_socket* s, int sec); |
147 | |
148 | /** |
149 | * @brief Sets a socket to non-blocking mode. |
150 | * |
151 | * @param sockfd The socket file descriptor. |
152 | * @return True if successful, false otherwise. |
153 | * |
154 | * @ingroup SocketFunctions |
155 | */ |
156 | bool vws_socket_set_nonblocking(int sockfd); |
157 | |
158 | /** |
159 | * @brief Closes the connection to the host. |
160 | * |
161 | * @param c The socket connection. |
162 | * |
163 | * @ingroup SocketFunctions |
164 | */ |
165 | void vws_socket_disconnect(vws_socket* s); |
166 | |
167 | /** |
168 | * @brief Checks if a connection is established. |
169 | * |
170 | * @param c The socket connection. |
171 | * @return Returns true if the connection is established, false otherwise. |
172 | * |
173 | * @ingroup SocketFunctions |
174 | */ |
175 | bool vws_socket_is_connected(vws_socket* s); |
176 | |
177 | /** |
178 | * @brief Closes a socket |
179 | * |
180 | * @param c The vws_socket |
181 | * |
182 | * @ingroup SocketFunctions |
183 | */ |
184 | void vws_socket_close(vws_socket* c); |
185 | |
186 | /** |
187 | * @brief Reads data from a socket connection into a buffer |
188 | * |
189 | * @param c The vws_cnx representing the Socket connection. |
190 | * @param data The buffer to read data into. |
191 | * @param size The size of the buffer. |
192 | * @return The number of bytes read, or an error code if an error occurred. |
193 | * |
194 | * @ingroup SocketFunctions |
195 | */ |
196 | ssize_t vws_socket_read(vws_socket* s); |
197 | |
198 | /** |
199 | * @brief Writes data from a buffer to a socket |
200 | * |
201 | * @param c The vws_cnx representing the socket |
202 | * @param data The buffer containing the data to write. |
203 | * @param size The size of the data to write. |
204 | * @return The number of bytes written, or an error code if an error occurred. |
205 | * |
206 | * @ingroup SocketFunctions |
207 | */ |
208 | ssize_t vws_socket_write(vws_socket* s, ucstr data, size_t size); |
209 | |
210 | #ifdef __cplusplus |
211 | } |
212 | #endif |
213 | |
214 | #endif /* VWS_SOCKET_DECLARE */ |
215 | |