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 namespace { 13 14 // Prevent the compiler from optimizing away the arguments so they appear 15 // nicely on the stack in crash dumps. 16 #pragma warning(push) 17 #pragma warning (disable: 4748) 18 #pragma optimize( "", off ) 19 20 // Pass the important values as function arguments so that they are available 21 // in crash dumps. 22 void CheckEventWait(WSAEVENT hEvent, DWORD wait_rv, DWORD expected) { 23 if (wait_rv != expected) { 24 DWORD err = ERROR_SUCCESS; 25 if (wait_rv == WAIT_FAILED) 26 err = GetLastError(); 27 CHECK(false); // Crash. 28 } 29 } 30 31 #pragma optimize( "", on ) 32 #pragma warning(pop) 33 34 net::PlatformSocketFactory* g_socket_factory = NULL; 35 36 } // namespace 37 38 void AssertEventNotSignaled(WSAEVENT hEvent) { 39 DWORD wait_rv = WaitForSingleObject(hEvent, 0); 40 CheckEventWait(hEvent, wait_rv, WAIT_TIMEOUT); 41 } 42 43 bool ResetEventIfSignaled(WSAEVENT hEvent) { 44 // TODO(wtc): Remove the CHECKs after enough testing. 45 DWORD wait_rv = WaitForSingleObject(hEvent, 0); 46 if (wait_rv == WAIT_TIMEOUT) 47 return false; // The event object is not signaled. 48 CheckEventWait(hEvent, wait_rv, WAIT_OBJECT_0); 49 BOOL ok = WSAResetEvent(hEvent); 50 CHECK(ok); 51 return true; 52 } 53 54 void PlatformSocketFactory::SetInstance(PlatformSocketFactory* factory) { 55 g_socket_factory = factory; 56 } 57 58 SOCKET CreatePlatformSocket(int family, int type, int protocol) { 59 if (g_socket_factory) 60 return g_socket_factory->CreateSocket(family, type, protocol); 61 else 62 return ::WSASocket(family, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED); 63 } 64 65 } // namespace net 66