Skip to content

Commit

Permalink
Merge pull request #9559 from iwubcode/gdb-stub-raii
Browse files Browse the repository at this point in the history
Common / Core: add raii object that cleans up WSA on destruction in gdb-stub
  • Loading branch information
JMC47 committed Mar 5, 2021
2 parents adcdeda + 7d50528 commit fc86e55
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Source/Core/Common/CMakeLists.txt
Expand Up @@ -110,6 +110,8 @@ add_library(common
SettingsHandler.h
SFMLHelper.cpp
SFMLHelper.h
SocketContext.cpp
SocketContext.h
SPSCQueue.h
StringUtil.cpp
StringUtil.h
Expand Down
22 changes: 22 additions & 0 deletions Source/Core/Common/SocketContext.cpp
@@ -0,0 +1,22 @@
// Copyright 2021 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "Common/SocketContext.h"

namespace Common
{
#ifdef _WIN32
SocketContext::SocketContext()
{
static_cast<void>(WSAStartup(MAKEWORD(2, 2), &m_data));
}
SocketContext::~SocketContext()
{
WSACleanup();
}
#else
SocketContext::SocketContext() = default;
SocketContext::~SocketContext() = default;
#endif
} // namespace Common
30 changes: 30 additions & 0 deletions Source/Core/Common/SocketContext.h
@@ -0,0 +1,30 @@
// Copyright 2021 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#ifdef _WIN32
#include <WinSock2.h>
#endif

namespace Common
{
class SocketContext
{
public:
SocketContext();
~SocketContext();

SocketContext(const SocketContext&) = delete;
SocketContext(SocketContext&&) = delete;

SocketContext& operator=(const SocketContext&) = delete;
SocketContext& operator=(SocketContext&&) = delete;

private:
#ifdef _WIN32
WSADATA m_data;
#endif
};
} // namespace Common
11 changes: 0 additions & 11 deletions Source/Core/Core/IOS/Network/IP/Top.cpp
Expand Up @@ -65,17 +65,6 @@ enum SOResultCode : s32
NetIPTopDevice::NetIPTopDevice(Kernel& ios, const std::string& device_name)
: Device(ios, device_name)
{
#ifdef _WIN32
const int ret = WSAStartup(MAKEWORD(2, 2), &InitData);
INFO_LOG_FMT(IOS_NET, "WSAStartup: {}", ret);
#endif
}

NetIPTopDevice::~NetIPTopDevice()
{
#ifdef _WIN32
WSACleanup();
#endif
}

void NetIPTopDevice::DoState(PointerWrap& p)
Expand Down
6 changes: 2 additions & 4 deletions Source/Core/Core/IOS/Network/IP/Top.h
Expand Up @@ -7,6 +7,7 @@
#include <string>

#include "Common/CommonTypes.h"
#include "Common/SocketContext.h"
#include "Core/IOS/Device.h"

#ifdef _WIN32
Expand Down Expand Up @@ -65,7 +66,6 @@ class NetIPTopDevice : public Device
{
public:
NetIPTopDevice(Kernel& ios, const std::string& device_name);
virtual ~NetIPTopDevice();

void DoState(PointerWrap& p) override;
std::optional<IPCReply> IOCtl(const IOCtlRequest& request) override;
Expand Down Expand Up @@ -99,8 +99,6 @@ class NetIPTopDevice : public Device
IPCReply HandleGetAddressInfoRequest(const IOCtlVRequest& request);
IPCReply HandleICMPPingRequest(const IOCtlVRequest& request);

#ifdef _WIN32
WSADATA InitData;
#endif
Common::SocketContext m_socket_context;
};
} // namespace IOS::HLE
20 changes: 9 additions & 11 deletions Source/Core/Core/PowerPC/GDBStub.cpp
Expand Up @@ -4,6 +4,7 @@

// Originally written by Sven Peter <sven@fail0verflow.com> for anergistic.

#include <optional>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
Expand All @@ -23,6 +24,7 @@ typedef SSIZE_T ssize_t;
#endif

#include "Common/Logging/Log.h"
#include "Common/SocketContext.h"
#include "Core/HW/CPU.h"
#include "Core/HW/Memmap.h"
#include "Core/Host.h"
Expand All @@ -31,6 +33,11 @@ typedef SSIZE_T ssize_t;
#include "Core/PowerPC/PPCCache.h"
#include "Core/PowerPC/PowerPC.h"

namespace
{
std::optional<Common::SocketContext> s_socket_context;
} // namespace

#define GDB_BFR_MAX 10000
#define GDB_MAX_BP 10

Expand Down Expand Up @@ -791,10 +798,6 @@ void gdb_handle_exception()
}
}

#ifdef _WIN32
WSADATA InitData;
#endif

// exported functions

static void gdb_init_generic(int domain, const sockaddr* server_addr, socklen_t server_addrlen,
Expand Down Expand Up @@ -833,10 +836,7 @@ void gdb_init(u32 port)
static void gdb_init_generic(int domain, const sockaddr* server_addr, socklen_t server_addrlen,
sockaddr* client_addr, socklen_t* client_addrlen)
{
#ifdef _WIN32
WSAStartup(MAKEWORD(2, 2), &InitData);
#endif

s_socket_context.emplace();
memset(bp_x, 0, sizeof bp_x);
memset(bp_r, 0, sizeof bp_r);
memset(bp_w, 0, sizeof bp_w);
Expand Down Expand Up @@ -884,9 +884,7 @@ void gdb_deinit()
sock = -1;
}

#ifdef _WIN32
WSACleanup();
#endif
s_socket_context.reset();
}

bool gdb_active()
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/DolphinLib.props
Expand Up @@ -142,6 +142,7 @@
<ClInclude Include="Common\Semaphore.h" />
<ClInclude Include="Common\SettingsHandler.h" />
<ClInclude Include="Common\SFMLHelper.h" />
<ClInclude Include="Common\SocketContext.h" />
<ClInclude Include="Common\SPSCQueue.h" />
<ClInclude Include="Common\StringUtil.h" />
<ClInclude Include="Common\Swap.h" />
Expand Down Expand Up @@ -718,6 +719,7 @@
<ClCompile Include="Common\SDCardUtil.cpp" />
<ClCompile Include="Common\SettingsHandler.cpp" />
<ClCompile Include="Common\SFMLHelper.cpp" />
<ClCompile Include="Common\SocketContext.cpp" />
<ClCompile Include="Common\StringUtil.cpp" />
<ClCompile Include="Common\SymbolDB.cpp" />
<ClCompile Include="Common\Thread.cpp" />
Expand Down

0 comments on commit fc86e55

Please sign in to comment.