MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / poll

Method poll

source/core/StarSocket.cpp:7–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5namespace Star {
6
7Maybe<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();

Callers 3

getWorldMethod · 0.45
triggerWorldCreationMethod · 0.45

Calls 11

netErrorStringFunction · 0.85
enumerateIteratorFunction · 0.85
emplaceAppendMethod · 0.80
doShutdownMethod · 0.80
formatFunction · 0.70
emptyMethod · 0.45
isOpenMethod · 0.45
sizeMethod · 0.45
getMethod · 0.45
addMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected