1 /* <!-- copyright */
2 /*
3  * aria2 - The high speed download utility
4  *
5  * Copyright (C) 2010 Tatsuhiro Tsujikawa
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * In addition, as a special exception, the copyright holders give
22  * permission to link the code of portions of this program with the
23  * OpenSSL library under certain conditions as described in each
24  * individual source file, and distribute linked combinations
25  * including the two.
26  * You must obey the GNU General Public License in all respects
27  * for all of the code used other than OpenSSL.  If you modify
28  * file(s) with this exception, you may extend this exception to your
29  * version of the file(s), but you are not obligated to do so.  If you
30  * do not wish to do so, delete this exception statement from your
31  * version.  If you delete this exception statement from all source
32  * files in the program, then also delete it here.
33  */
34 /* copyright --> */
35 #ifndef D_PORT_EVENT_POLL_H
36 #define D_PORT_EVENT_POLL_H
37 
38 #include "EventPoll.h"
39 
40 #ifdef HAVE_PORT_H
41 #  include <port.h>
42 #endif // HAVE_PORT_H
43 
44 #include <set>
45 
46 #include "Event.h"
47 #include "a2functional.h"
48 #ifdef ENABLE_ASYNC_DNS
49 #  include "AsyncNameResolver.h"
50 #endif // ENABLE_ASYNC_DNS
51 
52 namespace aria2 {
53 
54 class PortEventPoll : public EventPoll {
55 private:
56   class KSocketEntry;
57 
58   typedef Event<KSocketEntry> KEvent;
59   typedef CommandEvent<KSocketEntry, PortEventPoll> KCommandEvent;
60   typedef ADNSEvent<KSocketEntry, PortEventPoll> KADNSEvent;
61   typedef AsyncNameResolverEntry<PortEventPoll> KAsyncNameResolverEntry;
62   friend class AsyncNameResolverEntry<PortEventPoll>;
63 
64   struct A2PortEvent {
65     int events;
66     KSocketEntry* socketEntry;
67   };
68 
69   class KSocketEntry : public SocketEntry<KCommandEvent, KADNSEvent> {
70   public:
71     KSocketEntry(sock_t socket);
72 
73     A2PortEvent getEvents();
74   };
75 
76   friend int accumulateEvent(int events, const KEvent& event);
77 
78 private:
79   typedef std::set<std::shared_ptr<KSocketEntry>,
80                    DerefLess<std::shared_ptr<KSocketEntry>>>
81       KSocketEntrySet;
82   KSocketEntrySet socketEntries_;
83 #ifdef ENABLE_ASYNC_DNS
84   typedef std::set<std::shared_ptr<KAsyncNameResolverEntry>,
85                    DerefLess<std::shared_ptr<KAsyncNameResolverEntry>>>
86       KAsyncNameResolverEntrySet;
87   KAsyncNameResolverEntrySet nameResolverEntries_;
88 #endif // ENABLE_ASYNC_DNS
89 
90   int port_;
91 
92   size_t portEventsSize_;
93 
94   port_event_t* portEvents_;
95 
96   static const size_t PORT_EVENTS_SIZE = 1024;
97 
98   bool addEvents(sock_t socket, const KEvent& event);
99 
100   bool deleteEvents(sock_t socket, const KEvent& event);
101 
102   bool addEvents(sock_t socket, Command* command, int events,
103                  const std::shared_ptr<AsyncNameResolver>& rs);
104 
105   bool deleteEvents(sock_t socket, Command* command,
106                     const std::shared_ptr<AsyncNameResolver>& rs);
107 
108 public:
109   PortEventPoll();
110 
111   bool good() const;
112 
113   virtual ~PortEventPoll();
114 
115   virtual void poll(const struct timeval& tv) CXX11_OVERRIDE;
116 
117   virtual bool addEvents(sock_t socket, Command* command,
118                          EventPoll::EventType events) CXX11_OVERRIDE;
119 
120   virtual bool deleteEvents(sock_t socket, Command* command,
121                             EventPoll::EventType events) CXX11_OVERRIDE;
122 #ifdef ENABLE_ASYNC_DNS
123 
124   virtual bool
125   addNameResolver(const std::shared_ptr<AsyncNameResolver>& resolver,
126                   Command* command) CXX11_OVERRIDE;
127   virtual bool
128   deleteNameResolver(const std::shared_ptr<AsyncNameResolver>& resolver,
129                      Command* command) CXX11_OVERRIDE;
130 #endif // ENABLE_ASYNC_DNS
131 
132   static const int IEV_READ = POLLIN;
133   static const int IEV_WRITE = POLLOUT;
134   static const int IEV_ERROR = POLLERR;
135   static const int IEV_HUP = POLLHUP;
136 };
137 
138 } // namespace aria2
139 
140 #endif // D_PORT_EVENT_POLL_H
141