14 |
14 |
15 #include <stdbool.h> |
15 #include <stdbool.h> |
16 #include <stdint.h> |
16 #include <stdint.h> |
17 |
17 |
18 struct _flib_tcpsocket; |
18 struct _flib_tcpsocket; |
19 typedef struct _flib_tcpsocket *flib_tcpsocket; |
19 typedef struct _flib_tcpsocket flib_tcpsocket; |
20 |
20 |
21 struct _flib_acceptor; |
21 struct _flib_acceptor; |
22 typedef struct _flib_acceptor *flib_acceptor; |
22 typedef struct _flib_acceptor flib_acceptor; |
23 |
23 |
24 /** |
24 /** |
25 * Create a new acceptor which will listen for incoming TCP connections |
25 * Create a new acceptor which will listen for incoming TCP connections |
26 * on the given port. If port is 0, this will listen on a random |
26 * on the given port. If port is 0, this will listen on a random |
27 * unused port which can then be queried with flib_acceptor_listenport. |
27 * unused port which can then be queried with flib_acceptor_listenport. |
28 * |
28 * |
29 * Can return NULL on error. |
29 * Can return NULL on error. |
30 */ |
30 */ |
31 flib_acceptor flib_acceptor_create(uint16_t port); |
31 flib_acceptor *flib_acceptor_create(uint16_t port); |
32 |
32 |
33 /** |
33 /** |
34 * Return the port on which the acceptor is listening. |
34 * Return the port on which the acceptor is listening. |
35 */ |
35 */ |
36 uint16_t flib_acceptor_listenport(flib_acceptor acceptor); |
36 uint16_t flib_acceptor_listenport(flib_acceptor *acceptor); |
37 |
37 |
38 /** |
38 /** |
39 * Close the acceptor, free its memory and set it to NULL. |
39 * Close the acceptor and free its memory. |
40 * If the acceptor is already NULL, nothing happens. |
40 * If the acceptor is already NULL, nothing happens. |
41 */ |
41 */ |
42 void flib_acceptor_close(flib_acceptor *acceptorptr); |
42 void flib_acceptor_close(flib_acceptor *acceptor); |
43 |
43 |
44 /** |
44 /** |
45 * Try to accept a connection from an acceptor (listening socket). |
45 * Try to accept a connection from an acceptor (listening socket). |
46 * if localOnly is true, this will only accept connections which came from 127.0.0.1 |
46 * if localOnly is true, this will only accept connections which came from 127.0.0.1 |
47 * Returns NULL if nothing can be accepted. |
47 * Returns NULL if nothing can be accepted. |
48 */ |
48 */ |
49 flib_tcpsocket flib_socket_accept(flib_acceptor acceptor, bool localOnly); |
49 flib_tcpsocket *flib_socket_accept(flib_acceptor *acceptor, bool localOnly); |
50 |
50 |
51 /** |
51 /** |
52 * Close the socket, free its memory and set it to NULL. |
52 * Close the socket and free its memory. |
53 * If the socket is already NULL, nothing happens. |
53 * If the socket is already NULL, nothing happens. |
54 */ |
54 */ |
55 void flib_socket_close(flib_tcpsocket *socket); |
55 void flib_socket_close(flib_tcpsocket *socket); |
56 |
56 |
57 /** |
57 /** |
58 * Attempt to receive up to maxlen bytes from the socket, but does not |
58 * Attempt to receive up to maxlen bytes from the socket, but does not |
59 * block if nothing is available. |
59 * block if nothing is available. |
60 * Returns the ammount of data received, 0 if there was nothing to receive, |
60 * Returns the ammount of data received, 0 if there was nothing to receive, |
61 * or a negative number if the connection was closed or an error occurred. |
61 * or a negative number if the connection was closed or an error occurred. |
62 */ |
62 */ |
63 int flib_socket_nbrecv(flib_tcpsocket sock, void *data, int maxlen); |
63 int flib_socket_nbrecv(flib_tcpsocket *sock, void *data, int maxlen); |
64 |
64 |
65 int flib_socket_send(flib_tcpsocket sock, const void *data, int len); |
65 int flib_socket_send(flib_tcpsocket *sock, const void *data, int len); |
66 |
66 |
67 #endif /* SOCKET_H_ */ |
67 #endif /* SOCKET_H_ */ |