From 00bc7e6b380f432e4a7d957a2b2d460a715176f5 Mon Sep 17 00:00:00 2001 From: iwubcode Date: Thu, 4 Mar 2021 13:43:35 -0600 Subject: [PATCH] Common: Add RAII object that initializes and cleans up winsock --- Source/Core/Common/CMakeLists.txt | 2 ++ Source/Core/Common/SocketContext.cpp | 22 ++++++++++++++++++++ Source/Core/Common/SocketContext.h | 30 ++++++++++++++++++++++++++++ Source/Core/DolphinLib.props | 2 ++ 4 files changed, 56 insertions(+) create mode 100644 Source/Core/Common/SocketContext.cpp create mode 100644 Source/Core/Common/SocketContext.h 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(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 +#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/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 @@ + @@ -718,6 +719,7 @@ +