1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/*
* Copyright 2003-2017 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#ifdef USE_WINSELECT
#include "PollGroupWinSelect.hxx"
constexpr int EVENT_READ = 0;
constexpr int EVENT_WRITE = 1;
static inline bool HasEvent(unsigned events, int event_id)
{
return (events & (1 << event_id)) != 0;
}
PollGroupWinSelect::PollGroupWinSelect() { }
PollGroupWinSelect::~PollGroupWinSelect() { }
bool PollGroupWinSelect::CanModify(PollGroupWinSelect::Item &item,
unsigned events, int event_id)
{
if (item.index[event_id] < 0 && HasEvent(events, event_id))
return !event_set[event_id].IsFull();
return true;
}
void PollGroupWinSelect::Modify(PollGroupWinSelect::Item &item, int fd,
unsigned events, int event_id)
{
int index = item.index[event_id];
auto &set = event_set[event_id];
if (index < 0 && HasEvent(events, event_id))
item.index[event_id] = set.Add(fd);
else if (index >= 0 && !HasEvent(events, event_id)) {
if (index != set.Size() - 1) {
set.MoveToEnd(index);
items[set[index]].index[event_id] = index;
}
set.RemoveLast();
item.index[event_id] = -1;
}
}
bool PollGroupWinSelect::Add(int fd, unsigned events, void *obj)
{
assert(items.find(fd) == items.end());
auto &item = items[fd];
item.index[EVENT_READ] = -1;
item.index[EVENT_WRITE] = -1;
item.obj = obj;
item.events = 0;
if (!CanModify(item, events, EVENT_READ)) {
items.erase(fd);
return false;
}
if (!CanModify(item, events, EVENT_WRITE)) {
items.erase(fd);
return false;
}
Modify(item, fd, events, EVENT_READ);
Modify(item, fd, events, EVENT_WRITE);
return true;
}
bool PollGroupWinSelect::Modify(int fd, unsigned events, void *obj)
{
auto item_iter = items.find(fd);
assert(item_iter != items.end());
auto &item = item_iter->second;
if (!CanModify(item, events, EVENT_READ))
return false;
if (!CanModify(item, events, EVENT_WRITE))
return false;
item.obj = obj;
Modify(item, fd, events, EVENT_READ);
Modify(item, fd, events, EVENT_WRITE);
return true;
}
bool PollGroupWinSelect::Remove(int fd)
{
auto item_iter = items.find(fd);
assert(item_iter != items.end());
auto &item = item_iter->second;
Modify(item, fd, 0, EVENT_READ);
Modify(item, fd, 0, EVENT_WRITE);
items.erase(item_iter);
return true;
}
void PollGroupWinSelect::ReadEvents(PollResultGeneric &result, int timeout_ms)
{
bool use_sleep = event_set[EVENT_READ].IsEmpty() &&
event_set[EVENT_WRITE].IsEmpty();
if (use_sleep) {
Sleep(timeout_ms < 0 ? INFINITE : (DWORD) timeout_ms);
return;
}
SocketSet read_set(event_set[EVENT_READ]);
SocketSet write_set(event_set[EVENT_WRITE]);
SocketSet except_set(event_set[EVENT_WRITE]);
timeval tv;
if (timeout_ms >= 0) {
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
}
int ret = select(0,
read_set.IsEmpty() ? nullptr : read_set.GetPtr(),
write_set.IsEmpty() ? nullptr : write_set.GetPtr(),
except_set.IsEmpty() ? nullptr : except_set.GetPtr(),
timeout_ms < 0 ? nullptr : &tv);
if (ret == 0 || ret == SOCKET_ERROR)
return;
for (int i = 0; i < read_set.Size(); ++i)
items[read_set[i]].events |= READ;
for (int i = 0; i < write_set.Size(); ++i)
items[write_set[i]].events |= WRITE;
for (int i = 0; i < except_set.Size(); ++i)
items[except_set[i]].events |= WRITE;
for (auto i = items.begin(); i != items.end(); ++i)
if (i->second.events != 0) {
result.Add(i->second.events, i->second.obj);
i->second.events = 0;
}
}
#endif
|