OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_BASE_PHYSICALSOCKETSERVER_H__ | 11 #ifndef WEBRTC_BASE_PHYSICALSOCKETSERVER_H__ |
12 #define WEBRTC_BASE_PHYSICALSOCKETSERVER_H__ | 12 #define WEBRTC_BASE_PHYSICALSOCKETSERVER_H__ |
13 | 13 |
| 14 #if defined(WEBRTC_POSIX) && defined(WEBRTC_LINUX) |
| 15 #include <sys/epoll.h> |
| 16 #define WEBRTC_USE_EPOLL 1 |
| 17 #endif |
| 18 |
14 #include <memory> | 19 #include <memory> |
| 20 #include <set> |
15 #include <vector> | 21 #include <vector> |
16 | 22 |
17 #include "webrtc/base/nethelpers.h" | 23 #include "webrtc/base/nethelpers.h" |
18 #include "webrtc/base/socketserver.h" | 24 #include "webrtc/base/socketserver.h" |
19 #include "webrtc/base/criticalsection.h" | 25 #include "webrtc/base/criticalsection.h" |
20 | 26 |
21 #if defined(WEBRTC_POSIX) | 27 #if defined(WEBRTC_POSIX) |
22 typedef int SOCKET; | 28 typedef int SOCKET; |
23 #endif // WEBRTC_POSIX | 29 #endif // WEBRTC_POSIX |
24 | 30 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 75 |
70 // Internal Factory for Accept (virtual so it can be overwritten in tests). | 76 // Internal Factory for Accept (virtual so it can be overwritten in tests). |
71 virtual AsyncSocket* WrapSocket(SOCKET s); | 77 virtual AsyncSocket* WrapSocket(SOCKET s); |
72 | 78 |
73 // SocketServer: | 79 // SocketServer: |
74 bool Wait(int cms, bool process_io) override; | 80 bool Wait(int cms, bool process_io) override; |
75 void WakeUp() override; | 81 void WakeUp() override; |
76 | 82 |
77 void Add(Dispatcher* dispatcher); | 83 void Add(Dispatcher* dispatcher); |
78 void Remove(Dispatcher* dispatcher); | 84 void Remove(Dispatcher* dispatcher); |
| 85 void Update(Dispatcher* dispatcher); |
79 | 86 |
80 #if defined(WEBRTC_POSIX) | 87 #if defined(WEBRTC_POSIX) |
81 // Sets the function to be executed in response to the specified POSIX signal. | 88 // Sets the function to be executed in response to the specified POSIX signal. |
82 // The function is executed from inside Wait() using the "self-pipe trick"-- | 89 // The function is executed from inside Wait() using the "self-pipe trick"-- |
83 // regardless of which thread receives the signal--and hence can safely | 90 // regardless of which thread receives the signal--and hence can safely |
84 // manipulate user-level data structures. | 91 // manipulate user-level data structures. |
85 // "handler" may be SIG_IGN, SIG_DFL, or a user-specified function, just like | 92 // "handler" may be SIG_IGN, SIG_DFL, or a user-specified function, just like |
86 // with signal(2). | 93 // with signal(2). |
87 // Only one PhysicalSocketServer should have user-level signal handlers. | 94 // Only one PhysicalSocketServer should have user-level signal handlers. |
88 // Dispatching signals on multiple PhysicalSocketServers is not reliable. | 95 // Dispatching signals on multiple PhysicalSocketServers is not reliable. |
89 // The signal mask is not modified. It is the caller's responsibily to | 96 // The signal mask is not modified. It is the caller's responsibily to |
90 // maintain it as desired. | 97 // maintain it as desired. |
91 virtual bool SetPosixSignalHandler(int signum, void (*handler)(int)); | 98 virtual bool SetPosixSignalHandler(int signum, void (*handler)(int)); |
92 | 99 |
93 protected: | 100 protected: |
94 Dispatcher* signal_dispatcher(); | 101 Dispatcher* signal_dispatcher(); |
95 #endif | 102 #endif |
96 | 103 |
97 private: | 104 private: |
98 typedef std::vector<Dispatcher*> DispatcherList; | 105 typedef std::set<Dispatcher*> DispatcherSet; |
99 typedef std::vector<size_t*> IteratorList; | 106 |
| 107 void AddRemovePendingDispatchers(); |
100 | 108 |
101 #if defined(WEBRTC_POSIX) | 109 #if defined(WEBRTC_POSIX) |
| 110 bool WaitSelect(int cms, bool process_io); |
102 static bool InstallSignal(int signum, void (*handler)(int)); | 111 static bool InstallSignal(int signum, void (*handler)(int)); |
103 | 112 |
104 std::unique_ptr<PosixSignalDispatcher> signal_dispatcher_; | 113 std::unique_ptr<PosixSignalDispatcher> signal_dispatcher_; |
105 #endif | 114 #endif // WEBRTC_POSIX |
106 DispatcherList dispatchers_; | 115 #if defined(WEBRTC_USE_EPOLL) |
107 IteratorList iterators_; | 116 void AddEpoll(Dispatcher* dispatcher); |
| 117 void RemoveEpoll(Dispatcher* dispatcher); |
| 118 void UpdateEpoll(Dispatcher* dispatcher); |
| 119 bool WaitEpoll(int cms); |
| 120 bool WaitPoll(int cms, Dispatcher* dispatcher); |
| 121 |
| 122 int epoll_fd_ = INVALID_SOCKET; |
| 123 std::vector<struct epoll_event> epoll_events_; |
| 124 #endif // WEBRTC_USE_EPOLL |
| 125 DispatcherSet dispatchers_; |
| 126 DispatcherSet pending_add_dispatchers_; |
| 127 DispatcherSet pending_remove_dispatchers_; |
| 128 bool processing_dispatchers_ = false; |
108 Signaler* signal_wakeup_; | 129 Signaler* signal_wakeup_; |
109 CriticalSection crit_; | 130 CriticalSection crit_; |
110 bool fWait_; | 131 bool fWait_; |
111 #if defined(WEBRTC_WIN) | 132 #if defined(WEBRTC_WIN) |
112 WSAEVENT socket_ev_; | 133 WSAEVENT socket_ev_; |
113 #endif | 134 #endif |
114 }; | 135 }; |
115 | 136 |
116 class PhysicalSocket : public AsyncSocket, public sigslot::has_slots<> { | 137 class PhysicalSocket : public AsyncSocket, public sigslot::has_slots<> { |
117 public: | 138 public: |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 // Make virtual so ::sendto can be overwritten in tests. | 186 // Make virtual so ::sendto can be overwritten in tests. |
166 virtual int DoSendTo(SOCKET socket, const char* buf, int len, int flags, | 187 virtual int DoSendTo(SOCKET socket, const char* buf, int len, int flags, |
167 const struct sockaddr* dest_addr, socklen_t addrlen); | 188 const struct sockaddr* dest_addr, socklen_t addrlen); |
168 | 189 |
169 void OnResolveResult(AsyncResolverInterface* resolver); | 190 void OnResolveResult(AsyncResolverInterface* resolver); |
170 | 191 |
171 void UpdateLastError(); | 192 void UpdateLastError(); |
172 void MaybeRemapSendError(); | 193 void MaybeRemapSendError(); |
173 | 194 |
174 uint8_t enabled_events() const { return enabled_events_; } | 195 uint8_t enabled_events() const { return enabled_events_; } |
175 void SetEnabledEvents(uint8_t events); | 196 virtual void SetEnabledEvents(uint8_t events); |
176 void EnableEvents(uint8_t events); | 197 virtual void EnableEvents(uint8_t events); |
177 void DisableEvents(uint8_t events); | 198 virtual void DisableEvents(uint8_t events); |
178 | 199 |
179 static int TranslateOption(Option opt, int* slevel, int* sopt); | 200 static int TranslateOption(Option opt, int* slevel, int* sopt); |
180 | 201 |
181 PhysicalSocketServer* ss_; | 202 PhysicalSocketServer* ss_; |
182 SOCKET s_; | 203 SOCKET s_; |
183 bool udp_; | 204 bool udp_; |
184 CriticalSection crit_; | 205 CriticalSection crit_; |
185 int error_ GUARDED_BY(crit_); | 206 int error_ GUARDED_BY(crit_); |
186 ConnState state_; | 207 ConnState state_; |
187 AsyncResolver* resolver_; | 208 AsyncResolver* resolver_; |
(...skipping 25 matching lines...) Expand all Loading... |
213 int GetDescriptor() override; | 234 int GetDescriptor() override; |
214 bool IsDescriptorClosed() override; | 235 bool IsDescriptorClosed() override; |
215 #endif | 236 #endif |
216 | 237 |
217 uint32_t GetRequestedEvents() override; | 238 uint32_t GetRequestedEvents() override; |
218 void OnPreEvent(uint32_t ff) override; | 239 void OnPreEvent(uint32_t ff) override; |
219 void OnEvent(uint32_t ff, int err) override; | 240 void OnEvent(uint32_t ff, int err) override; |
220 | 241 |
221 int Close() override; | 242 int Close() override; |
222 | 243 |
| 244 #if defined(WEBRTC_USE_EPOLL) |
| 245 protected: |
| 246 void StartBatchedEventUpdates(); |
| 247 void FinishBatchedEventUpdates(); |
| 248 |
| 249 void SetEnabledEvents(uint8_t events) override; |
| 250 void EnableEvents(uint8_t events) override; |
| 251 void DisableEvents(uint8_t events) override; |
| 252 #endif |
| 253 |
| 254 private: |
223 #if defined(WEBRTC_WIN) | 255 #if defined(WEBRTC_WIN) |
224 private: | |
225 static int next_id_; | 256 static int next_id_; |
226 int id_; | 257 int id_; |
227 bool signal_close_; | 258 bool signal_close_; |
228 int signal_err_; | 259 int signal_err_; |
229 #endif // WEBRTC_WIN | 260 #endif // WEBRTC_WIN |
| 261 #if defined(WEBRTC_USE_EPOLL) |
| 262 void MaybeUpdateDispatcher(uint8_t old_events); |
| 263 |
| 264 int saved_enabled_events_ = -1; |
| 265 #endif |
230 }; | 266 }; |
231 | 267 |
232 } // namespace rtc | 268 } // namespace rtc |
233 | 269 |
234 #endif // WEBRTC_BASE_PHYSICALSOCKETSERVER_H__ | 270 #endif // WEBRTC_BASE_PHYSICALSOCKETSERVER_H__ |
OLD | NEW |