]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/sockets.cpp
CMake: switch minizip casing
[xonotic/netradiant.git] / radiant / sockets.cpp
1
2 #include "sockets.h"
3
4 #if defined( WIN32 )
5 #include <winsock2.h>
6 #elif defined ( POSIX )
7 #include <sys/time.h>
8 #define SOCKET_ERROR -1
9 #else
10 #error "unsupported platform"
11 #endif
12
13 #ifdef __APPLE__
14 #include <unistd.h>
15 #endif
16
17 int Net_Wait( socket_t *sock, long sec, long usec ){
18 // used for select()
19 #ifdef WIN32
20         TIMEVAL tout = { sec, usec };
21 #endif
22 #if defined ( POSIX )
23         timeval tout;
24         tout.tv_sec = sec;
25         tout.tv_usec = usec;
26 #endif
27
28         // select() will identify if the socket needs an update
29         // if the socket is identified that means there's either a message or the connection has been closed/reset/terminated
30         fd_set readfds;
31         FD_ZERO( &readfds );
32         FD_SET( ( (unsigned int)sock->socket ), &readfds );
33         // from select man page:
34         // n is the highest-numbered descriptor in any of the three sets, plus 1
35         // (no use on windows)
36         switch ( select( sock->socket + 1, &readfds, 0, 0, &tout ) )
37         {
38         case SOCKET_ERROR:
39                 return -1;
40         case 0:
41                 return 0;
42         default:
43                 return 1;
44         }
45 }