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