cce867dba7e3c539592a4e0dc726d075964c23a6
Stefan Schuermans IO for Windows

Stefan Schuermans authored 7 years ago

1) /* Blinker
2)    Copyright 2011-2014 Stefan Schuermans <stefan@blinkenarea.org>
3)    Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4)    a blinkenarea.org project */
5) 
6) #include <winsock2.h>
7) #include <set>
8) 
9) #include "Io.h"
10) #include "Time.h"
11) 
12) namespace Blinker {
13) 
14) /**
15)  * @brief wait for I/O events
16)  * @param[in] read I/O objects to check for readability
17)  * @param[out] read I/O objects that are readable
18)  * @param[in] write I/O objects to check for writability
19)  * @param[out] write I/O objects that are writable
20)  * @param[in] timeout maximum time to wait
21)  */
22) void Io::wait(Set &read, Set &write, const Time &timeout)
23) {
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

24)   // get read set and write set, also count entries
25)   unsigned int rds = 0;
Stefan Schuermans IO for Windows

Stefan Schuermans authored 7 years ago

26)   fd_set fd_rd;
27)   FD_ZERO(&fd_rd);
28)   for (Set::const_iterator it = read.begin(); it != read.end(); ++it) {
29)     if ((*it)->m_socket != INVALID_SOCKET) {
30)       FD_SET((*it)->m_socket, &fd_rd);
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

31)       ++rds;
Stefan Schuermans IO for Windows

Stefan Schuermans authored 7 years ago

32)     }
33)   }
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

34)   unsigned int wrs = 0;
Stefan Schuermans IO for Windows

Stefan Schuermans authored 7 years ago

35)   fd_set fd_wr;
36)   FD_ZERO(&fd_wr);
37)   for (Set::const_iterator it = write.begin(); it != write.end(); ++it) {
38)     if ((*it)->m_socket != INVALID_SOCKET) {
39)       FD_SET((*it)->m_socket, &fd_wr);
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

40)       ++wrs;
Stefan Schuermans IO for Windows

Stefan Schuermans authored 7 years ago

41)     }
42)   }
43) 
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

44)   /* special case for no sockets to check,
45)      because select does not work for allsets empty on Windows */
46)   if (rds == 0 && wrs == 0) {
47)     if (timeout <= Time::zero) {
48)       return;
49)     }
50)     Sleep(timeout.toMs());
51)   }
52) 
Stefan Schuermans IO for Windows

Stefan Schuermans authored 7 years ago

53)   // get timeout
54)   struct timeval to;
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

55)   if (timeout < Time::zero) { // don't use negative timeout
Stefan Schuermans IO for Windows

Stefan Schuermans authored 7 years ago

56)     Time::zero.toTimeval(to);
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

57)   } else {
Stefan Schuermans IO for Windows

Stefan Schuermans authored 7 years ago

58)     timeout.toTimeval(to);
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

59)   }
Stefan Schuermans IO for Windows

Stefan Schuermans authored 7 years ago

60) 
61)   // wait for I/O event on sockets
62)   fd_set fd_err;
63)   FD_ZERO(&fd_err);
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

64)   int res = select(0 /* ignored on Windows */, &fd_rd, &fd_wr, &fd_err, &to);