BlinkenArea - GitList
Repositories
Blog
Wiki
Blinker
Code
Commits
Branches
Tags
Search
Tree:
47efe3a
Branches
Tags
master
Blinker
src
windows
Io.cpp
IO for Windows
Stefan Schuermans
commited
47efe3a
at 2017-10-28 20:23:07
Io.cpp
Blame
History
Raw
/* 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); // error or timeout if (res <= 0) { // return with empty sets read.clear(); write.clear(); return; } // remove file descriptors without event pending from sets Set::iterator it; it = read.begin(); while (it != read.end()) { if ((*it)->m_socket != INVALID_SOCKET && FD_ISSET((*it)->m_socket, &fd_rd)) { ++it; } else { Set::iterator del = it; ++it; read.erase(del); } } // while it it = write.begin(); while (it != write.end()) { if ((*it)->m_socket != INVALID_SOCKET && FD_ISSET((*it)->m_socket, &fd_wr)) { ++it; } else { Set::iterator del = it; ++it; write.erase(del); } } // while it } /// constructor Io::Io(): m_socket(INVALID_SOCKET) { } } // namespace Blinker