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