File: SocketContext.cpp

package info (click to toggle)
dolphin-emu 2512%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 76,328 kB
  • sloc: cpp: 499,023; ansic: 119,674; python: 6,547; sh: 2,338; makefile: 1,093; asm: 726; pascal: 257; javascript: 183; perl: 97; objc: 75; xml: 30
file content (55 lines) | stat: -rw-r--r-- 1,512 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2021 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "Common/SocketContext.h"

#include "Common/Logging/Log.h"
#include "Common/Network.h"

namespace Common
{
#ifdef _WIN32
SocketContext::SocketContext()
{
  std::lock_guard<std::mutex> g(s_lock);
  if (s_num_objects == 0)
  {
    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));
    }
  }
  s_num_objects++;
}
SocketContext::~SocketContext()
{
  std::lock_guard<std::mutex> g(s_lock);
  s_num_objects--;
  if (s_num_objects == 0)
  {
    WSACleanup();
  }
}

std::mutex SocketContext::s_lock;
size_t SocketContext::s_num_objects = 0;
WSADATA SocketContext::s_data;

#else
SocketContext::SocketContext() = default;
SocketContext::~SocketContext() = default;
#endif
}  // namespace Common