Update include paths in miscellaneous content/ directories for base/process changes.
[chromium-blink-merge.git] / net / base / winsock_util.cc
blob8a4083a30a9dde465cc99af0cc60b41f92e66141
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.
5 #include "net/base/winsock_util.h"
7 #include "base/logging.h"
8 #include "net/base/net_errors.h"
10 namespace net {
12 namespace {
14 // Prevent the compiler from optimizing away the arguments so they appear
15 // nicely on the stack in crash dumps.
16 #pragma warning (disable: 4748)
17 #pragma optimize( "", off )
19 // Pass the important values as function arguments so that they are available
20 // in crash dumps.
21 void CheckEventWait(WSAEVENT hEvent, DWORD wait_rv, DWORD expected) {
22 if (wait_rv != expected) {
23 DWORD err = ERROR_SUCCESS;
24 if (wait_rv == WAIT_FAILED)
25 err = GetLastError();
26 CHECK(false); // Crash.
30 #pragma optimize( "", on )
31 #pragma warning (default: 4748)
33 net::PlatformSocketFactory* g_socket_factory = NULL;
35 } // namespace
37 void AssertEventNotSignaled(WSAEVENT hEvent) {
38 DWORD wait_rv = WaitForSingleObject(hEvent, 0);
39 CheckEventWait(hEvent, wait_rv, WAIT_TIMEOUT);
42 bool ResetEventIfSignaled(WSAEVENT hEvent) {
43 // TODO(wtc): Remove the CHECKs after enough testing.
44 DWORD wait_rv = WaitForSingleObject(hEvent, 0);
45 if (wait_rv == WAIT_TIMEOUT)
46 return false; // The event object is not signaled.
47 CheckEventWait(hEvent, wait_rv, WAIT_OBJECT_0);
48 BOOL ok = WSAResetEvent(hEvent);
49 CHECK(ok);
50 return true;
53 void PlatformSocketFactory::SetInstance(PlatformSocketFactory* factory) {
54 g_socket_factory = factory;
57 SOCKET CreatePlatformSocket(int family, int type, int protocol) {
58 if (g_socket_factory)
59 return g_socket_factory->CreateSocket(family, type, protocol);
60 else
61 return ::WSASocket(family, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED);
64 } // namespace net