Debian Codegraph @

Source/Core/Common/QoSSession.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "Common/QoSSession.h"
5
6 #if defined(_WIN32)
7 #include <Qos2.h>
8 #pragma comment(lib, "qwave")
9 #endif
10
11 namespace Common
12 {
13 #if defined(_WIN32)
14 QoSSession::QoSSession(ENetPeer* peer, int tos_val) : m_peer(peer)
15 {
16 QOS_VERSION ver = {1, 0};
17
18 if (!QOSCreateHandle(&ver, &m_qos_handle))
19 return;
20
21 sockaddr_in sin = {};
22
23 sin.sin_family = AF_INET;
24 sin.sin_port = ENET_HOST_TO_NET_16(peer->host->address.port);
25 sin.sin_addr.s_addr = peer->host->address.host;
26
27 if (QOSAddSocketToFlow(m_qos_handle, peer->host->socket, reinterpret_cast<PSOCKADDR>(&sin),
28 QOSTrafficTypeControl, QOS_NON_ADAPTIVE_FLOW, &m_qos_flow_id))
29 {
30 // We shift the complete ToS value by 3 to get rid of the 3 bit ECN field
31 DWORD dscp = static_cast<DWORD>(tos_val >> 3);
32
33 // Sets DSCP to the same as Linux
34 // This will fail if we're not admin, but we ignore it
35 QOSSetFlow(m_qos_handle, m_qos_flow_id, QOSSetOutgoingDSCPValue, sizeof(DWORD), &dscp, 0,
36 nullptr);
37
38 m_success = true;
39 }
40 }
41
42 QoSSession::~QoSSession()
43 {
44 if (m_qos_handle == nullptr)
45 return;
46
47 if (m_qos_flow_id != 0)
48 QOSRemoveSocketFromFlow(m_qos_handle, m_peer->host->socket, m_qos_flow_id, 0);
49
50 QOSCloseHandle(m_qos_handle);
51 }
52 #else
53 QoSSession::QoSSession(ENetPeer* peer, int tos_val)
54 {
55 // Apple systems don't support SO_PRIORITY on BSD sockets, but they do support a flag for
56 // marking sockets as a specific type of traffic. `NET_SERVICE_TYPE_RV` should roughly
57 // correspond to "low delay tolerant, low-medium loss tolerant, elastic flow, variable
58 // packet interval, rate and size".
59 #if defined(__APPLE__)
60 constexpr int srv_type = NET_SERVICE_TYPE_RV;
61 setsockopt(peer->host->socket, SOL_SOCKET, SO_NET_SERVICE_TYPE, &srv_type, sizeof(srv_type));
62 #endif
63
64 #if defined(__linux__)
65 constexpr int priority = 7;
66 setsockopt(peer->host->socket, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority));
67 #endif
68
69 m_success = setsockopt(peer->host->socket, IPPROTO_IP, IP_TOS, &tos_val, sizeof(tos_val)) == 0;
70 }
71
72 QoSSession::~QoSSession() = default;
73 #endif
74
75 QoSSession& QoSSession::operator=(QoSSession&& session)
76 {
77 if (this != &session)
78 {
79 #if defined(_WIN32)
80 m_qos_handle = session.m_qos_handle;
81 m_qos_flow_id = session.m_qos_flow_id;
82 m_peer = session.m_peer;
83
84 session.m_qos_handle = nullptr;
85 session.m_qos_flow_id = 0;
86 session.m_peer = nullptr;
87 #endif
88 m_success = session.m_success;
89 session.m_success = false;
90 }
91
92 return *this;
93 }
94 } // namespace Common