OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/base/winsock_util.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "net/base/net_errors.h" |
| 9 |
| 10 namespace net { |
| 11 |
| 12 void AssertEventNotSignaled(WSAEVENT hEvent) { |
| 13 DWORD wait_rv = WaitForSingleObject(hEvent, 0); |
| 14 if (wait_rv != WAIT_TIMEOUT) { |
| 15 DWORD err = ERROR_SUCCESS; |
| 16 if (wait_rv == WAIT_FAILED) |
| 17 err = GetLastError(); |
| 18 CHECK(false); // Crash. |
| 19 // This LOG statement is unreachable since we have already crashed, but it |
| 20 // should prevent the compiler from optimizing away the |wait_rv| and |
| 21 // |err| variables so they appear nicely on the stack in crash dumps. |
| 22 VLOG(1) << "wait_rv=" << wait_rv << ", err=" << err; |
| 23 } |
| 24 } |
| 25 |
| 26 bool ResetEventIfSignaled(WSAEVENT hEvent) { |
| 27 // TODO(wtc): Remove the CHECKs after enough testing. |
| 28 DWORD wait_rv = WaitForSingleObject(hEvent, 0); |
| 29 if (wait_rv == WAIT_TIMEOUT) |
| 30 return false; // The event object is not signaled. |
| 31 CHECK_EQ(WAIT_OBJECT_0, wait_rv); |
| 32 BOOL ok = WSAResetEvent(hEvent); |
| 33 CHECK(ok); |
| 34 return true; |
| 35 } |
| 36 |
| 37 // Map winsock error to Chromium error. |
| 38 int MapWinsockError(int os_error) { |
| 39 // There are numerous Winsock error codes, but these are the ones we thus far |
| 40 // find interesting. |
| 41 switch (os_error) { |
| 42 case WSAEACCES: |
| 43 return ERR_ACCESS_DENIED; |
| 44 case WSAENETDOWN: |
| 45 return ERR_INTERNET_DISCONNECTED; |
| 46 case WSAETIMEDOUT: |
| 47 return ERR_TIMED_OUT; |
| 48 case WSAECONNRESET: |
| 49 case WSAENETRESET: // Related to keep-alive |
| 50 return ERR_CONNECTION_RESET; |
| 51 case WSAECONNABORTED: |
| 52 return ERR_CONNECTION_ABORTED; |
| 53 case WSAECONNREFUSED: |
| 54 return ERR_CONNECTION_REFUSED; |
| 55 case WSA_IO_INCOMPLETE: |
| 56 case WSAEDISCON: |
| 57 // WSAEDISCON is returned by WSARecv or WSARecvFrom for message-oriented |
| 58 // sockets (where a return value of zero means a zero-byte message) to |
| 59 // indicate graceful connection shutdown. We should not ever see this |
| 60 // error code for TCP sockets, which are byte stream oriented. |
| 61 LOG(DFATAL) << "Unexpected error " << os_error |
| 62 << " mapped to net::ERR_UNEXPECTED"; |
| 63 return ERR_UNEXPECTED; |
| 64 case WSAEHOSTUNREACH: |
| 65 case WSAENETUNREACH: |
| 66 return ERR_ADDRESS_UNREACHABLE; |
| 67 case WSAEADDRNOTAVAIL: |
| 68 return ERR_ADDRESS_INVALID; |
| 69 case WSAENOTCONN: |
| 70 return ERR_SOCKET_NOT_CONNECTED; |
| 71 case ERROR_SUCCESS: |
| 72 return OK; |
| 73 default: |
| 74 LOG(WARNING) << "Unknown error " << os_error |
| 75 << " mapped to net::ERR_FAILED"; |
| 76 return ERR_FAILED; |
| 77 } |
| 78 } |
| 79 |
| 80 } // namespace net |
OLD | NEW |