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