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.
22 #ifndef PDNS_MPLEXER_HH
23 #define PDNS_MPLEXER_HH
24 #include <boost/function.hpp>
25 #include <boost/any.hpp>
26 #include <boost/shared_array.hpp>
27 #include <boost/tuple/tuple.hpp>
28 #include <boost/tuple/tuple_comparison.hpp>
35 class FDMultiplexerException : public std::runtime_error
38 FDMultiplexerException(const std::string& str) : std::runtime_error(str)
43 /** Very simple FD multiplexer, based on callbacks and boost::any parameters
44 As a special service, this parameter is kept around and can be modified,
45 allowing for state to be stored inside the multiplexer.
47 It has some "interesting" semantics
53 typedef boost::any funcparam_t;
56 typedef boost::function< void(int, funcparam_t&) > callbackfunc_t;
59 callbackfunc_t d_callback;
60 funcparam_t d_parameter;
65 FDMultiplexer() : d_inrun(false)
67 virtual ~FDMultiplexer()
70 /* tv will be updated to 'now' before run returns */
71 /* timeout is in ms */
72 virtual int run(struct timeval* tv, int timeout=500) = 0;
74 //! Add an fd to the read watch list - currently an fd can only be on one list at a time!
75 virtual void addReadFD(int fd, callbackfunc_t toDo, const funcparam_t& parameter=funcparam_t())
77 this->addFD(d_readCallbacks, fd, toDo, parameter);
80 //! Add an fd to the write watch list - currently an fd can only be on one list at a time!
81 virtual void addWriteFD(int fd, callbackfunc_t toDo, const funcparam_t& parameter=funcparam_t())
83 this->addFD(d_writeCallbacks, fd, toDo, parameter);
86 //! Remove an fd from the read watch list. You can't call this function on an fd that is closed already!
87 /** WARNING: references to 'parameter' become invalid after this function! */
88 virtual void removeReadFD(int fd)
90 this->removeFD(d_readCallbacks, fd);
93 //! Remove an fd from the write watch list. You can't call this function on an fd that is closed already!
94 /** WARNING: references to 'parameter' become invalid after this function! */
95 virtual void removeWriteFD(int fd)
97 this->removeFD(d_writeCallbacks, fd);
100 virtual void setReadTTD(int fd, struct timeval tv, int timeout)
102 if(!d_readCallbacks.count(fd))
103 throw FDMultiplexerException("attempt to timestamp fd not in the multiplexer");
104 tv.tv_sec += timeout;
105 d_readCallbacks[fd].d_ttd=tv;
108 virtual funcparam_t& getReadParameter(int fd)
110 if(!d_readCallbacks.count(fd))
111 throw FDMultiplexerException("attempt to look up data in multiplexer for unlisted fd "+std::to_string(fd));
112 return d_readCallbacks[fd].d_parameter;
115 virtual std::vector<std::pair<int, funcparam_t> > getTimeouts(const struct timeval& tv)
117 std::vector<std::pair<int, funcparam_t> > ret;
118 for(callbackmap_t::iterator i=d_readCallbacks.begin(); i!=d_readCallbacks.end(); ++i)
119 if(i->second.d_ttd.tv_sec && boost::tie(tv.tv_sec, tv.tv_usec) > boost::tie(i->second.d_ttd.tv_sec, i->second.d_ttd.tv_usec))
120 ret.push_back(std::make_pair(i->first, i->second.d_parameter));
124 typedef FDMultiplexer* getMultiplexer_t();
125 typedef std::multimap<int, getMultiplexer_t*> FDMultiplexermap_t;
127 static FDMultiplexermap_t& getMultiplexerMap()
129 static FDMultiplexermap_t theMap;
133 virtual std::string getName() = 0;
136 typedef std::map<int, Callback> callbackmap_t;
137 callbackmap_t d_readCallbacks, d_writeCallbacks;
139 virtual void addFD(callbackmap_t& cbmap, int fd, callbackfunc_t toDo, const funcparam_t& parameter)=0;
140 virtual void removeFD(callbackmap_t& cbmap, int fd)=0;
142 callbackmap_t::iterator d_iter;
144 void accountingAddFD(callbackmap_t& cbmap, int fd, callbackfunc_t toDo, const funcparam_t& parameter)
148 cb.d_parameter=parameter;
149 memset(&cb.d_ttd, 0, sizeof(cb.d_ttd));
152 throw FDMultiplexerException("Tried to add fd "+std::to_string(fd)+ " to multiplexer twice");
156 void accountingRemoveFD(callbackmap_t& cbmap, int fd)
159 throw FDMultiplexerException("Tried to remove unlisted fd "+std::to_string(fd)+ " from multiplexer");
163 class SelectFDMultiplexer : public FDMultiplexer
166 SelectFDMultiplexer()
168 virtual ~SelectFDMultiplexer()
171 virtual int run(struct timeval* tv, int timeout=500);
173 virtual void addFD(callbackmap_t& cbmap, int fd, callbackfunc_t toDo, const funcparam_t& parameter);
174 virtual void removeFD(callbackmap_t& cbmap, int fd);
175 std::string getName()