[lldb-server] Introduce Socket::Initialize and Terminate to simply WSASocket setup

Reviewers: zturner, labath

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D60440

llvm-svn: 358044
This commit is contained in:
Aaron Smith 2019-04-10 04:57:18 +00:00
parent 5f2b5cd85e
commit f8a74c18ec
8 changed files with 55 additions and 50 deletions

View File

@ -50,6 +50,9 @@ public:
~Socket() override;
static llvm::Error Initialize();
static void Terminate();
static std::unique_ptr<Socket> Create(const SocketProtocol protocol,
bool child_processes_inherit,
Status &error);

View File

@ -19,6 +19,8 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Errno.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/WindowsError.h"
#ifndef LLDB_DISABLE_POSIX
#include "lldb/Host/posix/DomainSocket.h"
@ -78,6 +80,31 @@ Socket::Socket(SocketProtocol protocol, bool should_close,
Socket::~Socket() { Close(); }
llvm::Error Socket::Initialize() {
#if defined(_WIN32)
auto wVersion = WINSOCK_VERSION;
WSADATA wsaData;
int err = ::WSAStartup(wVersion, &wsaData);
if (err == 0) {
if (wsaData.wVersion < wVersion) {
WSACleanup();
return llvm::make_error<llvm::StringError>(
"WSASock version is not expected.", llvm::inconvertibleErrorCode());
}
} else {
return llvm::errorCodeToError(llvm::mapWindowsError(::WSAGetLastError()));
}
#endif
return llvm::Error::success();
}
void Socket::Terminate() {
#if defined(_WIN32)
::WSACleanup();
#endif
}
std::unique_ptr<Socket> Socket::Create(const SocketProtocol protocol,
bool child_processes_inherit,
Status &error) {

View File

@ -17,6 +17,7 @@
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/Host.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Host/Socket.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Reproducer.h"
#include "lldb/Utility/Timer.h"
@ -90,6 +91,11 @@ llvm::Error SystemInitializerCommon::Initialize() {
Log::Initialize();
HostInfo::Initialize();
llvm::Error error = Socket::Initialize();
if (error)
return error;
static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
@ -132,6 +138,7 @@ void SystemInitializerCommon::Terminate() {
ProcessWindowsLog::Terminate();
#endif
Socket::Terminate();
HostInfo::Terminate();
Log::DisableAllLogChannels();
FileSystem::Terminate();

View File

@ -125,8 +125,6 @@ void PlatformWindows::Initialize() {
if (g_initialize_count++ == 0) {
#if defined(_WIN32)
WSADATA dummy;
WSAStartup(MAKEWORD(2, 2), &dummy);
// Force a host flag to true for the default platform object.
PlatformSP default_platform_sp(new PlatformWindows(true));
default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
@ -139,12 +137,9 @@ void PlatformWindows::Initialize() {
}
}
void PlatformWindows::Terminate(void) {
void PlatformWindows::Terminate() {
if (g_initialize_count > 0) {
if (--g_initialize_count == 0) {
#ifdef _WIN32
WSACleanup();
#endif
PluginManager::UnregisterPlugin(PlatformWindows::CreateInstance);
}
}

View File

@ -10,6 +10,7 @@
#include "lldb/Host/ConnectionFileDescriptor.h"
#include "lldb/Host/PseudoTerminal.h"
#include "lldb/Host/common/TCPSocket.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include <future>
@ -19,17 +20,10 @@ namespace {
class MainLoopTest : public testing::Test {
public:
static void SetUpTestCase() {
#ifdef _MSC_VER
WSADATA data;
ASSERT_EQ(0, WSAStartup(MAKEWORD(2, 2), &data));
#endif
ASSERT_THAT_ERROR(Socket::Initialize(), llvm::Succeeded());
}
static void TearDownTestCase() {
#ifdef _MSC_VER
ASSERT_EQ(0, WSACleanup());
#endif
}
static void TearDownTestCase() { Socket::Terminate(); }
void SetUp() override {
bool child_processes_inherit = false;

View File

@ -6,29 +6,24 @@
//
//===----------------------------------------------------------------------===//
#include "lldb/Host/SocketAddress.h"
#include "lldb/Host/Socket.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include "lldb/Host/SocketAddress.h"
using namespace lldb_private;
namespace {
class SocketAddressTest : public testing::Test {
public:
static void SetUpTestCase() {
#ifdef _MSC_VER
WSADATA data;
ASSERT_EQ(0, WSAStartup(MAKEWORD(2, 2), &data));
#endif
}
static void TearDownTestCase() {
#ifdef _MSC_VER
ASSERT_EQ(0, WSACleanup());
#endif
ASSERT_THAT_ERROR(Socket::Initialize(), llvm::Succeeded());
}
static void TearDownTestCase() { Socket::Terminate(); }
};
} // namespace
using namespace lldb_private;
TEST_F(SocketAddressTest, Set) {
SocketAddress sa;
ASSERT_TRUE(sa.SetToLocalhost(AF_INET, 1138));

View File

@ -18,6 +18,7 @@
#include "lldb/Host/common/UDPSocket.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Testing/Support/Error.h"
#ifndef LLDB_DISABLE_POSIX
#include "lldb/Host/posix/DomainSocket.h"
@ -28,17 +29,10 @@ using namespace lldb_private;
class SocketTest : public testing::Test {
public:
void SetUp() override {
#if defined(_MSC_VER)
WSADATA data;
::WSAStartup(MAKEWORD(2, 2), &data);
#endif
ASSERT_THAT_ERROR(Socket::Initialize(), llvm::Succeeded());
}
void TearDown() override {
#if defined(_MSC_VER)
::WSACleanup();
#endif
}
void TearDown() override { Socket::Terminate(); }
protected:
static void AcceptThread(Socket *listen_socket,

View File

@ -7,27 +7,17 @@
//===----------------------------------------------------------------------===//
#include "GDBRemoteTestUtils.h"
#if defined(_MSC_VER)
#include "lldb/Host/windows/windows.h"
#include <WinSock2.h>
#endif
#include "lldb/Host/Socket.h"
#include "llvm/Testing/Support/Error.h"
namespace lldb_private {
namespace process_gdb_remote {
void GDBRemoteTest::SetUpTestCase() {
#if defined(_MSC_VER)
WSADATA data;
::WSAStartup(MAKEWORD(2, 2), &data);
#endif
ASSERT_THAT_ERROR(Socket::Initialize(), llvm::Succeeded());
}
void GDBRemoteTest::TearDownTestCase() {
#if defined(_MSC_VER)
::WSACleanup();
#endif
}
void GDBRemoteTest::TearDownTestCase() { Socket::Terminate(); }
} // namespace process_gdb_remote
} // namespace lldb_private