henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_PHYSICAL_SOCKET_SERVER_H_ |
| 12 | #define RTC_BASE_PHYSICAL_SOCKET_SERVER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #if defined(WEBRTC_POSIX) && defined(WEBRTC_LINUX) |
| 15 | #include <sys/epoll.h> |
| 16 | #define WEBRTC_USE_EPOLL 1 |
| 17 | #endif |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 18 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 19 | #include <memory> |
| 20 | #include <set> |
| 21 | #include <vector> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/critical_section.h" |
| 24 | #include "rtc_base/net_helpers.h" |
| 25 | #include "rtc_base/socket_server.h" |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 26 | #include "rtc_base/system/rtc_export.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 27 | |
| 28 | #if defined(WEBRTC_POSIX) |
| 29 | typedef int SOCKET; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 30 | #endif // WEBRTC_POSIX |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 31 | |
| 32 | namespace rtc { |
| 33 | |
| 34 | // Event constants for the Dispatcher class. |
| 35 | enum DispatcherEvent { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 36 | DE_READ = 0x0001, |
| 37 | DE_WRITE = 0x0002, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 38 | DE_CONNECT = 0x0004, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 39 | DE_CLOSE = 0x0008, |
| 40 | DE_ACCEPT = 0x0010, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | class Signaler; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 44 | |
| 45 | class Dispatcher { |
| 46 | public: |
| 47 | virtual ~Dispatcher() {} |
| 48 | virtual uint32_t GetRequestedEvents() = 0; |
| 49 | virtual void OnPreEvent(uint32_t ff) = 0; |
| 50 | virtual void OnEvent(uint32_t ff, int err) = 0; |
| 51 | #if defined(WEBRTC_WIN) |
| 52 | virtual WSAEVENT GetWSAEvent() = 0; |
| 53 | virtual SOCKET GetSocket() = 0; |
| 54 | virtual bool CheckSignalClose() = 0; |
| 55 | #elif defined(WEBRTC_POSIX) |
| 56 | virtual int GetDescriptor() = 0; |
| 57 | virtual bool IsDescriptorClosed() = 0; |
| 58 | #endif |
| 59 | }; |
| 60 | |
| 61 | // A socket server that provides the real sockets of the underlying OS. |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 62 | class RTC_EXPORT PhysicalSocketServer : public SocketServer { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 63 | public: |
| 64 | PhysicalSocketServer(); |
| 65 | ~PhysicalSocketServer() override; |
| 66 | |
| 67 | // SocketFactory: |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 68 | Socket* CreateSocket(int family, int type) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 69 | AsyncSocket* CreateAsyncSocket(int family, int type) override; |
| 70 | |
| 71 | // Internal Factory for Accept (virtual so it can be overwritten in tests). |
| 72 | virtual AsyncSocket* WrapSocket(SOCKET s); |
| 73 | |
| 74 | // SocketServer: |
| 75 | bool Wait(int cms, bool process_io) override; |
| 76 | void WakeUp() override; |
| 77 | |
| 78 | void Add(Dispatcher* dispatcher); |
| 79 | void Remove(Dispatcher* dispatcher); |
| 80 | void Update(Dispatcher* dispatcher); |
| 81 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 82 | private: |
| 83 | typedef std::set<Dispatcher*> DispatcherSet; |
| 84 | |
| 85 | void AddRemovePendingDispatchers(); |
| 86 | |
| 87 | #if defined(WEBRTC_POSIX) |
| 88 | bool WaitSelect(int cms, bool process_io); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 89 | #endif // WEBRTC_POSIX |
| 90 | #if defined(WEBRTC_USE_EPOLL) |
| 91 | void AddEpoll(Dispatcher* dispatcher); |
| 92 | void RemoveEpoll(Dispatcher* dispatcher); |
| 93 | void UpdateEpoll(Dispatcher* dispatcher); |
| 94 | bool WaitEpoll(int cms); |
| 95 | bool WaitPoll(int cms, Dispatcher* dispatcher); |
| 96 | |
Niels Möller | 611fba4 | 2020-05-13 14:42:22 +0200 | [diff] [blame] | 97 | const int epoll_fd_ = INVALID_SOCKET; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 98 | std::vector<struct epoll_event> epoll_events_; |
| 99 | #endif // WEBRTC_USE_EPOLL |
| 100 | DispatcherSet dispatchers_; |
| 101 | DispatcherSet pending_add_dispatchers_; |
| 102 | DispatcherSet pending_remove_dispatchers_; |
| 103 | bool processing_dispatchers_ = false; |
| 104 | Signaler* signal_wakeup_; |
| 105 | CriticalSection crit_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 106 | #if defined(WEBRTC_WIN) |
Niels Möller | 611fba4 | 2020-05-13 14:42:22 +0200 | [diff] [blame] | 107 | const WSAEVENT socket_ev_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 108 | #endif |
Niels Möller | 611fba4 | 2020-05-13 14:42:22 +0200 | [diff] [blame] | 109 | bool fWait_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | class PhysicalSocket : public AsyncSocket, public sigslot::has_slots<> { |
| 113 | public: |
| 114 | PhysicalSocket(PhysicalSocketServer* ss, SOCKET s = INVALID_SOCKET); |
| 115 | ~PhysicalSocket() override; |
| 116 | |
| 117 | // Creates the underlying OS socket (same as the "socket" function). |
| 118 | virtual bool Create(int family, int type); |
| 119 | |
| 120 | SocketAddress GetLocalAddress() const override; |
| 121 | SocketAddress GetRemoteAddress() const override; |
| 122 | |
| 123 | int Bind(const SocketAddress& bind_addr) override; |
| 124 | int Connect(const SocketAddress& addr) override; |
| 125 | |
| 126 | int GetError() const override; |
| 127 | void SetError(int error) override; |
| 128 | |
| 129 | ConnState GetState() const override; |
| 130 | |
| 131 | int GetOption(Option opt, int* value) override; |
| 132 | int SetOption(Option opt, int value) override; |
| 133 | |
| 134 | int Send(const void* pv, size_t cb) override; |
| 135 | int SendTo(const void* buffer, |
| 136 | size_t length, |
| 137 | const SocketAddress& addr) override; |
| 138 | |
| 139 | int Recv(void* buffer, size_t length, int64_t* timestamp) override; |
| 140 | int RecvFrom(void* buffer, |
| 141 | size_t length, |
| 142 | SocketAddress* out_addr, |
| 143 | int64_t* timestamp) override; |
| 144 | |
| 145 | int Listen(int backlog) override; |
| 146 | AsyncSocket* Accept(SocketAddress* out_addr) override; |
| 147 | |
| 148 | int Close() override; |
| 149 | |
| 150 | SocketServer* socketserver() { return ss_; } |
| 151 | |
| 152 | protected: |
| 153 | int DoConnect(const SocketAddress& connect_addr); |
| 154 | |
| 155 | // Make virtual so ::accept can be overwritten in tests. |
| 156 | virtual SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen); |
| 157 | |
| 158 | // Make virtual so ::send can be overwritten in tests. |
| 159 | virtual int DoSend(SOCKET socket, const char* buf, int len, int flags); |
| 160 | |
| 161 | // Make virtual so ::sendto can be overwritten in tests. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 162 | virtual int DoSendTo(SOCKET socket, |
| 163 | const char* buf, |
| 164 | int len, |
| 165 | int flags, |
| 166 | const struct sockaddr* dest_addr, |
| 167 | socklen_t addrlen); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 168 | |
| 169 | void OnResolveResult(AsyncResolverInterface* resolver); |
| 170 | |
| 171 | void UpdateLastError(); |
| 172 | void MaybeRemapSendError(); |
| 173 | |
| 174 | uint8_t enabled_events() const { return enabled_events_; } |
| 175 | virtual void SetEnabledEvents(uint8_t events); |
| 176 | virtual void EnableEvents(uint8_t events); |
| 177 | virtual void DisableEvents(uint8_t events); |
| 178 | |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 179 | int TranslateOption(Option opt, int* slevel, int* sopt); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 180 | |
| 181 | PhysicalSocketServer* ss_; |
| 182 | SOCKET s_; |
| 183 | bool udp_; |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 184 | int family_ = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 185 | CriticalSection crit_; |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 186 | int error_ RTC_GUARDED_BY(crit_); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 187 | ConnState state_; |
| 188 | AsyncResolver* resolver_; |
| 189 | |
| 190 | #if !defined(NDEBUG) |
| 191 | std::string dbg_addr_; |
| 192 | #endif |
| 193 | |
| 194 | private: |
| 195 | uint8_t enabled_events_ = 0; |
| 196 | }; |
| 197 | |
| 198 | class SocketDispatcher : public Dispatcher, public PhysicalSocket { |
| 199 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 200 | explicit SocketDispatcher(PhysicalSocketServer* ss); |
| 201 | SocketDispatcher(SOCKET s, PhysicalSocketServer* ss); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 202 | ~SocketDispatcher() override; |
| 203 | |
| 204 | bool Initialize(); |
| 205 | |
| 206 | virtual bool Create(int type); |
| 207 | bool Create(int family, int type) override; |
| 208 | |
| 209 | #if defined(WEBRTC_WIN) |
| 210 | WSAEVENT GetWSAEvent() override; |
| 211 | SOCKET GetSocket() override; |
| 212 | bool CheckSignalClose() override; |
| 213 | #elif defined(WEBRTC_POSIX) |
| 214 | int GetDescriptor() override; |
| 215 | bool IsDescriptorClosed() override; |
| 216 | #endif |
| 217 | |
| 218 | uint32_t GetRequestedEvents() override; |
| 219 | void OnPreEvent(uint32_t ff) override; |
| 220 | void OnEvent(uint32_t ff, int err) override; |
| 221 | |
| 222 | int Close() override; |
| 223 | |
| 224 | #if defined(WEBRTC_USE_EPOLL) |
| 225 | protected: |
| 226 | void StartBatchedEventUpdates(); |
| 227 | void FinishBatchedEventUpdates(); |
| 228 | |
| 229 | void SetEnabledEvents(uint8_t events) override; |
| 230 | void EnableEvents(uint8_t events) override; |
| 231 | void DisableEvents(uint8_t events) override; |
| 232 | #endif |
| 233 | |
| 234 | private: |
| 235 | #if defined(WEBRTC_WIN) |
| 236 | static int next_id_; |
| 237 | int id_; |
| 238 | bool signal_close_; |
| 239 | int signal_err_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 240 | #endif // WEBRTC_WIN |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 241 | #if defined(WEBRTC_USE_EPOLL) |
| 242 | void MaybeUpdateDispatcher(uint8_t old_events); |
| 243 | |
| 244 | int saved_enabled_events_ = -1; |
| 245 | #endif |
| 246 | }; |
| 247 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 248 | } // namespace rtc |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 249 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 250 | #endif // RTC_BASE_PHYSICAL_SOCKET_SERVER_H_ |