Debian Codegraph @

radiant/sockets.cpp

raw
1 #include "sockets.h"
2 #include "globaldefs.h"
3
4 #if GDEF_OS_WINDOWS
5 #include <winsock2.h>
6 #elif GDEF_OS_POSIX
7 #include <sys/time.h>
8 const int SOCKET_ERROR = -1;
9 #else
10 #error "unsupported platform"
11 #endif
12
13 #if GDEF_OS_MACOS
14 #include <unistd.h>
15 #endif
16
17 int Net_Wait( socket_t *sock, long sec, long usec ){
18 // used for select()
19 #if GDEF_OS_WINDOWS
20 TIMEVAL tout = { sec, usec };
21 #endif
22 #if GDEF_OS_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 }