@@ -41,75 +41,140 @@ extern "C" {
41
41
#endif
42
42
43
43
/* *
44
- * Initialize the network stack (needed for Windows).
45
- * Returns 0 on success, non-zero on failure.
44
+ * Initialize the network stack. This function is necessary for Windows
45
+ * platforms to set up the Winsock library. On other platforms, it may
46
+ * perform other necessary initializations.
47
+ *
48
+ * @return 0 on success, non-zero on failure.
46
49
*/
47
50
int fossil_io_network_create (void );
48
51
49
52
/* *
50
- * Clean up network stack (needed for Windows).
53
+ * Clean up the network stack. This function is necessary for Windows
54
+ * platforms to clean up the Winsock library. On other platforms, it may
55
+ * perform other necessary clean-up operations.
51
56
*/
52
57
void fossil_io_network_destroy (void );
53
58
54
59
/* *
55
- * Create a new TCP socket.
56
- * Returns a valid socket on success or FOSSIL_IO_INVALID_SOCKET on failure.
60
+ * Create a new socket of the specified type (TCP or UDP).
61
+ *
62
+ * @param type The type of socket to create (e.g., SOCK_STREAM for TCP, SOCK_DGRAM for UDP).
63
+ * @return A valid socket on success, or FOSSIL_IO_INVALID_SOCKET on failure.
57
64
*/
58
65
fossil_io_socket_t fossil_io_network_create_socket (int type);
59
66
60
67
/* *
61
- * Bind a socket to a specific port (IPv4/IPv6).
62
- * Returns 0 on success, -1 on failure.
68
+ * Bind a socket to a specific IP address and port. This function associates
69
+ * the socket with a local address so that it can listen for incoming connections
70
+ * or send data.
71
+ *
72
+ * @param sock The socket to bind.
73
+ * @param ip The IP address to bind to (e.g., "127.0.0.1" for localhost).
74
+ * @param port The port number to bind to.
75
+ * @return 0 on success, -1 on failure.
63
76
*/
64
77
int fossil_io_network_bind (fossil_io_socket_t sock, const char *ip, uint16_t port);
65
78
66
79
/* *
67
- * Listen for incoming connections.
68
- * Returns 0 on success, -1 on failure.
80
+ * Listen for incoming connections on a bound socket. This function puts the
81
+ * socket into a state where it can accept incoming connection requests.
82
+ *
83
+ * @param sock The socket to listen on.
84
+ * @param backlog The maximum length of the queue of pending connections.
85
+ * @return 0 on success, -1 on failure.
69
86
*/
70
87
int fossil_io_network_listen (fossil_io_socket_t sock, int backlog);
71
88
72
89
/* *
73
- * Accept a new connection.
74
- * Returns a valid socket on success, or FOSSIL_IO_INVALID_SOCKET on failure.
90
+ * Accept a new incoming connection on a listening socket. This function
91
+ * extracts the first connection request on the queue of pending connections
92
+ * and creates a new socket for the connection.
93
+ *
94
+ * @param sock The listening socket.
95
+ * @param client_ip A buffer to store the IP address of the connecting client.
96
+ * @param client_port A pointer to store the port number of the connecting client.
97
+ * @return A valid socket for the new connection on success, or FOSSIL_IO_INVALID_SOCKET on failure.
75
98
*/
76
99
fossil_io_socket_t fossil_io_network_accept (fossil_io_socket_t sock, char *client_ip, uint16_t *client_port);
77
100
78
101
/* *
79
- * Connect to a remote server.
80
- * Returns 0 on success, -1 on failure.
102
+ * Connect to a remote server. This function establishes a connection to a
103
+ * specified IP address and port.
104
+ *
105
+ * @param sock The socket to use for the connection.
106
+ * @param ip The IP address of the remote server.
107
+ * @param port The port number of the remote server.
108
+ * @return 0 on success, -1 on failure.
81
109
*/
82
110
int fossil_io_network_connect (fossil_io_socket_t sock, const char *ip, uint16_t port);
83
111
84
112
/* *
85
- * Send data over a socket.
86
- * Returns the number of bytes sent, or -1 on failure.
113
+ * Send data over a connected socket. This function transmits data to the
114
+ * remote peer connected to the socket.
115
+ *
116
+ * @param sock The socket to use for sending data.
117
+ * @param data A pointer to the data to be sent.
118
+ * @param len The length of the data to be sent.
119
+ * @return The number of bytes sent, or -1 on failure.
87
120
*/
88
121
int fossil_io_network_send (fossil_io_socket_t sock, const void *data, size_t len);
89
122
90
123
/* *
91
- * Receive data from a socket.
92
- * Returns the number of bytes received, or -1 on failure.
124
+ * Receive data from a connected socket. This function reads data from the
125
+ * remote peer connected to the socket.
126
+ *
127
+ * @param sock The socket to use for receiving data.
128
+ * @param buffer A buffer to store the received data.
129
+ * @param len The maximum length of data to be received.
130
+ * @return The number of bytes received, or -1 on failure.
93
131
*/
94
132
int fossil_io_network_receive (fossil_io_socket_t sock, void *buffer, size_t len);
95
133
96
134
/* *
97
- * Close a socket.
135
+ * Close a socket. This function releases the resources associated with the
136
+ * socket and closes the connection.
137
+ *
138
+ * @param sock The socket to be closed.
98
139
*/
99
140
void fossil_io_network_close (fossil_io_socket_t sock);
100
141
101
142
/* *
102
- * Send data to a specific IP address and port.
103
- * Returns the number of bytes sent, or -1 on failure.
143
+ * Send data to a specific IP address and port using a UDP socket. This function
144
+ * transmits data to the specified address and port without establishing a connection.
145
+ *
146
+ * @param sock The socket to use for sending data.
147
+ * @param data A pointer to the data to be sent.
148
+ * @param len The length of the data to be sent.
149
+ * @param ip The IP address of the destination.
150
+ * @param port The port number of the destination.
151
+ * @return The number of bytes sent, or -1 on failure.
104
152
*/
105
153
int fossil_io_network_sendto (fossil_io_socket_t sock, const void *data, size_t len, const char *ip, uint16_t port);
106
154
107
155
/* *
108
- * Receive data from a specific IP address and port.
109
- * Returns the number of bytes received, or -1 on failure.
156
+ * Receive data from a specific IP address and port using a UDP socket. This function
157
+ * reads data from the specified address and port without establishing a connection.
158
+ *
159
+ * @param sock The socket to use for receiving data.
160
+ * @param buffer A buffer to store the received data.
161
+ * @param len The maximum length of data to be received.
162
+ * @param ip A buffer to store the IP address of the sender.
163
+ * @param port A pointer to store the port number of the sender.
164
+ * @return The number of bytes received, or -1 on failure.
110
165
*/
111
166
int fossil_io_network_recvfrom (fossil_io_socket_t sock, void *buffer, size_t len, char *ip, uint16_t *port);
112
167
168
+ /* *
169
+ * Bridge function for network operations. This function can be used to
170
+ * transfer data between two sockets, effectively bridging them.
171
+ *
172
+ * @param sock1 The first socket.
173
+ * @param sock2 The second socket.
174
+ * @return 0 on success, -1 on failure.
175
+ */
176
+ int fossil_io_network_bridge (fossil_io_socket_t sock1, fossil_io_socket_t sock2);
177
+
113
178
#ifdef __cplusplus
114
179
}
115
180
@@ -127,99 +192,166 @@ namespace fossil {
127
192
class Network {
128
193
public:
129
194
/* *
130
- * Initialize the network stack (needed for Windows).
131
- * Returns 0 on success, non-zero on failure.
195
+ * Initialize the network stack. This function is necessary for Windows
196
+ * platforms to set up the Winsock library. On other platforms, it may
197
+ * perform other necessary initializations.
198
+ *
199
+ * @return 0 on success, non-zero on failure.
132
200
*/
133
201
static int init (void ) {
134
202
return fossil_io_network_create ();
135
203
}
136
204
137
205
/* *
138
- * Clean up network stack (needed for Windows).
206
+ * Clean up the network stack. This function is necessary for Windows
207
+ * platforms to clean up the Winsock library. On other platforms, it may
208
+ * perform other necessary clean-up operations.
139
209
*/
140
210
static void cleanup (void ) {
141
211
fossil_io_network_destroy ();
142
212
}
143
213
144
214
/* *
145
- * Create a new TCP socket.
146
- * Returns a valid socket on success or FOSSIL_IO_INVALID_SOCKET on failure.
215
+ * Create a new socket of the specified type (TCP or UDP).
216
+ *
217
+ * @param type The type of socket to create (e.g., SOCK_STREAM for TCP, SOCK_DGRAM for UDP).
218
+ * @return A valid socket on success, or FOSSIL_IO_INVALID_SOCKET on failure.
147
219
*/
148
220
static fossil_io_socket_t create_socket (int type) {
149
221
return fossil_io_network_create_socket (type);
150
222
}
151
223
152
224
/* *
153
- * Bind a socket to a specific port (IPv4/IPv6).
154
- * Returns 0 on success, -1 on failure.
225
+ * Bind a socket to a specific IP address and port. This function associates
226
+ * the socket with a local address so that it can listen for incoming connections
227
+ * or send data.
228
+ *
229
+ * @param sock The socket to bind.
230
+ * @param ip The IP address to bind to (e.g., "127.0.0.1" for localhost).
231
+ * @param port The port number to bind to.
232
+ * @return 0 on success, -1 on failure.
155
233
*/
156
234
static int bind (fossil_io_socket_t sock, const char *ip, uint16_t port) {
157
235
return fossil_io_network_bind (sock, ip, port);
158
236
}
159
237
160
238
/* *
161
- * Listen for incoming connections.
162
- * Returns 0 on success, -1 on failure.
239
+ * Listen for incoming connections on a bound socket. This function puts the
240
+ * socket into a state where it can accept incoming connection requests.
241
+ *
242
+ * @param sock The socket to listen on.
243
+ * @param backlog The maximum length of the queue of pending connections.
244
+ * @return 0 on success, -1 on failure.
163
245
*/
164
246
static int listen (fossil_io_socket_t sock, int backlog) {
165
247
return fossil_io_network_listen (sock, backlog);
166
248
}
167
249
168
250
/* *
169
- * Accept a new connection.
170
- * Returns a valid socket on success, or FOSSIL_IO_INVALID_SOCKET on failure.
251
+ * Accept a new incoming connection on a listening socket. This function
252
+ * extracts the first connection request on the queue of pending connections
253
+ * and creates a new socket for the connection.
254
+ *
255
+ * @param sock The listening socket.
256
+ * @param client_ip A buffer to store the IP address of the connecting client.
257
+ * @param client_port A pointer to store the port number of the connecting client.
258
+ * @return A valid socket for the new connection on success, or FOSSIL_IO_INVALID_SOCKET on failure.
171
259
*/
172
260
static fossil_io_socket_t accept (fossil_io_socket_t sock, char *client_ip, uint16_t *client_port) {
173
261
return fossil_io_network_accept (sock, client_ip, client_port);
174
262
}
175
263
176
264
/* *
177
- * Connect to a remote server.
178
- * Returns 0 on success, -1 on failure.
265
+ * Connect to a remote server. This function establishes a connection to a
266
+ * specified IP address and port.
267
+ *
268
+ * @param sock The socket to use for the connection.
269
+ * @param ip The IP address of the remote server.
270
+ * @param port The port number of the remote server.
271
+ * @return 0 on success, -1 on failure.
179
272
*/
180
273
static int connect (fossil_io_socket_t sock, const char *ip, uint16_t port) {
181
274
return fossil_io_network_connect (sock, ip, port);
182
275
}
183
276
184
277
/* *
185
- * Send data over a socket.
186
- * Returns the number of bytes sent, or -1 on failure.
278
+ * Send data over a connected socket. This function transmits data to the
279
+ * remote peer connected to the socket.
280
+ *
281
+ * @param sock The socket to use for sending data.
282
+ * @param data A pointer to the data to be sent.
283
+ * @param len The length of the data to be sent.
284
+ * @return The number of bytes sent, or -1 on failure.
187
285
*/
188
286
static int send (fossil_io_socket_t sock, const void *data, size_t len) {
189
287
return fossil_io_network_send (sock, data, len);
190
288
}
191
289
192
290
/* *
193
- * Receive data from a socket.
194
- * Returns the number of bytes received, or -1 on failure.
291
+ * Receive data from a connected socket. This function reads data from the
292
+ * remote peer connected to the socket.
293
+ *
294
+ * @param sock The socket to use for receiving data.
295
+ * @param buffer A buffer to store the received data.
296
+ * @param len The maximum length of data to be received.
297
+ * @return The number of bytes received, or -1 on failure.
195
298
*/
196
299
static int receive (fossil_io_socket_t sock, void *buffer, size_t len) {
197
300
return fossil_io_network_receive (sock, buffer, len);
198
301
}
199
302
200
303
/* *
201
- * Close a socket.
304
+ * Close a socket. This function releases the resources associated with the
305
+ * socket and closes the connection.
306
+ *
307
+ * @param sock The socket to be closed.
202
308
*/
203
309
static void close (fossil_io_socket_t sock) {
204
310
fossil_io_network_close (sock);
205
311
}
206
312
207
313
/* *
208
- * Send data to a specific IP address and port.
209
- * Returns the number of bytes sent, or -1 on failure.
314
+ * Send data to a specific IP address and port using a UDP socket. This function
315
+ * transmits data to the specified address and port without establishing a connection.
316
+ *
317
+ * @param sock The socket to use for sending data.
318
+ * @param data A pointer to the data to be sent.
319
+ * @param len The length of the data to be sent.
320
+ * @param ip The IP address of the destination.
321
+ * @param port The port number of the destination.
322
+ * @return The number of bytes sent, or -1 on failure.
210
323
*/
211
324
static int sendto (fossil_io_socket_t sock, const void *data, size_t len, const char *ip, uint16_t port) {
212
325
return fossil_io_network_sendto (sock, data, len, ip, port);
213
326
}
214
327
215
328
/* *
216
- * Receive data from a specific IP address and port.
217
- * Returns the number of bytes received, or -1 on failure.
329
+ * Receive data from a specific IP address and port using a UDP socket. This function
330
+ * reads data from the specified address and port without establishing a connection.
331
+ *
332
+ * @param sock The socket to use for receiving data.
333
+ * @param buffer A buffer to store the received data.
334
+ * @param len The maximum length of data to be received.
335
+ * @param ip A buffer to store the IP address of the sender.
336
+ * @param port A pointer to store the port number of the sender.
337
+ * @return The number of bytes received, or -1 on failure.
218
338
*/
219
339
static int recvfrom (fossil_io_socket_t sock, void *buffer, size_t len, char *ip, uint16_t *port) {
220
340
return fossil_io_network_recvfrom (sock, buffer, len, ip, port);
221
341
}
222
342
343
+ /* *
344
+ * Bridge function for network operations. This function can be used to
345
+ * transfer data between two sockets, effectively bridging them.
346
+ *
347
+ * @param sock1 The first socket.
348
+ * @param sock2 The second socket.
349
+ * @return 0 on success, -1 on failure.
350
+ */
351
+ static int bridge (fossil_io_socket_t sock1, fossil_io_socket_t sock2) {
352
+ return fossil_io_network_bridge (sock1, sock2);
353
+ }
354
+
223
355
};
224
356
}
225
357
}
0 commit comments