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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
#if defined(__sun__) && defined(__svr4__)
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <port.h>
#include <sys/port_impl.h>
#endif
#include <unistd.h>
#include "mplexer.hh"
#include "sstuff.hh"
#include <iostream>
#include "misc.hh"
#include "namespaces.hh"
class PortsFDMultiplexer : public FDMultiplexer
{
public:
PortsFDMultiplexer(unsigned int maxEventsHint);
~PortsFDMultiplexer()
{
close(d_portfd);
}
int run(struct timeval* tv, int timeout = 500) override;
void getAvailableFDs(std::vector<int>& fds, int timeout) override;
void addFD(int fd, FDMultiplexer::EventKind kind) override;
void removeFD(int fd, FDMultiplexer::EventKind kind) override;
string getName() const override
{
return "solaris completion ports";
}
private:
int d_portfd;
std::vector<port_event_t> d_pevents;
};
static FDMultiplexer* makePorts(unsigned int maxEventsHint)
{
return new PortsFDMultiplexer(maxEventsHint);
}
static struct PortsRegisterOurselves
{
PortsRegisterOurselves()
{
FDMultiplexer::getMultiplexerMap().emplace(0, &makePorts); // priority 0!
}
} doItPorts;
PortsFDMultiplexer::PortsFDMultiplexer(unsigned int maxEventsHint) :
d_pevents(maxEventsHint)
{
d_portfd = port_create(); // not hard max
if (d_portfd < 0) {
throw FDMultiplexerException("Setting up port: " + stringerror());
}
}
static int convertEventKind(FDMultiplexer::EventKind kind)
{
switch (kind) {
case FDMultiplexer::EventKind::Read:
return POLLIN;
case FDMultiplexer::EventKind::Write:
return POLLOUT;
case FDMultiplexer::EventKind::Both:
return POLLIN | POLLOUT;
}
throw std::runtime_error("Unhandled event kind in the ports multiplexer");
}
void PortsFDMultiplexer::addFD(int fd, FDMultiplexer::EventKind kind)
{
if (port_associate(d_portfd, PORT_SOURCE_FD, fd, convertEventKind(kind), 0) < 0) {
throw FDMultiplexerException("Adding fd to port set: " + stringerror());
}
}
void PortsFDMultiplexer::removeFD(int fd, FDMultiplexer::EventKind)
{
if (port_dissociate(d_portfd, PORT_SOURCE_FD, fd) < 0 && errno != ENOENT) { // it appears under some circumstances, ENOENT will be returned, without this being an error. Apache has this same "fix"
throw FDMultiplexerException("Removing fd from port set: " + stringerror());
}
}
void PortsFDMultiplexer::getAvailableFDs(std::vector<int>& fds, int timeout)
{
struct timespec timeoutspec;
timeoutspec.tv_sec = timeout / 1000;
timeoutspec.tv_nsec = (timeout % 1000) * 1000000;
unsigned int numevents = 1;
int ret = port_getn(d_portfd, d_pevents.data(), min(PORT_MAX_LIST, static_cast<int>(d_pevents.size())), &numevents, timeout != -1 ? &timeoutspec : nullptr);
/* port_getn has an unusual API - (ret == -1, errno == ETIME) can
mean partial success; you must check (*numevents) in this case
and process anything in there, otherwise you'll never see any
events from that object again. We don't care about pure timeouts
(ret == -1, errno == ETIME, *numevents == 0) so we don't bother
with that case. */
if (ret == -1 && errno != ETIME) {
if (errno != EINTR) {
throw FDMultiplexerException("completion port_getn returned error: " + stringerror());
}
// EINTR is not really an error
return;
}
if (numevents == 0) {
// nothing
return;
}
fds.reserve(numevents);
for (unsigned int n = 0; n < numevents; ++n) {
const auto fd = d_pevents[n].portev_object;
/* we need to re-associate the FD */
if ((d_pevents[n].portev_events & POLLIN || d_pevents[n].portev_events & POLLERR || d_pevents[n].portev_events & POLLHUP)) {
if (d_readCallbacks.count(fd)) {
if (port_associate(d_portfd, PORT_SOURCE_FD, fd, d_writeCallbacks.count(fd) > 0 ? POLLIN | POLLOUT : POLLIN, 0) < 0) {
throw FDMultiplexerException("Unable to add fd back to ports (read): " + stringerror());
}
}
}
else if ((d_pevents[n].portev_events & POLLOUT || d_pevents[n].portev_events & POLLERR)) {
if (d_writeCallbacks.count(fd)) {
if (port_associate(d_portfd, PORT_SOURCE_FD, fd, d_readCallbacks.count(fd) > 0 ? POLLIN | POLLOUT : POLLOUT, 0) < 0) {
throw FDMultiplexerException("Unable to add fd back to ports (write): " + stringerror());
}
}
}
else {
/* not registered, this is unexpected */
continue;
}
fds.push_back(fd);
}
}
int PortsFDMultiplexer::run(struct timeval* now, int timeout)
{
if (d_inrun) {
throw FDMultiplexerException("FDMultiplexer::run() is not reentrant!\n");
}
struct timespec timeoutspec;
timeoutspec.tv_sec = timeout / 1000;
timeoutspec.tv_nsec = (timeout % 1000) * 1000000;
unsigned int numevents = 1;
int ret = port_getn(d_portfd, d_pevents.data(), min(PORT_MAX_LIST, static_cast<int>(d_pevents.size())), &numevents, timeout != -1 ? &timeoutspec : nullptr);
/* port_getn has an unusual API - (ret == -1, errno == ETIME) can
mean partial success; you must check (*numevents) in this case
and process anything in there, otherwise you'll never see any
events from that object again. We don't care about pure timeouts
(ret == -1, errno == ETIME, *numevents == 0) so we don't bother
with that case. */
if (ret == -1 && errno != ETIME) {
if (errno != EINTR) {
throw FDMultiplexerException("completion port_getn returned error: " + stringerror());
}
// EINTR is not really an error
gettimeofday(now, nullptr);
return 0;
}
gettimeofday(now, nullptr);
if (!numevents) {
// nothing
return 0;
}
d_inrun = true;
int count = 0;
for (unsigned int n = 0; n < numevents; ++n) {
if (d_pevents[n].portev_events & POLLIN || d_pevents[n].portev_events & POLLERR || d_pevents[n].portev_events & POLLHUP) {
const auto& iter = d_readCallbacks.find(d_pevents[n].portev_object);
if (iter != d_readCallbacks.end()) {
iter->d_callback(iter->d_fd, iter->d_parameter);
count++;
if (d_readCallbacks.count(d_pevents[n].portev_object) && port_associate(d_portfd, PORT_SOURCE_FD, d_pevents[n].portev_object, d_writeCallbacks.count(d_pevents[n].portev_object) ? POLLIN | POLLOUT : POLLIN, 0) < 0) {
throw FDMultiplexerException("Unable to add fd back to ports (read): " + stringerror());
}
}
}
if (d_pevents[n].portev_events & POLLOUT || d_pevents[n].portev_events & POLLERR) {
const auto& iter = d_writeCallbacks.find(d_pevents[n].portev_object);
if (iter != d_writeCallbacks.end()) {
iter->d_callback(iter->d_fd, iter->d_parameter);
count++;
if (d_writeCallbacks.count(d_pevents[n].portev_object) && port_associate(d_portfd, PORT_SOURCE_FD, d_pevents[n].portev_object, d_readCallbacks.count(d_pevents[n].portev_object) ? POLLIN | POLLOUT : POLLOUT, 0) < 0) {
throw FDMultiplexerException("Unable to add fd back to ports (write): " + stringerror());
}
}
}
}
d_inrun = false;
return count;
}
#if 0
void acceptData(int fd, boost::any& parameter)
{
cout<<"Have data on fd "<<fd<<endl;
Socket* sock=boost::any_cast<Socket*>(parameter);
string packet;
IPEndpoint rem;
sock->recvFrom(packet, rem);
cout<<"Received "<<packet.size()<<" bytes!\n";
}
int main()
{
Socket s(AF_INET, SOCK_DGRAM);
IPEndpoint loc("0.0.0.0", 2000);
s.bind(loc);
PortsFDMultiplexer sfm;
sfm.addReadFD(s.getHandle(), &acceptData, &s);
for(int n=0; n < 100 ; ++n) {
sfm.run();
}
sfm.removeReadFD(s.getHandle());
sfm.removeReadFD(s.getHandle());
}
#endif
|