2021-03-04 12:43:35 -07:00
|
|
|
// Copyright 2021 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-03-04 12:43:35 -07:00
|
|
|
|
|
|
|
#include "Common/SocketContext.h"
|
|
|
|
|
2024-06-06 11:40:05 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/Network.h"
|
|
|
|
|
2021-03-04 12:43:35 -07:00
|
|
|
namespace Common
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
SocketContext::SocketContext()
|
|
|
|
{
|
2023-10-14 18:52:26 -06:00
|
|
|
std::lock_guard<std::mutex> g(s_lock);
|
|
|
|
if (s_num_objects == 0)
|
|
|
|
{
|
2024-06-06 11:40:05 -06:00
|
|
|
const int ret = WSAStartup(MAKEWORD(2, 2), &s_data);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
INFO_LOG_FMT(COMMON, "WSAStartup succeeded, wVersion={}.{}, wHighVersion={}.{}",
|
|
|
|
int(LOBYTE(s_data.wVersion)), int(HIBYTE(s_data.wVersion)),
|
|
|
|
int(LOBYTE(s_data.wHighVersion)), int(HIBYTE(s_data.wHighVersion)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The WSAStartup function directly returns the extended error code in the return value.
|
|
|
|
// A call to the WSAGetLastError function is not needed and should not be used.
|
|
|
|
//
|
|
|
|
// Source:
|
|
|
|
// https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsastartup
|
|
|
|
ERROR_LOG_FMT(COMMON, "WSAStartup failed with error {}: {}", ret,
|
|
|
|
Common::DecodeNetworkError(ret));
|
|
|
|
}
|
2023-10-14 18:52:26 -06:00
|
|
|
}
|
|
|
|
s_num_objects++;
|
2021-03-04 12:43:35 -07:00
|
|
|
}
|
|
|
|
SocketContext::~SocketContext()
|
|
|
|
{
|
2023-10-14 18:52:26 -06:00
|
|
|
std::lock_guard<std::mutex> g(s_lock);
|
|
|
|
s_num_objects--;
|
|
|
|
if (s_num_objects == 0)
|
|
|
|
{
|
|
|
|
WSACleanup();
|
|
|
|
}
|
2021-03-04 12:43:35 -07:00
|
|
|
}
|
2023-10-14 18:52:26 -06:00
|
|
|
|
|
|
|
std::mutex SocketContext::s_lock;
|
|
|
|
size_t SocketContext::s_num_objects = 0;
|
|
|
|
WSADATA SocketContext::s_data;
|
|
|
|
|
2021-03-04 12:43:35 -07:00
|
|
|
#else
|
|
|
|
SocketContext::SocketContext() = default;
|
|
|
|
SocketContext::~SocketContext() = default;
|
|
|
|
#endif
|
|
|
|
} // namespace Common
|