| 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 |
| 19 #include <memory> | 15 // This header is deprecated and is just left here temporarily during |
| 20 #include <set> | 16 // refactoring. See https://bugs.webrtc.org/7634 for more details. |
| 21 #include <vector> | 17 #include "webrtc/rtc_base/physicalsocketserver.h" |
| 22 | 18 |
| 23 #include "webrtc/base/nethelpers.h" | 19 #endif // WEBRTC_BASE_PHYSICALSOCKETSERVER_H_ |
| 24 #include "webrtc/base/socketserver.h" | |
| 25 #include "webrtc/base/criticalsection.h" | |
| 26 | |
| 27 #if defined(WEBRTC_POSIX) | |
| 28 typedef int SOCKET; | |
| 29 #endif // WEBRTC_POSIX | |
| 30 | |
| 31 namespace rtc { | |
| 32 | |
| 33 // Event constants for the Dispatcher class. | |
| 34 enum DispatcherEvent { | |
| 35 DE_READ = 0x0001, | |
| 36 DE_WRITE = 0x0002, | |
| 37 DE_CONNECT = 0x0004, | |
| 38 DE_CLOSE = 0x0008, | |
| 39 DE_ACCEPT = 0x0010, | |
| 40 }; | |
| 41 | |
| 42 class Signaler; | |
| 43 #if defined(WEBRTC_POSIX) | |
| 44 class PosixSignalDispatcher; | |
| 45 #endif | |
| 46 | |
| 47 class Dispatcher { | |
| 48 public: | |
| 49 virtual ~Dispatcher() {} | |
| 50 virtual uint32_t GetRequestedEvents() = 0; | |
| 51 virtual void OnPreEvent(uint32_t ff) = 0; | |
| 52 virtual void OnEvent(uint32_t ff, int err) = 0; | |
| 53 #if defined(WEBRTC_WIN) | |
| 54 virtual WSAEVENT GetWSAEvent() = 0; | |
| 55 virtual SOCKET GetSocket() = 0; | |
| 56 virtual bool CheckSignalClose() = 0; | |
| 57 #elif defined(WEBRTC_POSIX) | |
| 58 virtual int GetDescriptor() = 0; | |
| 59 virtual bool IsDescriptorClosed() = 0; | |
| 60 #endif | |
| 61 }; | |
| 62 | |
| 63 // A socket server that provides the real sockets of the underlying OS. | |
| 64 class PhysicalSocketServer : public SocketServer { | |
| 65 public: | |
| 66 PhysicalSocketServer(); | |
| 67 ~PhysicalSocketServer() override; | |
| 68 | |
| 69 // SocketFactory: | |
| 70 Socket* CreateSocket(int type) override; | |
| 71 Socket* CreateSocket(int family, int type) override; | |
| 72 | |
| 73 AsyncSocket* CreateAsyncSocket(int type) override; | |
| 74 AsyncSocket* CreateAsyncSocket(int family, int type) override; | |
| 75 | |
| 76 // Internal Factory for Accept (virtual so it can be overwritten in tests). | |
| 77 virtual AsyncSocket* WrapSocket(SOCKET s); | |
| 78 | |
| 79 // SocketServer: | |
| 80 bool Wait(int cms, bool process_io) override; | |
| 81 void WakeUp() override; | |
| 82 | |
| 83 void Add(Dispatcher* dispatcher); | |
| 84 void Remove(Dispatcher* dispatcher); | |
| 85 void Update(Dispatcher* dispatcher); | |
| 86 | |
| 87 #if defined(WEBRTC_POSIX) | |
| 88 // Sets the function to be executed in response to the specified POSIX signal. | |
| 89 // The function is executed from inside Wait() using the "self-pipe trick"-- | |
| 90 // regardless of which thread receives the signal--and hence can safely | |
| 91 // manipulate user-level data structures. | |
| 92 // "handler" may be SIG_IGN, SIG_DFL, or a user-specified function, just like | |
| 93 // with signal(2). | |
| 94 // Only one PhysicalSocketServer should have user-level signal handlers. | |
| 95 // Dispatching signals on multiple PhysicalSocketServers is not reliable. | |
| 96 // The signal mask is not modified. It is the caller's responsibily to | |
| 97 // maintain it as desired. | |
| 98 virtual bool SetPosixSignalHandler(int signum, void (*handler)(int)); | |
| 99 | |
| 100 protected: | |
| 101 Dispatcher* signal_dispatcher(); | |
| 102 #endif | |
| 103 | |
| 104 private: | |
| 105 typedef std::set<Dispatcher*> DispatcherSet; | |
| 106 | |
| 107 void AddRemovePendingDispatchers(); | |
| 108 | |
| 109 #if defined(WEBRTC_POSIX) | |
| 110 bool WaitSelect(int cms, bool process_io); | |
| 111 static bool InstallSignal(int signum, void (*handler)(int)); | |
| 112 | |
| 113 std::unique_ptr<PosixSignalDispatcher> signal_dispatcher_; | |
| 114 #endif // WEBRTC_POSIX | |
| 115 #if defined(WEBRTC_USE_EPOLL) | |
| 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; | |
| 129 Signaler* signal_wakeup_; | |
| 130 CriticalSection crit_; | |
| 131 bool fWait_; | |
| 132 #if defined(WEBRTC_WIN) | |
| 133 WSAEVENT socket_ev_; | |
| 134 #endif | |
| 135 }; | |
| 136 | |
| 137 class PhysicalSocket : public AsyncSocket, public sigslot::has_slots<> { | |
| 138 public: | |
| 139 PhysicalSocket(PhysicalSocketServer* ss, SOCKET s = INVALID_SOCKET); | |
| 140 ~PhysicalSocket() override; | |
| 141 | |
| 142 // Creates the underlying OS socket (same as the "socket" function). | |
| 143 virtual bool Create(int family, int type); | |
| 144 | |
| 145 SocketAddress GetLocalAddress() const override; | |
| 146 SocketAddress GetRemoteAddress() const override; | |
| 147 | |
| 148 int Bind(const SocketAddress& bind_addr) override; | |
| 149 int Connect(const SocketAddress& addr) override; | |
| 150 | |
| 151 int GetError() const override; | |
| 152 void SetError(int error) override; | |
| 153 | |
| 154 ConnState GetState() const override; | |
| 155 | |
| 156 int GetOption(Option opt, int* value) override; | |
| 157 int SetOption(Option opt, int value) override; | |
| 158 | |
| 159 int Send(const void* pv, size_t cb) override; | |
| 160 int SendTo(const void* buffer, | |
| 161 size_t length, | |
| 162 const SocketAddress& addr) override; | |
| 163 | |
| 164 int Recv(void* buffer, size_t length, int64_t* timestamp) override; | |
| 165 int RecvFrom(void* buffer, | |
| 166 size_t length, | |
| 167 SocketAddress* out_addr, | |
| 168 int64_t* timestamp) override; | |
| 169 | |
| 170 int Listen(int backlog) override; | |
| 171 AsyncSocket* Accept(SocketAddress* out_addr) override; | |
| 172 | |
| 173 int Close() override; | |
| 174 | |
| 175 SocketServer* socketserver() { return ss_; } | |
| 176 | |
| 177 protected: | |
| 178 int DoConnect(const SocketAddress& connect_addr); | |
| 179 | |
| 180 // Make virtual so ::accept can be overwritten in tests. | |
| 181 virtual SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen); | |
| 182 | |
| 183 // Make virtual so ::send can be overwritten in tests. | |
| 184 virtual int DoSend(SOCKET socket, const char* buf, int len, int flags); | |
| 185 | |
| 186 // Make virtual so ::sendto can be overwritten in tests. | |
| 187 virtual int DoSendTo(SOCKET socket, const char* buf, int len, int flags, | |
| 188 const struct sockaddr* dest_addr, socklen_t addrlen); | |
| 189 | |
| 190 void OnResolveResult(AsyncResolverInterface* resolver); | |
| 191 | |
| 192 void UpdateLastError(); | |
| 193 void MaybeRemapSendError(); | |
| 194 | |
| 195 uint8_t enabled_events() const { return enabled_events_; } | |
| 196 virtual void SetEnabledEvents(uint8_t events); | |
| 197 virtual void EnableEvents(uint8_t events); | |
| 198 virtual void DisableEvents(uint8_t events); | |
| 199 | |
| 200 static int TranslateOption(Option opt, int* slevel, int* sopt); | |
| 201 | |
| 202 PhysicalSocketServer* ss_; | |
| 203 SOCKET s_; | |
| 204 bool udp_; | |
| 205 CriticalSection crit_; | |
| 206 int error_ GUARDED_BY(crit_); | |
| 207 ConnState state_; | |
| 208 AsyncResolver* resolver_; | |
| 209 | |
| 210 #if !defined(NDEBUG) | |
| 211 std::string dbg_addr_; | |
| 212 #endif | |
| 213 | |
| 214 private: | |
| 215 uint8_t enabled_events_ = 0; | |
| 216 }; | |
| 217 | |
| 218 class SocketDispatcher : public Dispatcher, public PhysicalSocket { | |
| 219 public: | |
| 220 explicit SocketDispatcher(PhysicalSocketServer *ss); | |
| 221 SocketDispatcher(SOCKET s, PhysicalSocketServer *ss); | |
| 222 ~SocketDispatcher() override; | |
| 223 | |
| 224 bool Initialize(); | |
| 225 | |
| 226 virtual bool Create(int type); | |
| 227 bool Create(int family, int type) override; | |
| 228 | |
| 229 #if defined(WEBRTC_WIN) | |
| 230 WSAEVENT GetWSAEvent() override; | |
| 231 SOCKET GetSocket() override; | |
| 232 bool CheckSignalClose() override; | |
| 233 #elif defined(WEBRTC_POSIX) | |
| 234 int GetDescriptor() override; | |
| 235 bool IsDescriptorClosed() override; | |
| 236 #endif | |
| 237 | |
| 238 uint32_t GetRequestedEvents() override; | |
| 239 void OnPreEvent(uint32_t ff) override; | |
| 240 void OnEvent(uint32_t ff, int err) override; | |
| 241 | |
| 242 int Close() override; | |
| 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: | |
| 255 #if defined(WEBRTC_WIN) | |
| 256 static int next_id_; | |
| 257 int id_; | |
| 258 bool signal_close_; | |
| 259 int signal_err_; | |
| 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 | |
| 266 }; | |
| 267 | |
| 268 } // namespace rtc | |
| 269 | |
| 270 #endif // WEBRTC_BASE_PHYSICALSOCKETSERVER_H__ | |
| OLD | NEW |