]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/kqueuemplexer.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/types.h>
31 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
32 #include <sys/event.h>
36 #include "namespaces.hh"
38 class KqueueFDMultiplexer
: public FDMultiplexer
41 KqueueFDMultiplexer();
42 virtual ~KqueueFDMultiplexer()
47 virtual int run(struct timeval
* tv
, int timeout
=500) override
;
48 virtual void getAvailableFDs(std::vector
<int>& fds
, int timeout
) override
;
50 virtual void addFD(callbackmap_t
& cbmap
, int fd
, callbackfunc_t toDo
, const boost::any
& parameter
, const struct timeval
* ttd
=nullptr) override
;
51 virtual void removeFD(callbackmap_t
& cbmap
, int fd
) override
;
52 string
getName() const override
58 boost::shared_array
<struct kevent
> d_kevents
;
59 static unsigned int s_maxevents
; // not a hard maximum
62 unsigned int KqueueFDMultiplexer::s_maxevents
=1024;
64 static FDMultiplexer
* make()
66 return new KqueueFDMultiplexer();
69 static struct KqueueRegisterOurselves
71 KqueueRegisterOurselves() {
72 FDMultiplexer::getMultiplexerMap().insert(make_pair(0, &make
)); // priority 0!
76 KqueueFDMultiplexer::KqueueFDMultiplexer() : d_kevents(new struct kevent
[s_maxevents
])
80 throw FDMultiplexerException("Setting up kqueue: "+stringerror());
83 void KqueueFDMultiplexer::addFD(callbackmap_t
& cbmap
, int fd
, callbackfunc_t toDo
, const boost::any
& parameter
, const struct timeval
* ttd
)
85 accountingAddFD(cbmap
, fd
, toDo
, parameter
, ttd
);
87 struct kevent kqevent
;
88 EV_SET(&kqevent
, fd
, (&cbmap
== &d_readCallbacks
) ? EVFILT_READ
: EVFILT_WRITE
, EV_ADD
, 0,0,0);
90 if(kevent(d_kqueuefd
, &kqevent
, 1, 0, 0, 0) < 0) {
92 throw FDMultiplexerException("Adding fd to kqueue set: "+stringerror());
96 void KqueueFDMultiplexer::removeFD(callbackmap_t
& cbmap
, int fd
)
98 accountingRemoveFD(cbmap
, fd
);
100 struct kevent kqevent
;
101 EV_SET(&kqevent
, fd
, (&cbmap
== &d_readCallbacks
) ? EVFILT_READ
: EVFILT_WRITE
, EV_DELETE
, 0,0,0);
103 if(kevent(d_kqueuefd
, &kqevent
, 1, 0, 0, 0) < 0) // ponder putting Callback back on the map..
104 throw FDMultiplexerException("Removing fd from kqueue set: "+stringerror());
107 void KqueueFDMultiplexer::getAvailableFDs(std::vector
<int>& fds
, int timeout
)
110 ts
.tv_sec
=timeout
/1000;
111 ts
.tv_nsec
=(timeout
% 1000) * 1000000;
113 int ret
= kevent(d_kqueuefd
, 0, 0, d_kevents
.get(), s_maxevents
, &ts
);
115 if(ret
< 0 && errno
!= EINTR
)
116 throw FDMultiplexerException("kqueue returned error: "+stringerror());
118 for(int n
=0; n
< ret
; ++n
) {
119 fds
.push_back(d_kevents
[n
].ident
);
123 int KqueueFDMultiplexer::run(struct timeval
* now
, int timeout
)
126 throw FDMultiplexerException("FDMultiplexer::run() is not reentrant!\n");
130 ts
.tv_sec
=timeout
/1000;
131 ts
.tv_nsec
=(timeout
% 1000) * 1000000;
133 int ret
=kevent(d_kqueuefd
, 0, 0, d_kevents
.get(), s_maxevents
, &ts
);
134 gettimeofday(now
,0); // MANDATORY!
136 if(ret
< 0 && errno
!=EINTR
)
137 throw FDMultiplexerException("kqueue returned error: "+stringerror());
139 if(ret
< 0) // nothing - thanks AB!
144 for(int n
=0; n
< ret
; ++n
) {
145 d_iter
=d_readCallbacks
.find(d_kevents
[n
].ident
);
146 if(d_iter
!= d_readCallbacks
.end()) {
147 d_iter
->d_callback(d_iter
->d_fd
, d_iter
->d_parameter
);
148 continue; // so we don't find ourselves as writable again
151 d_iter
=d_writeCallbacks
.find(d_kevents
[n
].ident
);
153 if(d_iter
!= d_writeCallbacks
.end()) {
154 d_iter
->d_callback(d_iter
->d_fd
, d_iter
->d_parameter
);
163 void acceptData(int fd
, boost::any
& parameter
)
165 cout
<<"Have data on fd "<<fd
<<endl
;
166 Socket
* sock
=boost::any_cast
<Socket
*>(parameter
);
169 sock
->recvFrom(packet
, rem
);
170 cout
<<"Received "<<packet
.size()<<" bytes!\n";
175 Socket
s(AF_INET
, SOCK_DGRAM
);
177 IPEndpoint
loc("0.0.0.0", 2000);
180 KqueueFDMultiplexer sfm
;
182 sfm
.addReadFD(s
.getHandle(), &acceptData
, &s
);
184 for(int n
=0; n
< 100 ; ++n
) {
187 sfm
.removeReadFD(s
.getHandle());
188 sfm
.removeReadFD(s
.getHandle());