summaryrefslogtreecommitdiff
path: root/Source
diff options
context:
space:
mode:
authorJMC47 <JMC4789@gmail.com>2021-03-05 05:28:31 -0500
committerGitHub <noreply@github.com>2021-03-05 05:28:31 -0500
commitfc86e554e03916c8f9144f8dba9048977fb21300 (patch)
tree3a65bc268b5b8cd022766b6588c7dd805b09274d /Source
parentadcdeda372a37f93652c28131b58de93e40857d9 (diff)
parent7d5052896df794ec6c15bedcf7fe54144b5e301c (diff)
Merge pull request #9559 from iwubcode/gdb-stub-raii
Common / Core: add raii object that cleans up WSA on destruction in gdb-stub
Diffstat (limited to 'Source')
-rw-r--r--Source/Core/Common/CMakeLists.txt2
-rw-r--r--Source/Core/Common/SocketContext.cpp22
-rw-r--r--Source/Core/Common/SocketContext.h30
-rw-r--r--Source/Core/Core/IOS/Network/IP/Top.cpp11
-rw-r--r--Source/Core/Core/IOS/Network/IP/Top.h6
-rw-r--r--Source/Core/Core/PowerPC/GDBStub.cpp20
-rw-r--r--Source/Core/DolphinLib.props2
7 files changed, 67 insertions, 26 deletions
diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt
index 14a1863fcf..4a3623d4e8 100644
--- a/Source/Core/Common/CMakeLists.txt
+++ b/Source/Core/Common/CMakeLists.txt
@@ -110,6 +110,8 @@ add_library(common
SettingsHandler.h
SFMLHelper.cpp
SFMLHelper.h
+ SocketContext.cpp
+ SocketContext.h
SPSCQueue.h
StringUtil.cpp
StringUtil.h
diff --git a/Source/Core/Common/SocketContext.cpp b/Source/Core/Common/SocketContext.cpp
new file mode 100644
index 0000000000..e9174bc0d9
--- /dev/null
+++ b/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
diff --git a/Source/Core/Common/SocketContext.h b/Source/Core/Common/SocketContext.h
new file mode 100644
index 0000000000..1645201f1f
--- /dev/null
+++ b/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
diff --git a/Source/Core/Core/IOS/Network/IP/Top.cpp b/Source/Core/Core/IOS/Network/IP/Top.cpp
index 88a91d7a02..c34e938e27 100644
--- a/Source/Core/Core/IOS/Network/IP/Top.cpp
+++ b/Source/Core/Core/IOS/Network/IP/Top.cpp
@@ -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)
diff --git a/Source/Core/Core/IOS/Network/IP/Top.h b/Source/Core/Core/IOS/Network/IP/Top.h
index d87885d165..3e78ea2b06 100644
--- a/Source/Core/Core/IOS/Network/IP/Top.h
+++ b/Source/Core/Core/IOS/Network/IP/Top.h
@@ -7,6 +7,7 @@
#include <string>
#include "Common/CommonTypes.h"
+#include "Common/SocketContext.h"
#include "Core/IOS/Device.h"
#ifdef _WIN32
@@ -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;
@@ -99,8 +99,6 @@ private:
IPCReply HandleGetAddressInfoRequest(const IOCtlVRequest& request);
IPCReply HandleICMPPingRequest(const IOCtlVRequest& request);
-#ifdef _WIN32
- WSADATA InitData;
-#endif
+ Common::SocketContext m_socket_context;
};
} // namespace IOS::HLE
diff --git a/Source/Core/Core/PowerPC/GDBStub.cpp b/Source/Core/Core/PowerPC/GDBStub.cpp
index 20a0ed5939..8b2293d132 100644
--- a/Source/Core/Core/PowerPC/GDBStub.cpp
+++ b/Source/Core/Core/PowerPC/GDBStub.cpp
@@ -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>
@@ -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"
@@ -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
@@ -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,
@@ -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);
@@ -884,9 +884,7 @@ void gdb_deinit()
sock = -1;
}
-#ifdef _WIN32
- WSACleanup();
-#endif
+ s_socket_context.reset();
}
bool gdb_active()
diff --git a/Source/Core/DolphinLib.props b/Source/Core/DolphinLib.props
index ae228c0733..d78534993a 100644
--- a/Source/Core/DolphinLib.props
+++ b/Source/Core/DolphinLib.props
@@ -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" />
@@ -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" />