8b5ac66acae610418d15bc5e3e1c0e13eff3a2d5
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 maximum timeout in Io::...

Stefan Schuermans authored 6 years ago

24)   static const Time maxTimeout(1); // maximum timeout for I/O wait
Stefan Schuermans limit maximum timeout for s...

Stefan Schuermans authored 6 years ago

25) 
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 7 years ago

33)       ++rds;
Stefan Schuermans IO for Windows

Stefan Schuermans authored 7 years ago

34)     }
35)   }
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 7 years ago

42)       ++wrs;
Stefan Schuermans IO for Windows

Stefan Schuermans authored 7 years ago

43)     }
44)   }
45) 
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 7 years ago

55)   // get timeout
56)   struct timeval to;
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 7 years ago

58)     Time::zero.toTimeval(to);
Stefan Schuermans limit maximum timeout for s...

Stefan Schuermans authored 6 years ago

59)   } else if (timeout > maxTimeout) { // stay responsive -> no more than max
60)     maxTimeout.toTimeval(to);
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

61)   } else {
Stefan Schuermans IO for Windows

Stefan Schuermans authored 7 years ago

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

Stefan Schuermans authored 7 years ago

63)   }
Stefan Schuermans IO for Windows

Stefan Schuermans authored 7 years ago

64) 
65)   // wait for I/O event on sockets
66)   fd_set fd_err;
67)   FD_ZERO(&fd_err);
Stefan Schuermans fix Windows socket poll

Stefan Schuermans authored 7 years ago

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