]> icculus.org git repositories - divverent/netradiant.git/blob - radiant/sockets.cpp
NOW I do it right: #woxblox#
[divverent/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 {
19 // used for select()
20 #ifdef WIN32
21   TIMEVAL tout = { sec, usec };
22 #endif
23 #if defined (POSIX)
24         timeval tout;
25         tout.tv_sec = sec;
26         tout.tv_usec = usec;
27 #endif
28
29   // select() will identify if the socket needs an update
30   // if the socket is identified that means there's either a message or the connection has been closed/reset/terminated
31   fd_set readfds;
32   FD_ZERO(&readfds);
33   FD_SET(((unsigned int)sock->socket), &readfds);
34         // from select man page:
35         // n is the highest-numbered descriptor in any of the three sets, plus 1
36         // (no use on windows)
37   switch( select( sock->socket + 1, &readfds, 0, 0, &tout ) )
38   {
39   case SOCKET_ERROR:
40     return -1;
41   case 0:
42     return 0;
43   default:
44     return 1;
45   }
46 }
47