| 5 | namespace Star { |
| 6 | |
| 7 | Maybe<SocketPollResult> Socket::poll(SocketPollQuery const& query, unsigned timeout) { |
| 8 | if (query.empty()) |
| 9 | return {}; |
| 10 | |
| 11 | // Prevent close from being called on any socket during this call. |
| 12 | LinkedList<ReadLocker> readLockers; |
| 13 | for (auto const& p : query) |
| 14 | readLockers.emplaceAppend(p.first->m_mutex); |
| 15 | |
| 16 | // If any sockets are already closed, then this is an "event" according to |
| 17 | // this api but we cannot call poll on a closed socket, so just poll the rest |
| 18 | // of the sockets with no wait. |
| 19 | SocketPollResult result; |
| 20 | for (auto const& p : query) { |
| 21 | if (!p.first->isOpen()) { |
| 22 | result[p.first].exception = true; |
| 23 | timeout = 0; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | #ifdef STAR_SYSTEM_FAMILY_WINDOWS |
| 28 | fd_set readfs; |
| 29 | fd_set writefs; |
| 30 | fd_set exceptfs; |
| 31 | |
| 32 | FD_ZERO(&readfs); |
| 33 | FD_ZERO(&writefs); |
| 34 | FD_ZERO(&exceptfs); |
| 35 | |
| 36 | int ret; |
| 37 | for (auto const& p : query) { |
| 38 | if (p.first->isOpen()) { |
| 39 | if (p.second.readable) |
| 40 | FD_SET(p.first->m_impl->socketDesc, &readfs); |
| 41 | if (p.second.writable) |
| 42 | FD_SET(p.first->m_impl->socketDesc, &writefs); |
| 43 | FD_SET(p.first->m_impl->socketDesc, &exceptfs); |
| 44 | } |
| 45 | } |
| 46 | timeval time; |
| 47 | time.tv_usec = (timeout % 1000) * 1000; |
| 48 | time.tv_sec = timeout - timeout % 1000; |
| 49 | ret = ::select(0, &readfs, &writefs, &exceptfs, &time); |
| 50 | |
| 51 | if (ret < 0) |
| 52 | throw NetworkException::format("Error during call to select, '{}'", netErrorString()); |
| 53 | |
| 54 | if (ret == 0) |
| 55 | return {}; |
| 56 | |
| 57 | for (auto const& p : query) { |
| 58 | if (p.first->isOpen()) { |
| 59 | auto& r = result[p.first]; |
| 60 | r.readable = FD_ISSET(p.first->m_impl->socketDesc, &readfs); |
| 61 | r.writable = FD_ISSET(p.first->m_impl->socketDesc, &writefs); |
| 62 | r.exception = FD_ISSET(p.first->m_impl->socketDesc, &exceptfs); |
| 63 | if (r.exception) |
| 64 | p.first->doShutdown(); |
no test coverage detected