|
1 /* |
|
2 * nonblocksockets.h |
|
3 * |
|
4 * Created on: 31.05.2012 |
|
5 * Author: simmax |
|
6 */ |
|
7 |
|
8 #ifndef NONBLOCKSOCKETS_H_ |
|
9 #define NONBLOCKSOCKETS_H_ |
|
10 |
|
11 #include <SDL_net.h> |
|
12 #include <stdbool.h> |
|
13 |
|
14 typedef struct { |
|
15 TCPsocket sock; |
|
16 SDLNet_SocketSet sockset; |
|
17 } _NonBlockSocket; |
|
18 |
|
19 typedef _NonBlockSocket *NonBlockSocket; |
|
20 |
|
21 /** |
|
22 * Close the indicated socket, free its memory and set it to NULL. |
|
23 * If the socket is already NULL, nothing happens. |
|
24 */ |
|
25 void flib_nbsocket_close(NonBlockSocket *socket); |
|
26 |
|
27 /** |
|
28 * Try to accept a connection from a listening socket. |
|
29 * if localOnly is true, this will only accept connections which came from 127.0.0.1 |
|
30 * Returns NULL if nothing can be accepted. |
|
31 */ |
|
32 NonBlockSocket flib_nbsocket_accept(TCPsocket listensocket, bool localOnly); |
|
33 |
|
34 /** |
|
35 * Attempt to receive up to maxlen bytes from the socket, but does not |
|
36 * block if nothing is available. |
|
37 * Returns the ammount of data received, 0 if there was nothing to receive, |
|
38 * or a negative number if the connection was closed or an error occurred. |
|
39 */ |
|
40 int flib_nbsocket_recv(NonBlockSocket sock, void *data, int maxlen); |
|
41 |
|
42 /** |
|
43 * We can't do a nonblocking send over SDL_net, so this function just forwards |
|
44 * to SDLNet_TCP_Send for convenience, which blocks until all data is sent or an |
|
45 * error occurs. The ammount of data actually sent is returned, negative value on error. |
|
46 */ |
|
47 static inline int flib_nbsocket_blocksend(NonBlockSocket sock, void *data, int len) { |
|
48 return SDLNet_TCP_Send(sock->sock, data, len); |
|
49 } |
|
50 |
|
51 #endif /* NONBLOCKSOCKETS_H_ */ |