]>
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);
49 virtual void addFD(callbackmap_t
& cbmap
, int fd
, callbackfunc_t toDo
, const boost::any
& parameter
);
50 virtual void removeFD(callbackmap_t
& cbmap
, int fd
);
57 boost::shared_array
<struct kevent
> d_kevents
;
58 static unsigned int s_maxevents
; // not a hard maximum
61 unsigned int KqueueFDMultiplexer::s_maxevents
=1024;
63 static FDMultiplexer
* make()
65 return new KqueueFDMultiplexer();
68 static struct KqueueRegisterOurselves
70 KqueueRegisterOurselves() {
71 FDMultiplexer::getMultiplexerMap().insert(make_pair(0, &make
)); // priority 0!
75 KqueueFDMultiplexer::KqueueFDMultiplexer() : d_kevents(new struct kevent
[s_maxevents
])
79 throw FDMultiplexerException("Setting up kqueue: "+stringerror());
82 void KqueueFDMultiplexer::addFD(callbackmap_t
& cbmap
, int fd
, callbackfunc_t toDo
, const boost::any
& parameter
)
84 accountingAddFD(cbmap
, fd
, toDo
, parameter
);
86 struct kevent kqevent
;
87 EV_SET(&kqevent
, fd
, (&cbmap
== &d_readCallbacks
) ? EVFILT_READ
: EVFILT_WRITE
, EV_ADD
, 0,0,0);
89 if(kevent(d_kqueuefd
, &kqevent
, 1, 0, 0, 0) < 0) {
91 throw FDMultiplexerException("Adding fd to kqueue set: "+stringerror());
95 void KqueueFDMultiplexer::removeFD(callbackmap_t
& cbmap
, int fd
)
97 accountingRemoveFD(cbmap
, fd
);
99 struct kevent kqevent
;
100 EV_SET(&kqevent
, fd
, (&cbmap
== &d_readCallbacks
) ? EVFILT_READ
: EVFILT_WRITE
, EV_DELETE
, 0,0,0);
102 if(kevent(d_kqueuefd
, &kqevent
, 1, 0, 0, 0) < 0) // ponder putting Callback back on the map..
103 throw FDMultiplexerException("Removing fd from kqueue set: "+stringerror());
106 int KqueueFDMultiplexer::run(struct timeval
* now
, int timeout
)
109 throw FDMultiplexerException("FDMultiplexer::run() is not reentrant!\n");
113 ts
.tv_sec
=timeout
/1000;
114 ts
.tv_nsec
=(timeout
% 1000) * 1000000;
116 int ret
=kevent(d_kqueuefd
, 0, 0, d_kevents
.get(), s_maxevents
, &ts
);
117 gettimeofday(now
,0); // MANDATORY!
119 if(ret
< 0 && errno
!=EINTR
)
120 throw FDMultiplexerException("kqueue returned error: "+stringerror());
122 if(ret
< 0) // nothing - thanks AB!
127 for(int n
=0; n
< ret
; ++n
) {
128 d_iter
=d_readCallbacks
.find(d_kevents
[n
].ident
);
129 if(d_iter
!= d_readCallbacks
.end()) {
130 d_iter
->second
.d_callback(d_iter
->first
, d_iter
->second
.d_parameter
);
131 continue; // so we don't find ourselves as writable again
134 d_iter
=d_writeCallbacks
.find(d_kevents
[n
].ident
);
136 if(d_iter
!= d_writeCallbacks
.end()) {
137 d_iter
->second
.d_callback(d_iter
->first
, d_iter
->second
.d_parameter
);
146 void acceptData(int fd
, boost::any
& parameter
)
148 cout
<<"Have data on fd "<<fd
<<endl
;
149 Socket
* sock
=boost::any_cast
<Socket
*>(parameter
);
152 sock
->recvFrom(packet
, rem
);
153 cout
<<"Received "<<packet
.size()<<" bytes!\n";
158 Socket
s(AF_INET
, SOCK_DGRAM
);
160 IPEndpoint
loc("0.0.0.0", 2000);
163 KqueueFDMultiplexer sfm
;
165 sfm
.addReadFD(s
.getHandle(), &acceptData
, &s
);
167 for(int n
=0; n
< 100 ; ++n
) {
170 sfm
.removeReadFD(s
.getHandle());
171 sfm
.removeReadFD(s
.getHandle());