/* Blinker
Copyright 2011-2014 Stefan Schuermans <stefan@blinkenarea.org>
Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
a blinkenarea.org project */
#include <winsock2.h>
#include <set>
#include "Io.h"
#include "Time.h"
namespace Blinker {
/**
* @brief wait for I/O events
* @param[in] read I/O objects to check for readability
* @param[out] read I/O objects that are readable
* @param[in] write I/O objects to check for writability
* @param[out] write I/O objects that are writable
* @param[in] timeout maximum time to wait
*/
void Io::wait(Set &read, Set &write, const Time &timeout)
{
// get read set and write set
fd_set fd_rd;
FD_ZERO(&fd_rd);
for (Set::const_iterator it = read.begin(); it != read.end(); ++it) {
if ((*it)->m_socket != INVALID_SOCKET) {
FD_SET((*it)->m_socket, &fd_rd);
}
}
fd_set fd_wr;
FD_ZERO(&fd_wr);
for (Set::const_iterator it = write.begin(); it != write.end(); ++it) {
if ((*it)->m_socket != INVALID_SOCKET) {
FD_SET((*it)->m_socket, &fd_wr);
}
}
// get timeout
struct timeval to;
if (timeout < Time::zero) // don't use negaitve timeout
Time::zero.toTimeval(to);
else
timeout.toTimeval(to);
// wait for I/O event on sockets
fd_set fd_err;
FD_ZERO(&fd_err);
int res = select(0 /* ignore on Windows */, &fd_rd, &fd_wr, &fd_err, &to);