diff --git a/examples/peerconnection/client/main.cc b/examples/peerconnection/client/main.cc index ee70da620f..25dde1ef65 100644 --- a/examples/peerconnection/client/main.cc +++ b/examples/peerconnection/client/main.cc @@ -21,7 +21,7 @@ int PASCAL wWinMain(HINSTANCE instance, HINSTANCE prev_instance, wchar_t* cmd_line, int cmd_show) { - rtc::EnsureWinsockInit(); + rtc::WinsockInitializer winsock_init; rtc::Win32SocketServer w32_ss; rtc::Win32Thread w32_thread(&w32_ss); rtc::ThreadManager::Instance()->SetCurrentThread(&w32_thread); diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index e72e740e05..8dc98b6047 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -871,10 +871,6 @@ rtc_static_library("rtc_base_generic") { ] if (build_with_chromium) { - if (is_win) { - sources += [ "../../webrtc_overrides/rtc_base/win32socketinit.cc" ] - deps += [ "//net" ] - } include_dirs = [ "../../boringssl/src/include" ] public_configs += [ ":rtc_base_chromium_config" ] } else { @@ -890,12 +886,7 @@ rtc_static_library("rtc_base_generic") { ] if (is_win) { - configs += [ - "..:no_exit_time_destructors", - "..:no_global_constructors", - ] sources += [ - "win32socketinit.cc", "win32socketinit.h", "win32socketserver.cc", "win32socketserver.h", diff --git a/rtc_base/physicalsocketserver.cc b/rtc_base/physicalsocketserver.cc index 9a3fc4dfd4..4ad2857c00 100644 --- a/rtc_base/physicalsocketserver.cc +++ b/rtc_base/physicalsocketserver.cc @@ -50,7 +50,6 @@ #include "rtc_base/networkmonitor.h" #include "rtc_base/nullsocketserver.h" #include "rtc_base/timeutils.h" -#include "rtc_base/win32socketinit.h" #if defined(WEBRTC_WIN) #define LAST_SYSTEM_ERROR (::GetLastError()) @@ -117,14 +116,6 @@ PhysicalSocket::PhysicalSocket(PhysicalSocketServer* ss, SOCKET s) error_(0), state_((s == INVALID_SOCKET) ? CS_CLOSED : CS_CONNECTED), resolver_(nullptr) { -#if defined(WEBRTC_WIN) - // EnsureWinsockInit() ensures that winsock is initialized. The default - // version of this function doesn't do anything because winsock is - // initialized by constructor of a static object. If neccessary libjingle - // users can link it with a different version of this function by replacing - // win32socketinit.cc. See win32socketinit.cc for more details. - EnsureWinsockInit(); -#endif if (s_ != INVALID_SOCKET) { SetEnabledEvents(DE_READ | DE_WRITE); diff --git a/rtc_base/unittest_main.cc b/rtc_base/unittest_main.cc index a52f727832..8d4ff2df2d 100644 --- a/rtc_base/unittest_main.cc +++ b/rtc_base/unittest_main.cc @@ -24,6 +24,10 @@ #include "test/field_trial.h" #include "test/testsupport/fileutils.h" +#if defined(WEBRTC_WIN) +#include "rtc_base/win32socketinit.h" +#endif + #if defined(WEBRTC_IOS) #include "test/ios/test_support.h" #endif @@ -85,6 +89,8 @@ int main(int argc, char* argv[]) { webrtc::metrics::Enable(); #if defined(WEBRTC_WIN) + rtc::WinsockInitializer winsock_init; + if (!FLAG_default_error_handlers) { // Make sure any errors don't throw dialogs hanging the test run. _set_invalid_parameter_handler(TestInvalidParameterHandler); diff --git a/rtc_base/win32socketinit.cc b/rtc_base/win32socketinit.cc deleted file mode 100644 index ea0aae6054..0000000000 --- a/rtc_base/win32socketinit.cc +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2009 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "rtc_base/win32socketinit.h" - -#include "rtc_base/win32.h" - -namespace rtc { - -// Please don't remove this function. -void EnsureWinsockInit() { - // The default implementation uses a global initializer, so WSAStartup - // happens at module load time. Thus we don't need to do anything here. - // The hook is provided so that a client that statically links with - // libjingle can override it, to provide its own initialization. -} - -#if defined(WEBRTC_WIN) -class WinsockInitializer { - public: - WinsockInitializer() { - WSADATA wsaData; - WORD wVersionRequested = MAKEWORD(1, 0); - err_ = WSAStartup(wVersionRequested, &wsaData); - } - ~WinsockInitializer() { - if (!err_) - WSACleanup(); - } - int error() { return err_; } - - private: - int err_; -}; -WinsockInitializer g_winsockinit; -#endif - -} // namespace rtc diff --git a/rtc_base/win32socketinit.h b/rtc_base/win32socketinit.h index ea74809995..7f9bdccd6d 100644 --- a/rtc_base/win32socketinit.h +++ b/rtc_base/win32socketinit.h @@ -11,9 +11,30 @@ #ifndef RTC_BASE_WIN32SOCKETINIT_H_ #define RTC_BASE_WIN32SOCKETINIT_H_ +#ifndef WEBRTC_WIN +#error "Only #include this header in Windows builds" +#endif + +#include "rtc_base/win32.h" + namespace rtc { -void EnsureWinsockInit(); +class WinsockInitializer { + public: + WinsockInitializer() { + WSADATA wsaData; + WORD wVersionRequested = MAKEWORD(1, 0); + err_ = WSAStartup(wVersionRequested, &wsaData); + } + ~WinsockInitializer() { + if (!err_) + WSACleanup(); + } + int error() { return err_; } + + private: + int err_; +}; } // namespace rtc diff --git a/test/test_main.cc b/test/test_main.cc index 5bb2c10a58..c8d0b1b584 100644 --- a/test/test_main.cc +++ b/test/test_main.cc @@ -21,6 +21,10 @@ #include "test/testsupport/fileutils.h" #include "test/testsupport/perf_test.h" +#if defined(WEBRTC_WIN) +#include "rtc_base/win32socketinit.h" +#endif + #if defined(WEBRTC_IOS) #include "test/ios/test_support.h" @@ -89,6 +93,10 @@ int main(int argc, char* argv[]) { webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials); webrtc::metrics::Enable(); +#if defined(WEBRTC_WIN) + rtc::WinsockInitializer winsock_init; +#endif + rtc::LogMessage::SetLogToStderr(FLAG_logs); // Ensure that main thread gets wrapped as an rtc::Thread.