Remove rtc::EnsureWinsockInit and g_winsockinit.

In the effort of enabling -Wglobal-constructors and
-Wexit-time-destructors, WebRTC has to remove the Winsock global
initializer.

This will also remove it from Chromium (since it was unused).

After this CL, applications will have to explicitly initialize Winsock
before using WebRTC, this can be done by using the class
rtc::WinsockInitializer provided in rtc_base/win32socketinit.h.

Bug: webrtc:9693, webrtc:9754
Change-Id: I4aae12ff43671ef2713a6fc4592e20759dc6b495
Reviewed-on: https://webrtc-review.googlesource.com/99660
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24903}
This commit is contained in:
Mirko Bonadei
2018-09-17 12:20:10 +02:00
committed by Commit Bot
parent 49753428ff
commit ba5eaee9a2
7 changed files with 37 additions and 65 deletions

View File

@ -21,7 +21,7 @@ int PASCAL wWinMain(HINSTANCE instance,
HINSTANCE prev_instance, HINSTANCE prev_instance,
wchar_t* cmd_line, wchar_t* cmd_line,
int cmd_show) { int cmd_show) {
rtc::EnsureWinsockInit(); rtc::WinsockInitializer winsock_init;
rtc::Win32SocketServer w32_ss; rtc::Win32SocketServer w32_ss;
rtc::Win32Thread w32_thread(&w32_ss); rtc::Win32Thread w32_thread(&w32_ss);
rtc::ThreadManager::Instance()->SetCurrentThread(&w32_thread); rtc::ThreadManager::Instance()->SetCurrentThread(&w32_thread);

View File

@ -871,10 +871,6 @@ rtc_static_library("rtc_base_generic") {
] ]
if (build_with_chromium) { if (build_with_chromium) {
if (is_win) {
sources += [ "../../webrtc_overrides/rtc_base/win32socketinit.cc" ]
deps += [ "//net" ]
}
include_dirs = [ "../../boringssl/src/include" ] include_dirs = [ "../../boringssl/src/include" ]
public_configs += [ ":rtc_base_chromium_config" ] public_configs += [ ":rtc_base_chromium_config" ]
} else { } else {
@ -890,12 +886,7 @@ rtc_static_library("rtc_base_generic") {
] ]
if (is_win) { if (is_win) {
configs += [
"..:no_exit_time_destructors",
"..:no_global_constructors",
]
sources += [ sources += [
"win32socketinit.cc",
"win32socketinit.h", "win32socketinit.h",
"win32socketserver.cc", "win32socketserver.cc",
"win32socketserver.h", "win32socketserver.h",

View File

@ -50,7 +50,6 @@
#include "rtc_base/networkmonitor.h" #include "rtc_base/networkmonitor.h"
#include "rtc_base/nullsocketserver.h" #include "rtc_base/nullsocketserver.h"
#include "rtc_base/timeutils.h" #include "rtc_base/timeutils.h"
#include "rtc_base/win32socketinit.h"
#if defined(WEBRTC_WIN) #if defined(WEBRTC_WIN)
#define LAST_SYSTEM_ERROR (::GetLastError()) #define LAST_SYSTEM_ERROR (::GetLastError())
@ -117,14 +116,6 @@ PhysicalSocket::PhysicalSocket(PhysicalSocketServer* ss, SOCKET s)
error_(0), error_(0),
state_((s == INVALID_SOCKET) ? CS_CLOSED : CS_CONNECTED), state_((s == INVALID_SOCKET) ? CS_CLOSED : CS_CONNECTED),
resolver_(nullptr) { 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) { if (s_ != INVALID_SOCKET) {
SetEnabledEvents(DE_READ | DE_WRITE); SetEnabledEvents(DE_READ | DE_WRITE);

View File

@ -24,6 +24,10 @@
#include "test/field_trial.h" #include "test/field_trial.h"
#include "test/testsupport/fileutils.h" #include "test/testsupport/fileutils.h"
#if defined(WEBRTC_WIN)
#include "rtc_base/win32socketinit.h"
#endif
#if defined(WEBRTC_IOS) #if defined(WEBRTC_IOS)
#include "test/ios/test_support.h" #include "test/ios/test_support.h"
#endif #endif
@ -85,6 +89,8 @@ int main(int argc, char* argv[]) {
webrtc::metrics::Enable(); webrtc::metrics::Enable();
#if defined(WEBRTC_WIN) #if defined(WEBRTC_WIN)
rtc::WinsockInitializer winsock_init;
if (!FLAG_default_error_handlers) { if (!FLAG_default_error_handlers) {
// Make sure any errors don't throw dialogs hanging the test run. // Make sure any errors don't throw dialogs hanging the test run.
_set_invalid_parameter_handler(TestInvalidParameterHandler); _set_invalid_parameter_handler(TestInvalidParameterHandler);

View File

@ -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

View File

@ -11,9 +11,30 @@
#ifndef RTC_BASE_WIN32SOCKETINIT_H_ #ifndef RTC_BASE_WIN32SOCKETINIT_H_
#define 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 { 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 } // namespace rtc

View File

@ -21,6 +21,10 @@
#include "test/testsupport/fileutils.h" #include "test/testsupport/fileutils.h"
#include "test/testsupport/perf_test.h" #include "test/testsupport/perf_test.h"
#if defined(WEBRTC_WIN)
#include "rtc_base/win32socketinit.h"
#endif
#if defined(WEBRTC_IOS) #if defined(WEBRTC_IOS)
#include "test/ios/test_support.h" #include "test/ios/test_support.h"
@ -89,6 +93,10 @@ int main(int argc, char* argv[]) {
webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials); webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials);
webrtc::metrics::Enable(); webrtc::metrics::Enable();
#if defined(WEBRTC_WIN)
rtc::WinsockInitializer winsock_init;
#endif
rtc::LogMessage::SetLogToStderr(FLAG_logs); rtc::LogMessage::SetLogToStderr(FLAG_logs);
// Ensure that main thread gets wrapped as an rtc::Thread. // Ensure that main thread gets wrapped as an rtc::Thread.