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 | */ |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 10 | #include "webrtc/base/physicalsocketserver.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 11 | |
| 12 | #if defined(_MSC_VER) && _MSC_VER < 1300 |
| 13 | #pragma warning(disable:4786) |
| 14 | #endif |
| 15 | |
| 16 | #include <assert.h> |
| 17 | |
pbos@webrtc.org | 27e5898 | 2014-10-07 17:56:53 +0000 | [diff] [blame] | 18 | #ifdef MEMORY_SANITIZER |
| 19 | #include <sanitizer/msan_interface.h> |
| 20 | #endif |
| 21 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | #if defined(WEBRTC_POSIX) |
| 23 | #include <string.h> |
| 24 | #include <errno.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/select.h> |
| 28 | #include <unistd.h> |
| 29 | #include <signal.h> |
| 30 | #endif |
| 31 | |
| 32 | #if defined(WEBRTC_WIN) |
| 33 | #define WIN32_LEAN_AND_MEAN |
| 34 | #include <windows.h> |
| 35 | #include <winsock2.h> |
| 36 | #include <ws2tcpip.h> |
| 37 | #undef SetPort |
| 38 | #endif |
| 39 | |
| 40 | #include <algorithm> |
| 41 | #include <map> |
| 42 | |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 43 | #include "webrtc/base/arraysize.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | #include "webrtc/base/basictypes.h" |
| 45 | #include "webrtc/base/byteorder.h" |
| 46 | #include "webrtc/base/common.h" |
| 47 | #include "webrtc/base/logging.h" |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 48 | #include "webrtc/base/networkmonitor.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 49 | #include "webrtc/base/timeutils.h" |
| 50 | #include "webrtc/base/winping.h" |
| 51 | #include "webrtc/base/win32socketinit.h" |
| 52 | |
| 53 | // stm: this will tell us if we are on OSX |
| 54 | #ifdef HAVE_CONFIG_H |
| 55 | #include "config.h" |
| 56 | #endif |
| 57 | |
| 58 | #if defined(WEBRTC_POSIX) |
| 59 | #include <netinet/tcp.h> // for TCP_NODELAY |
| 60 | #define IP_MTU 14 // Until this is integrated from linux/in.h to netinet/in.h |
| 61 | typedef void* SockOptArg; |
| 62 | #endif // WEBRTC_POSIX |
| 63 | |
| 64 | #if defined(WEBRTC_WIN) |
| 65 | typedef char* SockOptArg; |
| 66 | #endif |
| 67 | |
| 68 | namespace rtc { |
| 69 | |
| 70 | #if defined(WEBRTC_WIN) |
| 71 | // Standard MTUs, from RFC 1191 |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 72 | const uint16_t PACKET_MAXIMUMS[] = { |
| 73 | 65535, // Theoretical maximum, Hyperchannel |
| 74 | 32000, // Nothing |
| 75 | 17914, // 16Mb IBM Token Ring |
| 76 | 8166, // IEEE 802.4 |
| 77 | // 4464, // IEEE 802.5 (4Mb max) |
| 78 | 4352, // FDDI |
| 79 | // 2048, // Wideband Network |
| 80 | 2002, // IEEE 802.5 (4Mb recommended) |
| 81 | // 1536, // Expermental Ethernet Networks |
| 82 | // 1500, // Ethernet, Point-to-Point (default) |
| 83 | 1492, // IEEE 802.3 |
| 84 | 1006, // SLIP, ARPANET |
| 85 | // 576, // X.25 Networks |
| 86 | // 544, // DEC IP Portal |
| 87 | // 512, // NETBIOS |
| 88 | 508, // IEEE 802/Source-Rt Bridge, ARCNET |
| 89 | 296, // Point-to-Point (low delay) |
| 90 | 68, // Official minimum |
| 91 | 0, // End of list marker |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static const int IP_HEADER_SIZE = 20u; |
| 95 | static const int IPV6_HEADER_SIZE = 40u; |
| 96 | static const int ICMP_HEADER_SIZE = 8u; |
| 97 | static const int ICMP_PING_TIMEOUT_MILLIS = 10000u; |
| 98 | #endif |
| 99 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 100 | PhysicalSocket::PhysicalSocket(PhysicalSocketServer* ss, SOCKET s) |
| 101 | : ss_(ss), s_(s), enabled_events_(0), error_(0), |
| 102 | state_((s == INVALID_SOCKET) ? CS_CLOSED : CS_CONNECTED), |
| 103 | resolver_(nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | #if defined(WEBRTC_WIN) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 105 | // EnsureWinsockInit() ensures that winsock is initialized. The default |
| 106 | // version of this function doesn't do anything because winsock is |
| 107 | // initialized by constructor of a static object. If neccessary libjingle |
| 108 | // users can link it with a different version of this function by replacing |
| 109 | // win32socketinit.cc. See win32socketinit.cc for more details. |
| 110 | EnsureWinsockInit(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 111 | #endif |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 112 | if (s_ != INVALID_SOCKET) { |
| 113 | enabled_events_ = DE_READ | DE_WRITE; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 114 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 115 | int type = SOCK_STREAM; |
| 116 | socklen_t len = sizeof(type); |
| 117 | VERIFY(0 == getsockopt(s_, SOL_SOCKET, SO_TYPE, (SockOptArg)&type, &len)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 118 | udp_ = (SOCK_DGRAM == type); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 119 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 120 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 121 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 122 | PhysicalSocket::~PhysicalSocket() { |
| 123 | Close(); |
| 124 | } |
| 125 | |
| 126 | bool PhysicalSocket::Create(int family, int type) { |
| 127 | Close(); |
| 128 | s_ = ::socket(family, type, 0); |
| 129 | udp_ = (SOCK_DGRAM == type); |
| 130 | UpdateLastError(); |
| 131 | if (udp_) |
| 132 | enabled_events_ = DE_READ | DE_WRITE; |
| 133 | return s_ != INVALID_SOCKET; |
| 134 | } |
| 135 | |
| 136 | SocketAddress PhysicalSocket::GetLocalAddress() const { |
| 137 | sockaddr_storage addr_storage = {0}; |
| 138 | socklen_t addrlen = sizeof(addr_storage); |
| 139 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 140 | int result = ::getsockname(s_, addr, &addrlen); |
| 141 | SocketAddress address; |
| 142 | if (result >= 0) { |
| 143 | SocketAddressFromSockAddrStorage(addr_storage, &address); |
| 144 | } else { |
| 145 | LOG(LS_WARNING) << "GetLocalAddress: unable to get local addr, socket=" |
| 146 | << s_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 147 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 148 | return address; |
| 149 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 150 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 151 | SocketAddress PhysicalSocket::GetRemoteAddress() const { |
| 152 | sockaddr_storage addr_storage = {0}; |
| 153 | socklen_t addrlen = sizeof(addr_storage); |
| 154 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 155 | int result = ::getpeername(s_, addr, &addrlen); |
| 156 | SocketAddress address; |
| 157 | if (result >= 0) { |
| 158 | SocketAddressFromSockAddrStorage(addr_storage, &address); |
| 159 | } else { |
| 160 | LOG(LS_WARNING) << "GetRemoteAddress: unable to get remote addr, socket=" |
| 161 | << s_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 162 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 163 | return address; |
| 164 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 165 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 166 | int PhysicalSocket::Bind(const SocketAddress& bind_addr) { |
| 167 | sockaddr_storage addr_storage; |
| 168 | size_t len = bind_addr.ToSockAddrStorage(&addr_storage); |
| 169 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 170 | int err = ::bind(s_, addr, static_cast<int>(len)); |
| 171 | UpdateLastError(); |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 172 | #if !defined(NDEBUG) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 173 | if (0 == err) { |
| 174 | dbg_addr_ = "Bound @ "; |
| 175 | dbg_addr_.append(GetLocalAddress().ToString()); |
| 176 | } |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 177 | #endif |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 178 | if (ss_->network_binder()) { |
| 179 | int result = |
| 180 | ss_->network_binder()->BindSocketToNetwork(s_, bind_addr.ipaddr()); |
| 181 | if (result < 0) { |
| 182 | LOG(LS_INFO) << "Binding socket to network address " |
| 183 | << bind_addr.ipaddr().ToString() << " result " << result; |
| 184 | } |
| 185 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 186 | return err; |
| 187 | } |
| 188 | |
| 189 | int PhysicalSocket::Connect(const SocketAddress& addr) { |
| 190 | // TODO(pthatcher): Implicit creation is required to reconnect... |
| 191 | // ...but should we make it more explicit? |
| 192 | if (state_ != CS_CLOSED) { |
| 193 | SetError(EALREADY); |
| 194 | return SOCKET_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 195 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 196 | if (addr.IsUnresolvedIP()) { |
| 197 | LOG(LS_VERBOSE) << "Resolving addr in PhysicalSocket::Connect"; |
| 198 | resolver_ = new AsyncResolver(); |
| 199 | resolver_->SignalDone.connect(this, &PhysicalSocket::OnResolveResult); |
| 200 | resolver_->Start(addr); |
| 201 | state_ = CS_CONNECTING; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 205 | return DoConnect(addr); |
| 206 | } |
| 207 | |
| 208 | int PhysicalSocket::DoConnect(const SocketAddress& connect_addr) { |
| 209 | if ((s_ == INVALID_SOCKET) && |
| 210 | !Create(connect_addr.family(), SOCK_STREAM)) { |
| 211 | return SOCKET_ERROR; |
| 212 | } |
| 213 | sockaddr_storage addr_storage; |
| 214 | size_t len = connect_addr.ToSockAddrStorage(&addr_storage); |
| 215 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 216 | int err = ::connect(s_, addr, static_cast<int>(len)); |
| 217 | UpdateLastError(); |
| 218 | if (err == 0) { |
| 219 | state_ = CS_CONNECTED; |
| 220 | } else if (IsBlockingError(GetError())) { |
| 221 | state_ = CS_CONNECTING; |
| 222 | enabled_events_ |= DE_CONNECT; |
| 223 | } else { |
| 224 | return SOCKET_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 225 | } |
| 226 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 227 | enabled_events_ |= DE_READ | DE_WRITE; |
| 228 | return 0; |
| 229 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 230 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 231 | int PhysicalSocket::GetError() const { |
| 232 | CritScope cs(&crit_); |
| 233 | return error_; |
| 234 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 235 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 236 | void PhysicalSocket::SetError(int error) { |
| 237 | CritScope cs(&crit_); |
| 238 | error_ = error; |
| 239 | } |
| 240 | |
| 241 | AsyncSocket::ConnState PhysicalSocket::GetState() const { |
| 242 | return state_; |
| 243 | } |
| 244 | |
| 245 | int PhysicalSocket::GetOption(Option opt, int* value) { |
| 246 | int slevel; |
| 247 | int sopt; |
| 248 | if (TranslateOption(opt, &slevel, &sopt) == -1) |
| 249 | return -1; |
| 250 | socklen_t optlen = sizeof(*value); |
| 251 | int ret = ::getsockopt(s_, slevel, sopt, (SockOptArg)value, &optlen); |
| 252 | if (ret != -1 && opt == OPT_DONTFRAGMENT) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 253 | #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 254 | *value = (*value != IP_PMTUDISC_DONT) ? 1 : 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 255 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 256 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 257 | return ret; |
| 258 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 259 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 260 | int PhysicalSocket::SetOption(Option opt, int value) { |
| 261 | int slevel; |
| 262 | int sopt; |
| 263 | if (TranslateOption(opt, &slevel, &sopt) == -1) |
| 264 | return -1; |
| 265 | if (opt == OPT_DONTFRAGMENT) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 266 | #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 267 | value = (value) ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 268 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 269 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 270 | return ::setsockopt(s_, slevel, sopt, (SockOptArg)&value, sizeof(value)); |
| 271 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 272 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 273 | int PhysicalSocket::Send(const void* pv, size_t cb) { |
| 274 | int sent = ::send(s_, reinterpret_cast<const char *>(pv), (int)cb, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 275 | #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 276 | // Suppress SIGPIPE. Without this, attempting to send on a socket whose |
| 277 | // other end is closed will result in a SIGPIPE signal being raised to |
| 278 | // our process, which by default will terminate the process, which we |
| 279 | // don't want. By specifying this flag, we'll just get the error EPIPE |
| 280 | // instead and can handle the error gracefully. |
| 281 | MSG_NOSIGNAL |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 282 | #else |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 283 | 0 |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 284 | #endif |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 285 | ); |
| 286 | UpdateLastError(); |
| 287 | MaybeRemapSendError(); |
| 288 | // We have seen minidumps where this may be false. |
| 289 | ASSERT(sent <= static_cast<int>(cb)); |
| 290 | if ((sent < 0) && IsBlockingError(GetError())) { |
| 291 | enabled_events_ |= DE_WRITE; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 292 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 293 | return sent; |
| 294 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 295 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 296 | int PhysicalSocket::SendTo(const void* buffer, |
| 297 | size_t length, |
| 298 | const SocketAddress& addr) { |
| 299 | sockaddr_storage saddr; |
| 300 | size_t len = addr.ToSockAddrStorage(&saddr); |
| 301 | int sent = ::sendto( |
| 302 | s_, static_cast<const char *>(buffer), static_cast<int>(length), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 303 | #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 304 | // Suppress SIGPIPE. See above for explanation. |
| 305 | MSG_NOSIGNAL, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 306 | #else |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 307 | 0, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 308 | #endif |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 309 | reinterpret_cast<sockaddr*>(&saddr), static_cast<int>(len)); |
| 310 | UpdateLastError(); |
| 311 | MaybeRemapSendError(); |
| 312 | // We have seen minidumps where this may be false. |
| 313 | ASSERT(sent <= static_cast<int>(length)); |
| 314 | if ((sent < 0) && IsBlockingError(GetError())) { |
| 315 | enabled_events_ |= DE_WRITE; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 316 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 317 | return sent; |
| 318 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 319 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 320 | int PhysicalSocket::Recv(void* buffer, size_t length) { |
| 321 | int received = ::recv(s_, static_cast<char*>(buffer), |
| 322 | static_cast<int>(length), 0); |
| 323 | if ((received == 0) && (length != 0)) { |
| 324 | // Note: on graceful shutdown, recv can return 0. In this case, we |
| 325 | // pretend it is blocking, and then signal close, so that simplifying |
| 326 | // assumptions can be made about Recv. |
| 327 | LOG(LS_WARNING) << "EOF from socket; deferring close event"; |
| 328 | // Must turn this back on so that the select() loop will notice the close |
| 329 | // event. |
| 330 | enabled_events_ |= DE_READ; |
| 331 | SetError(EWOULDBLOCK); |
| 332 | return SOCKET_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 333 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 334 | UpdateLastError(); |
| 335 | int error = GetError(); |
| 336 | bool success = (received >= 0) || IsBlockingError(error); |
| 337 | if (udp_ || success) { |
| 338 | enabled_events_ |= DE_READ; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 339 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 340 | if (!success) { |
| 341 | LOG_F(LS_VERBOSE) << "Error = " << error; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 342 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 343 | return received; |
| 344 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 345 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 346 | int PhysicalSocket::RecvFrom(void* buffer, |
| 347 | size_t length, |
| 348 | SocketAddress* out_addr) { |
| 349 | sockaddr_storage addr_storage; |
| 350 | socklen_t addr_len = sizeof(addr_storage); |
| 351 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 352 | int received = ::recvfrom(s_, static_cast<char*>(buffer), |
| 353 | static_cast<int>(length), 0, addr, &addr_len); |
| 354 | UpdateLastError(); |
| 355 | if ((received >= 0) && (out_addr != nullptr)) |
| 356 | SocketAddressFromSockAddrStorage(addr_storage, out_addr); |
| 357 | int error = GetError(); |
| 358 | bool success = (received >= 0) || IsBlockingError(error); |
| 359 | if (udp_ || success) { |
| 360 | enabled_events_ |= DE_READ; |
| 361 | } |
| 362 | if (!success) { |
| 363 | LOG_F(LS_VERBOSE) << "Error = " << error; |
| 364 | } |
| 365 | return received; |
| 366 | } |
| 367 | |
| 368 | int PhysicalSocket::Listen(int backlog) { |
| 369 | int err = ::listen(s_, backlog); |
| 370 | UpdateLastError(); |
| 371 | if (err == 0) { |
| 372 | state_ = CS_CONNECTING; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 373 | enabled_events_ |= DE_ACCEPT; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 374 | #if !defined(NDEBUG) |
| 375 | dbg_addr_ = "Listening @ "; |
| 376 | dbg_addr_.append(GetLocalAddress().ToString()); |
| 377 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 378 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 379 | return err; |
| 380 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 381 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 382 | AsyncSocket* PhysicalSocket::Accept(SocketAddress* out_addr) { |
| 383 | // Always re-subscribe DE_ACCEPT to make sure new incoming connections will |
| 384 | // trigger an event even if DoAccept returns an error here. |
| 385 | enabled_events_ |= DE_ACCEPT; |
| 386 | sockaddr_storage addr_storage; |
| 387 | socklen_t addr_len = sizeof(addr_storage); |
| 388 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 389 | SOCKET s = DoAccept(s_, addr, &addr_len); |
| 390 | UpdateLastError(); |
| 391 | if (s == INVALID_SOCKET) |
| 392 | return nullptr; |
| 393 | if (out_addr != nullptr) |
| 394 | SocketAddressFromSockAddrStorage(addr_storage, out_addr); |
| 395 | return ss_->WrapSocket(s); |
| 396 | } |
| 397 | |
| 398 | int PhysicalSocket::Close() { |
| 399 | if (s_ == INVALID_SOCKET) |
| 400 | return 0; |
| 401 | int err = ::closesocket(s_); |
| 402 | UpdateLastError(); |
| 403 | s_ = INVALID_SOCKET; |
| 404 | state_ = CS_CLOSED; |
| 405 | enabled_events_ = 0; |
| 406 | if (resolver_) { |
| 407 | resolver_->Destroy(false); |
| 408 | resolver_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 409 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 410 | return err; |
| 411 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 412 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 413 | int PhysicalSocket::EstimateMTU(uint16_t* mtu) { |
| 414 | SocketAddress addr = GetRemoteAddress(); |
| 415 | if (addr.IsAnyIP()) { |
| 416 | SetError(ENOTCONN); |
| 417 | return -1; |
| 418 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 419 | |
| 420 | #if defined(WEBRTC_WIN) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 421 | // Gets the interface MTU (TTL=1) for the interface used to reach |addr|. |
| 422 | WinPing ping; |
| 423 | if (!ping.IsValid()) { |
| 424 | SetError(EINVAL); // can't think of a better error ID |
| 425 | return -1; |
| 426 | } |
| 427 | int header_size = ICMP_HEADER_SIZE; |
| 428 | if (addr.family() == AF_INET6) { |
| 429 | header_size += IPV6_HEADER_SIZE; |
| 430 | } else if (addr.family() == AF_INET) { |
| 431 | header_size += IP_HEADER_SIZE; |
| 432 | } |
| 433 | |
| 434 | for (int level = 0; PACKET_MAXIMUMS[level + 1] > 0; ++level) { |
| 435 | int32_t size = PACKET_MAXIMUMS[level] - header_size; |
| 436 | WinPing::PingResult result = ping.Ping(addr.ipaddr(), size, |
| 437 | ICMP_PING_TIMEOUT_MILLIS, |
| 438 | 1, false); |
| 439 | if (result == WinPing::PING_FAIL) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 440 | SetError(EINVAL); // can't think of a better error ID |
| 441 | return -1; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 442 | } else if (result != WinPing::PING_TOO_LARGE) { |
| 443 | *mtu = PACKET_MAXIMUMS[level]; |
| 444 | return 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 445 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 446 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 447 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 448 | ASSERT(false); |
| 449 | return -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 450 | #elif defined(WEBRTC_MAC) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 451 | // No simple way to do this on Mac OS X. |
| 452 | // SIOCGIFMTU would work if we knew which interface would be used, but |
| 453 | // figuring that out is pretty complicated. For now we'll return an error |
| 454 | // and let the caller pick a default MTU. |
| 455 | SetError(EINVAL); |
| 456 | return -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 457 | #elif defined(WEBRTC_LINUX) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 458 | // Gets the path MTU. |
| 459 | int value; |
| 460 | socklen_t vlen = sizeof(value); |
| 461 | int err = getsockopt(s_, IPPROTO_IP, IP_MTU, &value, &vlen); |
| 462 | if (err < 0) { |
| 463 | UpdateLastError(); |
| 464 | return err; |
| 465 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 466 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 467 | ASSERT((0 <= value) && (value <= 65536)); |
| 468 | *mtu = value; |
| 469 | return 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 470 | #elif defined(__native_client__) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 471 | // Most socket operations, including this, will fail in NaCl's sandbox. |
| 472 | error_ = EACCES; |
| 473 | return -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 474 | #endif |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | |
| 478 | SOCKET PhysicalSocket::DoAccept(SOCKET socket, |
| 479 | sockaddr* addr, |
| 480 | socklen_t* addrlen) { |
| 481 | return ::accept(socket, addr, addrlen); |
| 482 | } |
| 483 | |
| 484 | void PhysicalSocket::OnResolveResult(AsyncResolverInterface* resolver) { |
| 485 | if (resolver != resolver_) { |
| 486 | return; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 487 | } |
| 488 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 489 | int error = resolver_->GetError(); |
| 490 | if (error == 0) { |
| 491 | error = DoConnect(resolver_->address()); |
| 492 | } else { |
| 493 | Close(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 494 | } |
| 495 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 496 | if (error) { |
| 497 | SetError(error); |
| 498 | SignalCloseEvent(this, error); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 499 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 500 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 501 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 502 | void PhysicalSocket::UpdateLastError() { |
| 503 | SetError(LAST_SYSTEM_ERROR); |
| 504 | } |
| 505 | |
| 506 | void PhysicalSocket::MaybeRemapSendError() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 507 | #if defined(WEBRTC_MAC) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 508 | // https://developer.apple.com/library/mac/documentation/Darwin/ |
| 509 | // Reference/ManPages/man2/sendto.2.html |
| 510 | // ENOBUFS - The output queue for a network interface is full. |
| 511 | // This generally indicates that the interface has stopped sending, |
| 512 | // but may be caused by transient congestion. |
| 513 | if (GetError() == ENOBUFS) { |
| 514 | SetError(EWOULDBLOCK); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 515 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 516 | #endif |
| 517 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 518 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 519 | int PhysicalSocket::TranslateOption(Option opt, int* slevel, int* sopt) { |
| 520 | switch (opt) { |
| 521 | case OPT_DONTFRAGMENT: |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 522 | #if defined(WEBRTC_WIN) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 523 | *slevel = IPPROTO_IP; |
| 524 | *sopt = IP_DONTFRAGMENT; |
| 525 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 526 | #elif defined(WEBRTC_MAC) || defined(BSD) || defined(__native_client__) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 527 | LOG(LS_WARNING) << "Socket::OPT_DONTFRAGMENT not supported."; |
| 528 | return -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 529 | #elif defined(WEBRTC_POSIX) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 530 | *slevel = IPPROTO_IP; |
| 531 | *sopt = IP_MTU_DISCOVER; |
| 532 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 533 | #endif |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 534 | case OPT_RCVBUF: |
| 535 | *slevel = SOL_SOCKET; |
| 536 | *sopt = SO_RCVBUF; |
| 537 | break; |
| 538 | case OPT_SNDBUF: |
| 539 | *slevel = SOL_SOCKET; |
| 540 | *sopt = SO_SNDBUF; |
| 541 | break; |
| 542 | case OPT_NODELAY: |
| 543 | *slevel = IPPROTO_TCP; |
| 544 | *sopt = TCP_NODELAY; |
| 545 | break; |
| 546 | case OPT_DSCP: |
| 547 | LOG(LS_WARNING) << "Socket::OPT_DSCP not supported."; |
| 548 | return -1; |
| 549 | case OPT_RTP_SENDTIME_EXTN_ID: |
| 550 | return -1; // No logging is necessary as this not a OS socket option. |
| 551 | default: |
| 552 | ASSERT(false); |
| 553 | return -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 554 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 555 | return 0; |
| 556 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 557 | |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 558 | SocketDispatcher::SocketDispatcher(PhysicalSocketServer *ss) |
| 559 | #if defined(WEBRTC_WIN) |
| 560 | : PhysicalSocket(ss), id_(0), signal_close_(false) |
| 561 | #else |
| 562 | : PhysicalSocket(ss) |
| 563 | #endif |
| 564 | { |
| 565 | } |
| 566 | |
| 567 | SocketDispatcher::SocketDispatcher(SOCKET s, PhysicalSocketServer *ss) |
| 568 | #if defined(WEBRTC_WIN) |
| 569 | : PhysicalSocket(ss, s), id_(0), signal_close_(false) |
| 570 | #else |
| 571 | : PhysicalSocket(ss, s) |
| 572 | #endif |
| 573 | { |
| 574 | } |
| 575 | |
| 576 | SocketDispatcher::~SocketDispatcher() { |
| 577 | Close(); |
| 578 | } |
| 579 | |
| 580 | bool SocketDispatcher::Initialize() { |
| 581 | ASSERT(s_ != INVALID_SOCKET); |
| 582 | // Must be a non-blocking |
| 583 | #if defined(WEBRTC_WIN) |
| 584 | u_long argp = 1; |
| 585 | ioctlsocket(s_, FIONBIO, &argp); |
| 586 | #elif defined(WEBRTC_POSIX) |
| 587 | fcntl(s_, F_SETFL, fcntl(s_, F_GETFL, 0) | O_NONBLOCK); |
| 588 | #endif |
| 589 | ss_->Add(this); |
| 590 | return true; |
| 591 | } |
| 592 | |
| 593 | bool SocketDispatcher::Create(int type) { |
| 594 | return Create(AF_INET, type); |
| 595 | } |
| 596 | |
| 597 | bool SocketDispatcher::Create(int family, int type) { |
| 598 | // Change the socket to be non-blocking. |
| 599 | if (!PhysicalSocket::Create(family, type)) |
| 600 | return false; |
| 601 | |
| 602 | if (!Initialize()) |
| 603 | return false; |
| 604 | |
| 605 | #if defined(WEBRTC_WIN) |
| 606 | do { id_ = ++next_id_; } while (id_ == 0); |
| 607 | #endif |
| 608 | return true; |
| 609 | } |
| 610 | |
| 611 | #if defined(WEBRTC_WIN) |
| 612 | |
| 613 | WSAEVENT SocketDispatcher::GetWSAEvent() { |
| 614 | return WSA_INVALID_EVENT; |
| 615 | } |
| 616 | |
| 617 | SOCKET SocketDispatcher::GetSocket() { |
| 618 | return s_; |
| 619 | } |
| 620 | |
| 621 | bool SocketDispatcher::CheckSignalClose() { |
| 622 | if (!signal_close_) |
| 623 | return false; |
| 624 | |
| 625 | char ch; |
| 626 | if (recv(s_, &ch, 1, MSG_PEEK) > 0) |
| 627 | return false; |
| 628 | |
| 629 | state_ = CS_CLOSED; |
| 630 | signal_close_ = false; |
| 631 | SignalCloseEvent(this, signal_err_); |
| 632 | return true; |
| 633 | } |
| 634 | |
| 635 | int SocketDispatcher::next_id_ = 0; |
| 636 | |
| 637 | #elif defined(WEBRTC_POSIX) |
| 638 | |
| 639 | int SocketDispatcher::GetDescriptor() { |
| 640 | return s_; |
| 641 | } |
| 642 | |
| 643 | bool SocketDispatcher::IsDescriptorClosed() { |
| 644 | // We don't have a reliable way of distinguishing end-of-stream |
| 645 | // from readability. So test on each readable call. Is this |
| 646 | // inefficient? Probably. |
| 647 | char ch; |
| 648 | ssize_t res = ::recv(s_, &ch, 1, MSG_PEEK); |
| 649 | if (res > 0) { |
| 650 | // Data available, so not closed. |
| 651 | return false; |
| 652 | } else if (res == 0) { |
| 653 | // EOF, so closed. |
| 654 | return true; |
| 655 | } else { // error |
| 656 | switch (errno) { |
| 657 | // Returned if we've already closed s_. |
| 658 | case EBADF: |
| 659 | // Returned during ungraceful peer shutdown. |
| 660 | case ECONNRESET: |
| 661 | return true; |
| 662 | default: |
| 663 | // Assume that all other errors are just blocking errors, meaning the |
| 664 | // connection is still good but we just can't read from it right now. |
| 665 | // This should only happen when connecting (and at most once), because |
| 666 | // in all other cases this function is only called if the file |
| 667 | // descriptor is already known to be in the readable state. However, |
| 668 | // it's not necessary a problem if we spuriously interpret a |
| 669 | // "connection lost"-type error as a blocking error, because typically |
| 670 | // the next recv() will get EOF, so we'll still eventually notice that |
| 671 | // the socket is closed. |
| 672 | LOG_ERR(LS_WARNING) << "Assuming benign blocking error"; |
| 673 | return false; |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | #endif // WEBRTC_POSIX |
| 679 | |
| 680 | uint32_t SocketDispatcher::GetRequestedEvents() { |
| 681 | return enabled_events_; |
| 682 | } |
| 683 | |
| 684 | void SocketDispatcher::OnPreEvent(uint32_t ff) { |
| 685 | if ((ff & DE_CONNECT) != 0) |
| 686 | state_ = CS_CONNECTED; |
| 687 | |
| 688 | #if defined(WEBRTC_WIN) |
| 689 | // We set CS_CLOSED from CheckSignalClose. |
| 690 | #elif defined(WEBRTC_POSIX) |
| 691 | if ((ff & DE_CLOSE) != 0) |
| 692 | state_ = CS_CLOSED; |
| 693 | #endif |
| 694 | } |
| 695 | |
| 696 | #if defined(WEBRTC_WIN) |
| 697 | |
| 698 | void SocketDispatcher::OnEvent(uint32_t ff, int err) { |
| 699 | int cache_id = id_; |
| 700 | // Make sure we deliver connect/accept first. Otherwise, consumers may see |
| 701 | // something like a READ followed by a CONNECT, which would be odd. |
| 702 | if (((ff & DE_CONNECT) != 0) && (id_ == cache_id)) { |
| 703 | if (ff != DE_CONNECT) |
| 704 | LOG(LS_VERBOSE) << "Signalled with DE_CONNECT: " << ff; |
| 705 | enabled_events_ &= ~DE_CONNECT; |
| 706 | #if !defined(NDEBUG) |
| 707 | dbg_addr_ = "Connected @ "; |
| 708 | dbg_addr_.append(GetRemoteAddress().ToString()); |
| 709 | #endif |
| 710 | SignalConnectEvent(this); |
| 711 | } |
| 712 | if (((ff & DE_ACCEPT) != 0) && (id_ == cache_id)) { |
| 713 | enabled_events_ &= ~DE_ACCEPT; |
| 714 | SignalReadEvent(this); |
| 715 | } |
| 716 | if ((ff & DE_READ) != 0) { |
| 717 | enabled_events_ &= ~DE_READ; |
| 718 | SignalReadEvent(this); |
| 719 | } |
| 720 | if (((ff & DE_WRITE) != 0) && (id_ == cache_id)) { |
| 721 | enabled_events_ &= ~DE_WRITE; |
| 722 | SignalWriteEvent(this); |
| 723 | } |
| 724 | if (((ff & DE_CLOSE) != 0) && (id_ == cache_id)) { |
| 725 | signal_close_ = true; |
| 726 | signal_err_ = err; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | #elif defined(WEBRTC_POSIX) |
| 731 | |
| 732 | void SocketDispatcher::OnEvent(uint32_t ff, int err) { |
| 733 | // Make sure we deliver connect/accept first. Otherwise, consumers may see |
| 734 | // something like a READ followed by a CONNECT, which would be odd. |
| 735 | if ((ff & DE_CONNECT) != 0) { |
| 736 | enabled_events_ &= ~DE_CONNECT; |
| 737 | SignalConnectEvent(this); |
| 738 | } |
| 739 | if ((ff & DE_ACCEPT) != 0) { |
| 740 | enabled_events_ &= ~DE_ACCEPT; |
| 741 | SignalReadEvent(this); |
| 742 | } |
| 743 | if ((ff & DE_READ) != 0) { |
| 744 | enabled_events_ &= ~DE_READ; |
| 745 | SignalReadEvent(this); |
| 746 | } |
| 747 | if ((ff & DE_WRITE) != 0) { |
| 748 | enabled_events_ &= ~DE_WRITE; |
| 749 | SignalWriteEvent(this); |
| 750 | } |
| 751 | if ((ff & DE_CLOSE) != 0) { |
| 752 | // The socket is now dead to us, so stop checking it. |
| 753 | enabled_events_ = 0; |
| 754 | SignalCloseEvent(this, err); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | #endif // WEBRTC_POSIX |
| 759 | |
| 760 | int SocketDispatcher::Close() { |
| 761 | if (s_ == INVALID_SOCKET) |
| 762 | return 0; |
| 763 | |
| 764 | #if defined(WEBRTC_WIN) |
| 765 | id_ = 0; |
| 766 | signal_close_ = false; |
| 767 | #endif |
| 768 | ss_->Remove(this); |
| 769 | return PhysicalSocket::Close(); |
| 770 | } |
| 771 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 772 | #if defined(WEBRTC_POSIX) |
| 773 | class EventDispatcher : public Dispatcher { |
| 774 | public: |
| 775 | EventDispatcher(PhysicalSocketServer* ss) : ss_(ss), fSignaled_(false) { |
| 776 | if (pipe(afd_) < 0) |
| 777 | LOG(LERROR) << "pipe failed"; |
| 778 | ss_->Add(this); |
| 779 | } |
| 780 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 781 | ~EventDispatcher() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 782 | ss_->Remove(this); |
| 783 | close(afd_[0]); |
| 784 | close(afd_[1]); |
| 785 | } |
| 786 | |
| 787 | virtual void Signal() { |
| 788 | CritScope cs(&crit_); |
| 789 | if (!fSignaled_) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 790 | const uint8_t b[1] = {0}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 791 | if (VERIFY(1 == write(afd_[1], b, sizeof(b)))) { |
| 792 | fSignaled_ = true; |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 797 | uint32_t GetRequestedEvents() override { return DE_READ; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 798 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 799 | void OnPreEvent(uint32_t ff) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 800 | // It is not possible to perfectly emulate an auto-resetting event with |
| 801 | // pipes. This simulates it by resetting before the event is handled. |
| 802 | |
| 803 | CritScope cs(&crit_); |
| 804 | if (fSignaled_) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 805 | uint8_t b[4]; // Allow for reading more than 1 byte, but expect 1. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 806 | VERIFY(1 == read(afd_[0], b, sizeof(b))); |
| 807 | fSignaled_ = false; |
| 808 | } |
| 809 | } |
| 810 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 811 | void OnEvent(uint32_t ff, int err) override { ASSERT(false); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 812 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 813 | int GetDescriptor() override { return afd_[0]; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 814 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 815 | bool IsDescriptorClosed() override { return false; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 816 | |
| 817 | private: |
| 818 | PhysicalSocketServer *ss_; |
| 819 | int afd_[2]; |
| 820 | bool fSignaled_; |
| 821 | CriticalSection crit_; |
| 822 | }; |
| 823 | |
| 824 | // These two classes use the self-pipe trick to deliver POSIX signals to our |
| 825 | // select loop. This is the only safe, reliable, cross-platform way to do |
| 826 | // non-trivial things with a POSIX signal in an event-driven program (until |
| 827 | // proper pselect() implementations become ubiquitous). |
| 828 | |
| 829 | class PosixSignalHandler { |
| 830 | public: |
| 831 | // POSIX only specifies 32 signals, but in principle the system might have |
| 832 | // more and the programmer might choose to use them, so we size our array |
| 833 | // for 128. |
| 834 | static const int kNumPosixSignals = 128; |
| 835 | |
| 836 | // There is just a single global instance. (Signal handlers do not get any |
| 837 | // sort of user-defined void * parameter, so they can't access anything that |
| 838 | // isn't global.) |
| 839 | static PosixSignalHandler* Instance() { |
Andrew MacDonald | 469c2c0 | 2015-05-22 17:50:26 -0700 | [diff] [blame] | 840 | RTC_DEFINE_STATIC_LOCAL(PosixSignalHandler, instance, ()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 841 | return &instance; |
| 842 | } |
| 843 | |
| 844 | // Returns true if the given signal number is set. |
| 845 | bool IsSignalSet(int signum) const { |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 846 | ASSERT(signum < static_cast<int>(arraysize(received_signal_))); |
| 847 | if (signum < static_cast<int>(arraysize(received_signal_))) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 848 | return received_signal_[signum]; |
| 849 | } else { |
| 850 | return false; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | // Clears the given signal number. |
| 855 | void ClearSignal(int signum) { |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 856 | ASSERT(signum < static_cast<int>(arraysize(received_signal_))); |
| 857 | if (signum < static_cast<int>(arraysize(received_signal_))) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 858 | received_signal_[signum] = false; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | // Returns the file descriptor to monitor for signal events. |
| 863 | int GetDescriptor() const { |
| 864 | return afd_[0]; |
| 865 | } |
| 866 | |
| 867 | // This is called directly from our real signal handler, so it must be |
| 868 | // signal-handler-safe. That means it cannot assume anything about the |
| 869 | // user-level state of the process, since the handler could be executed at any |
| 870 | // time on any thread. |
| 871 | void OnPosixSignalReceived(int signum) { |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 872 | if (signum >= static_cast<int>(arraysize(received_signal_))) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 873 | // We don't have space in our array for this. |
| 874 | return; |
| 875 | } |
| 876 | // Set a flag saying we've seen this signal. |
| 877 | received_signal_[signum] = true; |
| 878 | // Notify application code that we got a signal. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 879 | const uint8_t b[1] = {0}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 880 | if (-1 == write(afd_[1], b, sizeof(b))) { |
| 881 | // Nothing we can do here. If there's an error somehow then there's |
| 882 | // nothing we can safely do from a signal handler. |
| 883 | // No, we can't even safely log it. |
| 884 | // But, we still have to check the return value here. Otherwise, |
| 885 | // GCC 4.4.1 complains ignoring return value. Even (void) doesn't help. |
| 886 | return; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | private: |
| 891 | PosixSignalHandler() { |
| 892 | if (pipe(afd_) < 0) { |
| 893 | LOG_ERR(LS_ERROR) << "pipe failed"; |
| 894 | return; |
| 895 | } |
| 896 | if (fcntl(afd_[0], F_SETFL, O_NONBLOCK) < 0) { |
| 897 | LOG_ERR(LS_WARNING) << "fcntl #1 failed"; |
| 898 | } |
| 899 | if (fcntl(afd_[1], F_SETFL, O_NONBLOCK) < 0) { |
| 900 | LOG_ERR(LS_WARNING) << "fcntl #2 failed"; |
| 901 | } |
| 902 | memset(const_cast<void *>(static_cast<volatile void *>(received_signal_)), |
| 903 | 0, |
| 904 | sizeof(received_signal_)); |
| 905 | } |
| 906 | |
| 907 | ~PosixSignalHandler() { |
| 908 | int fd1 = afd_[0]; |
| 909 | int fd2 = afd_[1]; |
| 910 | // We clobber the stored file descriptor numbers here or else in principle |
| 911 | // a signal that happens to be delivered during application termination |
| 912 | // could erroneously write a zero byte to an unrelated file handle in |
| 913 | // OnPosixSignalReceived() if some other file happens to be opened later |
| 914 | // during shutdown and happens to be given the same file descriptor number |
| 915 | // as our pipe had. Unfortunately even with this precaution there is still a |
| 916 | // race where that could occur if said signal happens to be handled |
| 917 | // concurrently with this code and happens to have already read the value of |
| 918 | // afd_[1] from memory before we clobber it, but that's unlikely. |
| 919 | afd_[0] = -1; |
| 920 | afd_[1] = -1; |
| 921 | close(fd1); |
| 922 | close(fd2); |
| 923 | } |
| 924 | |
| 925 | int afd_[2]; |
| 926 | // These are boolean flags that will be set in our signal handler and read |
| 927 | // and cleared from Wait(). There is a race involved in this, but it is |
| 928 | // benign. The signal handler sets the flag before signaling the pipe, so |
| 929 | // we'll never end up blocking in select() while a flag is still true. |
| 930 | // However, if two of the same signal arrive close to each other then it's |
| 931 | // possible that the second time the handler may set the flag while it's still |
| 932 | // true, meaning that signal will be missed. But the first occurrence of it |
| 933 | // will still be handled, so this isn't a problem. |
| 934 | // Volatile is not necessary here for correctness, but this data _is_ volatile |
| 935 | // so I've marked it as such. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 936 | volatile uint8_t received_signal_[kNumPosixSignals]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 937 | }; |
| 938 | |
| 939 | class PosixSignalDispatcher : public Dispatcher { |
| 940 | public: |
| 941 | PosixSignalDispatcher(PhysicalSocketServer *owner) : owner_(owner) { |
| 942 | owner_->Add(this); |
| 943 | } |
| 944 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 945 | ~PosixSignalDispatcher() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 946 | owner_->Remove(this); |
| 947 | } |
| 948 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 949 | uint32_t GetRequestedEvents() override { return DE_READ; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 950 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 951 | void OnPreEvent(uint32_t ff) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 952 | // Events might get grouped if signals come very fast, so we read out up to |
| 953 | // 16 bytes to make sure we keep the pipe empty. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 954 | uint8_t b[16]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 955 | ssize_t ret = read(GetDescriptor(), b, sizeof(b)); |
| 956 | if (ret < 0) { |
| 957 | LOG_ERR(LS_WARNING) << "Error in read()"; |
| 958 | } else if (ret == 0) { |
| 959 | LOG(LS_WARNING) << "Should have read at least one byte"; |
| 960 | } |
| 961 | } |
| 962 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 963 | void OnEvent(uint32_t ff, int err) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 964 | for (int signum = 0; signum < PosixSignalHandler::kNumPosixSignals; |
| 965 | ++signum) { |
| 966 | if (PosixSignalHandler::Instance()->IsSignalSet(signum)) { |
| 967 | PosixSignalHandler::Instance()->ClearSignal(signum); |
| 968 | HandlerMap::iterator i = handlers_.find(signum); |
| 969 | if (i == handlers_.end()) { |
| 970 | // This can happen if a signal is delivered to our process at around |
| 971 | // the same time as we unset our handler for it. It is not an error |
| 972 | // condition, but it's unusual enough to be worth logging. |
| 973 | LOG(LS_INFO) << "Received signal with no handler: " << signum; |
| 974 | } else { |
| 975 | // Otherwise, execute our handler. |
| 976 | (*i->second)(signum); |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 982 | int GetDescriptor() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 983 | return PosixSignalHandler::Instance()->GetDescriptor(); |
| 984 | } |
| 985 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 986 | bool IsDescriptorClosed() override { return false; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 987 | |
| 988 | void SetHandler(int signum, void (*handler)(int)) { |
| 989 | handlers_[signum] = handler; |
| 990 | } |
| 991 | |
| 992 | void ClearHandler(int signum) { |
| 993 | handlers_.erase(signum); |
| 994 | } |
| 995 | |
| 996 | bool HasHandlers() { |
| 997 | return !handlers_.empty(); |
| 998 | } |
| 999 | |
| 1000 | private: |
| 1001 | typedef std::map<int, void (*)(int)> HandlerMap; |
| 1002 | |
| 1003 | HandlerMap handlers_; |
| 1004 | // Our owner. |
| 1005 | PhysicalSocketServer *owner_; |
| 1006 | }; |
| 1007 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1008 | class FileDispatcher: public Dispatcher, public AsyncFile { |
| 1009 | public: |
| 1010 | FileDispatcher(int fd, PhysicalSocketServer *ss) : ss_(ss), fd_(fd) { |
| 1011 | set_readable(true); |
| 1012 | |
| 1013 | ss_->Add(this); |
| 1014 | |
| 1015 | fcntl(fd_, F_SETFL, fcntl(fd_, F_GETFL, 0) | O_NONBLOCK); |
| 1016 | } |
| 1017 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 1018 | ~FileDispatcher() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1019 | ss_->Remove(this); |
| 1020 | } |
| 1021 | |
| 1022 | SocketServer* socketserver() { return ss_; } |
| 1023 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 1024 | int GetDescriptor() override { return fd_; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1025 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 1026 | bool IsDescriptorClosed() override { return false; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1027 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1028 | uint32_t GetRequestedEvents() override { return flags_; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1029 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1030 | void OnPreEvent(uint32_t ff) override {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1031 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1032 | void OnEvent(uint32_t ff, int err) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1033 | if ((ff & DE_READ) != 0) |
| 1034 | SignalReadEvent(this); |
| 1035 | if ((ff & DE_WRITE) != 0) |
| 1036 | SignalWriteEvent(this); |
| 1037 | if ((ff & DE_CLOSE) != 0) |
| 1038 | SignalCloseEvent(this, err); |
| 1039 | } |
| 1040 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 1041 | bool readable() override { return (flags_ & DE_READ) != 0; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1042 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 1043 | void set_readable(bool value) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1044 | flags_ = value ? (flags_ | DE_READ) : (flags_ & ~DE_READ); |
| 1045 | } |
| 1046 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 1047 | bool writable() override { return (flags_ & DE_WRITE) != 0; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1048 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 1049 | void set_writable(bool value) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1050 | flags_ = value ? (flags_ | DE_WRITE) : (flags_ & ~DE_WRITE); |
| 1051 | } |
| 1052 | |
| 1053 | private: |
| 1054 | PhysicalSocketServer* ss_; |
| 1055 | int fd_; |
| 1056 | int flags_; |
| 1057 | }; |
| 1058 | |
| 1059 | AsyncFile* PhysicalSocketServer::CreateFile(int fd) { |
| 1060 | return new FileDispatcher(fd, this); |
| 1061 | } |
| 1062 | |
| 1063 | #endif // WEBRTC_POSIX |
| 1064 | |
| 1065 | #if defined(WEBRTC_WIN) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1066 | static uint32_t FlagsToEvents(uint32_t events) { |
| 1067 | uint32_t ffFD = FD_CLOSE; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1068 | if (events & DE_READ) |
| 1069 | ffFD |= FD_READ; |
| 1070 | if (events & DE_WRITE) |
| 1071 | ffFD |= FD_WRITE; |
| 1072 | if (events & DE_CONNECT) |
| 1073 | ffFD |= FD_CONNECT; |
| 1074 | if (events & DE_ACCEPT) |
| 1075 | ffFD |= FD_ACCEPT; |
| 1076 | return ffFD; |
| 1077 | } |
| 1078 | |
| 1079 | class EventDispatcher : public Dispatcher { |
| 1080 | public: |
| 1081 | EventDispatcher(PhysicalSocketServer *ss) : ss_(ss) { |
| 1082 | hev_ = WSACreateEvent(); |
| 1083 | if (hev_) { |
| 1084 | ss_->Add(this); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | ~EventDispatcher() { |
| 1089 | if (hev_ != NULL) { |
| 1090 | ss_->Remove(this); |
| 1091 | WSACloseEvent(hev_); |
| 1092 | hev_ = NULL; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | virtual void Signal() { |
| 1097 | if (hev_ != NULL) |
| 1098 | WSASetEvent(hev_); |
| 1099 | } |
| 1100 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1101 | virtual uint32_t GetRequestedEvents() { return 0; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1102 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1103 | virtual void OnPreEvent(uint32_t ff) { WSAResetEvent(hev_); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1104 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1105 | virtual void OnEvent(uint32_t ff, int err) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1106 | |
| 1107 | virtual WSAEVENT GetWSAEvent() { |
| 1108 | return hev_; |
| 1109 | } |
| 1110 | |
| 1111 | virtual SOCKET GetSocket() { |
| 1112 | return INVALID_SOCKET; |
| 1113 | } |
| 1114 | |
| 1115 | virtual bool CheckSignalClose() { return false; } |
| 1116 | |
| 1117 | private: |
| 1118 | PhysicalSocketServer* ss_; |
| 1119 | WSAEVENT hev_; |
| 1120 | }; |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 1121 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1122 | |
| 1123 | // Sets the value of a boolean value to false when signaled. |
| 1124 | class Signaler : public EventDispatcher { |
| 1125 | public: |
| 1126 | Signaler(PhysicalSocketServer* ss, bool* pf) |
| 1127 | : EventDispatcher(ss), pf_(pf) { |
| 1128 | } |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 1129 | ~Signaler() override { } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1130 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1131 | void OnEvent(uint32_t ff, int err) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1132 | if (pf_) |
| 1133 | *pf_ = false; |
| 1134 | } |
| 1135 | |
| 1136 | private: |
| 1137 | bool *pf_; |
| 1138 | }; |
| 1139 | |
| 1140 | PhysicalSocketServer::PhysicalSocketServer() |
| 1141 | : fWait_(false) { |
| 1142 | signal_wakeup_ = new Signaler(this, &fWait_); |
| 1143 | #if defined(WEBRTC_WIN) |
| 1144 | socket_ev_ = WSACreateEvent(); |
| 1145 | #endif |
| 1146 | } |
| 1147 | |
| 1148 | PhysicalSocketServer::~PhysicalSocketServer() { |
| 1149 | #if defined(WEBRTC_WIN) |
| 1150 | WSACloseEvent(socket_ev_); |
| 1151 | #endif |
| 1152 | #if defined(WEBRTC_POSIX) |
| 1153 | signal_dispatcher_.reset(); |
| 1154 | #endif |
| 1155 | delete signal_wakeup_; |
| 1156 | ASSERT(dispatchers_.empty()); |
| 1157 | } |
| 1158 | |
| 1159 | void PhysicalSocketServer::WakeUp() { |
| 1160 | signal_wakeup_->Signal(); |
| 1161 | } |
| 1162 | |
| 1163 | Socket* PhysicalSocketServer::CreateSocket(int type) { |
| 1164 | return CreateSocket(AF_INET, type); |
| 1165 | } |
| 1166 | |
| 1167 | Socket* PhysicalSocketServer::CreateSocket(int family, int type) { |
| 1168 | PhysicalSocket* socket = new PhysicalSocket(this); |
| 1169 | if (socket->Create(family, type)) { |
| 1170 | return socket; |
| 1171 | } else { |
| 1172 | delete socket; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 1173 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | AsyncSocket* PhysicalSocketServer::CreateAsyncSocket(int type) { |
| 1178 | return CreateAsyncSocket(AF_INET, type); |
| 1179 | } |
| 1180 | |
| 1181 | AsyncSocket* PhysicalSocketServer::CreateAsyncSocket(int family, int type) { |
| 1182 | SocketDispatcher* dispatcher = new SocketDispatcher(this); |
| 1183 | if (dispatcher->Create(family, type)) { |
| 1184 | return dispatcher; |
| 1185 | } else { |
| 1186 | delete dispatcher; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 1187 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | AsyncSocket* PhysicalSocketServer::WrapSocket(SOCKET s) { |
| 1192 | SocketDispatcher* dispatcher = new SocketDispatcher(s, this); |
| 1193 | if (dispatcher->Initialize()) { |
| 1194 | return dispatcher; |
| 1195 | } else { |
| 1196 | delete dispatcher; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 1197 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | void PhysicalSocketServer::Add(Dispatcher *pdispatcher) { |
| 1202 | CritScope cs(&crit_); |
| 1203 | // Prevent duplicates. This can cause dead dispatchers to stick around. |
| 1204 | DispatcherList::iterator pos = std::find(dispatchers_.begin(), |
| 1205 | dispatchers_.end(), |
| 1206 | pdispatcher); |
| 1207 | if (pos != dispatchers_.end()) |
| 1208 | return; |
| 1209 | dispatchers_.push_back(pdispatcher); |
| 1210 | } |
| 1211 | |
| 1212 | void PhysicalSocketServer::Remove(Dispatcher *pdispatcher) { |
| 1213 | CritScope cs(&crit_); |
| 1214 | DispatcherList::iterator pos = std::find(dispatchers_.begin(), |
| 1215 | dispatchers_.end(), |
| 1216 | pdispatcher); |
| 1217 | // We silently ignore duplicate calls to Add, so we should silently ignore |
| 1218 | // the (expected) symmetric calls to Remove. Note that this may still hide |
| 1219 | // a real issue, so we at least log a warning about it. |
| 1220 | if (pos == dispatchers_.end()) { |
| 1221 | LOG(LS_WARNING) << "PhysicalSocketServer asked to remove a unknown " |
| 1222 | << "dispatcher, potentially from a duplicate call to Add."; |
| 1223 | return; |
| 1224 | } |
| 1225 | size_t index = pos - dispatchers_.begin(); |
| 1226 | dispatchers_.erase(pos); |
| 1227 | for (IteratorList::iterator it = iterators_.begin(); it != iterators_.end(); |
| 1228 | ++it) { |
| 1229 | if (index < **it) { |
| 1230 | --**it; |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | #if defined(WEBRTC_POSIX) |
| 1236 | bool PhysicalSocketServer::Wait(int cmsWait, bool process_io) { |
| 1237 | // Calculate timing information |
| 1238 | |
| 1239 | struct timeval *ptvWait = NULL; |
| 1240 | struct timeval tvWait; |
| 1241 | struct timeval tvStop; |
| 1242 | if (cmsWait != kForever) { |
| 1243 | // Calculate wait timeval |
| 1244 | tvWait.tv_sec = cmsWait / 1000; |
| 1245 | tvWait.tv_usec = (cmsWait % 1000) * 1000; |
| 1246 | ptvWait = &tvWait; |
| 1247 | |
| 1248 | // Calculate when to return in a timeval |
| 1249 | gettimeofday(&tvStop, NULL); |
| 1250 | tvStop.tv_sec += tvWait.tv_sec; |
| 1251 | tvStop.tv_usec += tvWait.tv_usec; |
| 1252 | if (tvStop.tv_usec >= 1000000) { |
| 1253 | tvStop.tv_usec -= 1000000; |
| 1254 | tvStop.tv_sec += 1; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | // Zero all fd_sets. Don't need to do this inside the loop since |
| 1259 | // select() zeros the descriptors not signaled |
| 1260 | |
| 1261 | fd_set fdsRead; |
| 1262 | FD_ZERO(&fdsRead); |
| 1263 | fd_set fdsWrite; |
| 1264 | FD_ZERO(&fdsWrite); |
pbos@webrtc.org | 27e5898 | 2014-10-07 17:56:53 +0000 | [diff] [blame] | 1265 | // Explicitly unpoison these FDs on MemorySanitizer which doesn't handle the |
| 1266 | // inline assembly in FD_ZERO. |
| 1267 | // http://crbug.com/344505 |
| 1268 | #ifdef MEMORY_SANITIZER |
| 1269 | __msan_unpoison(&fdsRead, sizeof(fdsRead)); |
| 1270 | __msan_unpoison(&fdsWrite, sizeof(fdsWrite)); |
| 1271 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1272 | |
| 1273 | fWait_ = true; |
| 1274 | |
| 1275 | while (fWait_) { |
| 1276 | int fdmax = -1; |
| 1277 | { |
| 1278 | CritScope cr(&crit_); |
| 1279 | for (size_t i = 0; i < dispatchers_.size(); ++i) { |
| 1280 | // Query dispatchers for read and write wait state |
| 1281 | Dispatcher *pdispatcher = dispatchers_[i]; |
| 1282 | ASSERT(pdispatcher); |
| 1283 | if (!process_io && (pdispatcher != signal_wakeup_)) |
| 1284 | continue; |
| 1285 | int fd = pdispatcher->GetDescriptor(); |
| 1286 | if (fd > fdmax) |
| 1287 | fdmax = fd; |
| 1288 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1289 | uint32_t ff = pdispatcher->GetRequestedEvents(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1290 | if (ff & (DE_READ | DE_ACCEPT)) |
| 1291 | FD_SET(fd, &fdsRead); |
| 1292 | if (ff & (DE_WRITE | DE_CONNECT)) |
| 1293 | FD_SET(fd, &fdsWrite); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | // Wait then call handlers as appropriate |
| 1298 | // < 0 means error |
| 1299 | // 0 means timeout |
| 1300 | // > 0 means count of descriptors ready |
| 1301 | int n = select(fdmax + 1, &fdsRead, &fdsWrite, NULL, ptvWait); |
| 1302 | |
| 1303 | // If error, return error. |
| 1304 | if (n < 0) { |
| 1305 | if (errno != EINTR) { |
| 1306 | LOG_E(LS_ERROR, EN, errno) << "select"; |
| 1307 | return false; |
| 1308 | } |
| 1309 | // Else ignore the error and keep going. If this EINTR was for one of the |
| 1310 | // signals managed by this PhysicalSocketServer, the |
| 1311 | // PosixSignalDeliveryDispatcher will be in the signaled state in the next |
| 1312 | // iteration. |
| 1313 | } else if (n == 0) { |
| 1314 | // If timeout, return success |
| 1315 | return true; |
| 1316 | } else { |
| 1317 | // We have signaled descriptors |
| 1318 | CritScope cr(&crit_); |
| 1319 | for (size_t i = 0; i < dispatchers_.size(); ++i) { |
| 1320 | Dispatcher *pdispatcher = dispatchers_[i]; |
| 1321 | int fd = pdispatcher->GetDescriptor(); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1322 | uint32_t ff = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1323 | int errcode = 0; |
| 1324 | |
| 1325 | // Reap any error code, which can be signaled through reads or writes. |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 1326 | // TODO(pthatcher): Should we set errcode if getsockopt fails? |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1327 | if (FD_ISSET(fd, &fdsRead) || FD_ISSET(fd, &fdsWrite)) { |
| 1328 | socklen_t len = sizeof(errcode); |
| 1329 | ::getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &len); |
| 1330 | } |
| 1331 | |
| 1332 | // Check readable descriptors. If we're waiting on an accept, signal |
| 1333 | // that. Otherwise we're waiting for data, check to see if we're |
| 1334 | // readable or really closed. |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 1335 | // TODO(pthatcher): Only peek at TCP descriptors. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1336 | if (FD_ISSET(fd, &fdsRead)) { |
| 1337 | FD_CLR(fd, &fdsRead); |
| 1338 | if (pdispatcher->GetRequestedEvents() & DE_ACCEPT) { |
| 1339 | ff |= DE_ACCEPT; |
| 1340 | } else if (errcode || pdispatcher->IsDescriptorClosed()) { |
| 1341 | ff |= DE_CLOSE; |
| 1342 | } else { |
| 1343 | ff |= DE_READ; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | // Check writable descriptors. If we're waiting on a connect, detect |
| 1348 | // success versus failure by the reaped error code. |
| 1349 | if (FD_ISSET(fd, &fdsWrite)) { |
| 1350 | FD_CLR(fd, &fdsWrite); |
| 1351 | if (pdispatcher->GetRequestedEvents() & DE_CONNECT) { |
| 1352 | if (!errcode) { |
| 1353 | ff |= DE_CONNECT; |
| 1354 | } else { |
| 1355 | ff |= DE_CLOSE; |
| 1356 | } |
| 1357 | } else { |
| 1358 | ff |= DE_WRITE; |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | // Tell the descriptor about the event. |
| 1363 | if (ff != 0) { |
| 1364 | pdispatcher->OnPreEvent(ff); |
| 1365 | pdispatcher->OnEvent(ff, errcode); |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | // Recalc the time remaining to wait. Doing it here means it doesn't get |
| 1371 | // calced twice the first time through the loop |
| 1372 | if (ptvWait) { |
| 1373 | ptvWait->tv_sec = 0; |
| 1374 | ptvWait->tv_usec = 0; |
| 1375 | struct timeval tvT; |
| 1376 | gettimeofday(&tvT, NULL); |
| 1377 | if ((tvStop.tv_sec > tvT.tv_sec) |
| 1378 | || ((tvStop.tv_sec == tvT.tv_sec) |
| 1379 | && (tvStop.tv_usec > tvT.tv_usec))) { |
| 1380 | ptvWait->tv_sec = tvStop.tv_sec - tvT.tv_sec; |
| 1381 | ptvWait->tv_usec = tvStop.tv_usec - tvT.tv_usec; |
| 1382 | if (ptvWait->tv_usec < 0) { |
| 1383 | ASSERT(ptvWait->tv_sec > 0); |
| 1384 | ptvWait->tv_usec += 1000000; |
| 1385 | ptvWait->tv_sec -= 1; |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | return true; |
| 1392 | } |
| 1393 | |
| 1394 | static void GlobalSignalHandler(int signum) { |
| 1395 | PosixSignalHandler::Instance()->OnPosixSignalReceived(signum); |
| 1396 | } |
| 1397 | |
| 1398 | bool PhysicalSocketServer::SetPosixSignalHandler(int signum, |
| 1399 | void (*handler)(int)) { |
| 1400 | // If handler is SIG_IGN or SIG_DFL then clear our user-level handler, |
| 1401 | // otherwise set one. |
| 1402 | if (handler == SIG_IGN || handler == SIG_DFL) { |
| 1403 | if (!InstallSignal(signum, handler)) { |
| 1404 | return false; |
| 1405 | } |
| 1406 | if (signal_dispatcher_) { |
| 1407 | signal_dispatcher_->ClearHandler(signum); |
| 1408 | if (!signal_dispatcher_->HasHandlers()) { |
| 1409 | signal_dispatcher_.reset(); |
| 1410 | } |
| 1411 | } |
| 1412 | } else { |
| 1413 | if (!signal_dispatcher_) { |
| 1414 | signal_dispatcher_.reset(new PosixSignalDispatcher(this)); |
| 1415 | } |
| 1416 | signal_dispatcher_->SetHandler(signum, handler); |
| 1417 | if (!InstallSignal(signum, &GlobalSignalHandler)) { |
| 1418 | return false; |
| 1419 | } |
| 1420 | } |
| 1421 | return true; |
| 1422 | } |
| 1423 | |
| 1424 | Dispatcher* PhysicalSocketServer::signal_dispatcher() { |
| 1425 | return signal_dispatcher_.get(); |
| 1426 | } |
| 1427 | |
| 1428 | bool PhysicalSocketServer::InstallSignal(int signum, void (*handler)(int)) { |
| 1429 | struct sigaction act; |
| 1430 | // It doesn't really matter what we set this mask to. |
| 1431 | if (sigemptyset(&act.sa_mask) != 0) { |
| 1432 | LOG_ERR(LS_ERROR) << "Couldn't set mask"; |
| 1433 | return false; |
| 1434 | } |
| 1435 | act.sa_handler = handler; |
| 1436 | #if !defined(__native_client__) |
| 1437 | // Use SA_RESTART so that our syscalls don't get EINTR, since we don't need it |
| 1438 | // and it's a nuisance. Though some syscalls still return EINTR and there's no |
| 1439 | // real standard for which ones. :( |
| 1440 | act.sa_flags = SA_RESTART; |
| 1441 | #else |
| 1442 | act.sa_flags = 0; |
| 1443 | #endif |
| 1444 | if (sigaction(signum, &act, NULL) != 0) { |
| 1445 | LOG_ERR(LS_ERROR) << "Couldn't set sigaction"; |
| 1446 | return false; |
| 1447 | } |
| 1448 | return true; |
| 1449 | } |
| 1450 | #endif // WEBRTC_POSIX |
| 1451 | |
| 1452 | #if defined(WEBRTC_WIN) |
| 1453 | bool PhysicalSocketServer::Wait(int cmsWait, bool process_io) { |
| 1454 | int cmsTotal = cmsWait; |
| 1455 | int cmsElapsed = 0; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1456 | uint32_t msStart = Time(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1457 | |
| 1458 | fWait_ = true; |
| 1459 | while (fWait_) { |
| 1460 | std::vector<WSAEVENT> events; |
| 1461 | std::vector<Dispatcher *> event_owners; |
| 1462 | |
| 1463 | events.push_back(socket_ev_); |
| 1464 | |
| 1465 | { |
| 1466 | CritScope cr(&crit_); |
| 1467 | size_t i = 0; |
| 1468 | iterators_.push_back(&i); |
| 1469 | // Don't track dispatchers_.size(), because we want to pick up any new |
| 1470 | // dispatchers that were added while processing the loop. |
| 1471 | while (i < dispatchers_.size()) { |
| 1472 | Dispatcher* disp = dispatchers_[i++]; |
| 1473 | if (!process_io && (disp != signal_wakeup_)) |
| 1474 | continue; |
| 1475 | SOCKET s = disp->GetSocket(); |
| 1476 | if (disp->CheckSignalClose()) { |
| 1477 | // We just signalled close, don't poll this socket |
| 1478 | } else if (s != INVALID_SOCKET) { |
| 1479 | WSAEventSelect(s, |
| 1480 | events[0], |
| 1481 | FlagsToEvents(disp->GetRequestedEvents())); |
| 1482 | } else { |
| 1483 | events.push_back(disp->GetWSAEvent()); |
| 1484 | event_owners.push_back(disp); |
| 1485 | } |
| 1486 | } |
| 1487 | ASSERT(iterators_.back() == &i); |
| 1488 | iterators_.pop_back(); |
| 1489 | } |
| 1490 | |
| 1491 | // Which is shorter, the delay wait or the asked wait? |
| 1492 | |
| 1493 | int cmsNext; |
| 1494 | if (cmsWait == kForever) { |
| 1495 | cmsNext = cmsWait; |
| 1496 | } else { |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 1497 | cmsNext = std::max(0, cmsTotal - cmsElapsed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | // Wait for one of the events to signal |
| 1501 | DWORD dw = WSAWaitForMultipleEvents(static_cast<DWORD>(events.size()), |
| 1502 | &events[0], |
| 1503 | false, |
| 1504 | cmsNext, |
| 1505 | false); |
| 1506 | |
| 1507 | if (dw == WSA_WAIT_FAILED) { |
| 1508 | // Failed? |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 1509 | // TODO(pthatcher): need a better strategy than this! |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1510 | WSAGetLastError(); |
| 1511 | ASSERT(false); |
| 1512 | return false; |
| 1513 | } else if (dw == WSA_WAIT_TIMEOUT) { |
| 1514 | // Timeout? |
| 1515 | return true; |
| 1516 | } else { |
| 1517 | // Figure out which one it is and call it |
| 1518 | CritScope cr(&crit_); |
| 1519 | int index = dw - WSA_WAIT_EVENT_0; |
| 1520 | if (index > 0) { |
| 1521 | --index; // The first event is the socket event |
| 1522 | event_owners[index]->OnPreEvent(0); |
| 1523 | event_owners[index]->OnEvent(0, 0); |
| 1524 | } else if (process_io) { |
| 1525 | size_t i = 0, end = dispatchers_.size(); |
| 1526 | iterators_.push_back(&i); |
| 1527 | iterators_.push_back(&end); // Don't iterate over new dispatchers. |
| 1528 | while (i < end) { |
| 1529 | Dispatcher* disp = dispatchers_[i++]; |
| 1530 | SOCKET s = disp->GetSocket(); |
| 1531 | if (s == INVALID_SOCKET) |
| 1532 | continue; |
| 1533 | |
| 1534 | WSANETWORKEVENTS wsaEvents; |
| 1535 | int err = WSAEnumNetworkEvents(s, events[0], &wsaEvents); |
| 1536 | if (err == 0) { |
| 1537 | |
| 1538 | #if LOGGING |
| 1539 | { |
| 1540 | if ((wsaEvents.lNetworkEvents & FD_READ) && |
| 1541 | wsaEvents.iErrorCode[FD_READ_BIT] != 0) { |
| 1542 | LOG(WARNING) << "PhysicalSocketServer got FD_READ_BIT error " |
| 1543 | << wsaEvents.iErrorCode[FD_READ_BIT]; |
| 1544 | } |
| 1545 | if ((wsaEvents.lNetworkEvents & FD_WRITE) && |
| 1546 | wsaEvents.iErrorCode[FD_WRITE_BIT] != 0) { |
| 1547 | LOG(WARNING) << "PhysicalSocketServer got FD_WRITE_BIT error " |
| 1548 | << wsaEvents.iErrorCode[FD_WRITE_BIT]; |
| 1549 | } |
| 1550 | if ((wsaEvents.lNetworkEvents & FD_CONNECT) && |
| 1551 | wsaEvents.iErrorCode[FD_CONNECT_BIT] != 0) { |
| 1552 | LOG(WARNING) << "PhysicalSocketServer got FD_CONNECT_BIT error " |
| 1553 | << wsaEvents.iErrorCode[FD_CONNECT_BIT]; |
| 1554 | } |
| 1555 | if ((wsaEvents.lNetworkEvents & FD_ACCEPT) && |
| 1556 | wsaEvents.iErrorCode[FD_ACCEPT_BIT] != 0) { |
| 1557 | LOG(WARNING) << "PhysicalSocketServer got FD_ACCEPT_BIT error " |
| 1558 | << wsaEvents.iErrorCode[FD_ACCEPT_BIT]; |
| 1559 | } |
| 1560 | if ((wsaEvents.lNetworkEvents & FD_CLOSE) && |
| 1561 | wsaEvents.iErrorCode[FD_CLOSE_BIT] != 0) { |
| 1562 | LOG(WARNING) << "PhysicalSocketServer got FD_CLOSE_BIT error " |
| 1563 | << wsaEvents.iErrorCode[FD_CLOSE_BIT]; |
| 1564 | } |
| 1565 | } |
| 1566 | #endif |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1567 | uint32_t ff = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1568 | int errcode = 0; |
| 1569 | if (wsaEvents.lNetworkEvents & FD_READ) |
| 1570 | ff |= DE_READ; |
| 1571 | if (wsaEvents.lNetworkEvents & FD_WRITE) |
| 1572 | ff |= DE_WRITE; |
| 1573 | if (wsaEvents.lNetworkEvents & FD_CONNECT) { |
| 1574 | if (wsaEvents.iErrorCode[FD_CONNECT_BIT] == 0) { |
| 1575 | ff |= DE_CONNECT; |
| 1576 | } else { |
| 1577 | ff |= DE_CLOSE; |
| 1578 | errcode = wsaEvents.iErrorCode[FD_CONNECT_BIT]; |
| 1579 | } |
| 1580 | } |
| 1581 | if (wsaEvents.lNetworkEvents & FD_ACCEPT) |
| 1582 | ff |= DE_ACCEPT; |
| 1583 | if (wsaEvents.lNetworkEvents & FD_CLOSE) { |
| 1584 | ff |= DE_CLOSE; |
| 1585 | errcode = wsaEvents.iErrorCode[FD_CLOSE_BIT]; |
| 1586 | } |
| 1587 | if (ff != 0) { |
| 1588 | disp->OnPreEvent(ff); |
| 1589 | disp->OnEvent(ff, errcode); |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | ASSERT(iterators_.back() == &end); |
| 1594 | iterators_.pop_back(); |
| 1595 | ASSERT(iterators_.back() == &i); |
| 1596 | iterators_.pop_back(); |
| 1597 | } |
| 1598 | |
| 1599 | // Reset the network event until new activity occurs |
| 1600 | WSAResetEvent(socket_ev_); |
| 1601 | } |
| 1602 | |
| 1603 | // Break? |
| 1604 | if (!fWait_) |
| 1605 | break; |
| 1606 | cmsElapsed = TimeSince(msStart); |
| 1607 | if ((cmsWait != kForever) && (cmsElapsed >= cmsWait)) { |
| 1608 | break; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | // Done |
| 1613 | return true; |
| 1614 | } |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 1615 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1616 | |
| 1617 | } // namespace rtc |