From 30e7ab94fa8d4177a8516a926dad5a7e663a1a51 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 24 Apr 2023 08:29:12 -0400 Subject: [PATCH] Common/TraversalProto: Move interface into Common namespace Gets the types out of the global namespace. --- Source/Core/Common/TraversalProto.h | 3 + Source/Core/Common/TraversalServer.cpp | 95 ++++++++++--------- Source/Core/Core/NetPlayClient.cpp | 10 +- Source/Core/Core/NetPlayClient.h | 2 +- Source/Core/Core/NetPlayServer.h | 2 +- .../Core/DolphinQt/NetPlay/NetPlayDialog.cpp | 2 +- 6 files changed, 61 insertions(+), 53 deletions(-) diff --git a/Source/Core/Common/TraversalProto.h b/Source/Core/Common/TraversalProto.h index fc7404048a..1946d7b9a9 100644 --- a/Source/Core/Common/TraversalProto.h +++ b/Source/Core/Common/TraversalProto.h @@ -6,6 +6,8 @@ #include #include "Common/CommonTypes.h" +namespace Common +{ constexpr size_t NETPLAY_CODE_SIZE = 8; using TraversalHostId = std::array; using TraversalRequestId = u64; @@ -92,3 +94,4 @@ struct TraversalPacket }; }; #pragma pack(pop) +} // namespace Common diff --git a/Source/Core/Common/TraversalServer.cpp b/Source/Core/Common/TraversalServer.cpp index e23c7676bd..b9b05519fa 100644 --- a/Source/Core/Common/TraversalServer.cpp +++ b/Source/Core/Common/TraversalServer.cpp @@ -31,8 +31,8 @@ static u64 currentTime; struct OutgoingPacketInfo { - TraversalPacket packet; - TraversalRequestId misc; + Common::TraversalPacket packet; + Common::TraversalRequestId misc; sockaddr_in6 dest; int tries; u64 sendTime; @@ -104,9 +104,9 @@ V* EvictSet(std::unordered_map>& map, const K& key) namespace std { template <> -struct hash +struct hash { - size_t operator()(const TraversalHostId& id) const + size_t operator()(const Common::TraversalHostId& id) const noexcept { auto p = (u32*)id.data(); return p[0] ^ ((p[1] << 13) | (p[1] >> 19)); @@ -114,11 +114,15 @@ struct hash }; } // namespace std -static int sock; -static std::unordered_map outgoingPackets; -static std::unordered_map> connectedClients; +using ConnectedClients = + std::unordered_map>; +using OutgoingPackets = std::unordered_map; -static TraversalInetAddress MakeInetAddress(const sockaddr_in6& addr) +static int sock; +static OutgoingPackets outgoingPackets; +static ConnectedClients connectedClients; + +static Common::TraversalInetAddress MakeInetAddress(const sockaddr_in6& addr) { if (addr.sin6_family != AF_INET6) { @@ -126,7 +130,7 @@ static TraversalInetAddress MakeInetAddress(const sockaddr_in6& addr) exit(1); } u32* words = (u32*)addr.sin6_addr.s6_addr; - TraversalInetAddress result = {0}; + Common::TraversalInetAddress result = {}; if (words[0] == 0 && words[1] == 0 && words[2] == 0xffff0000) { result.isIPV6 = false; @@ -141,7 +145,7 @@ static TraversalInetAddress MakeInetAddress(const sockaddr_in6& addr) return result; } -static sockaddr_in6 MakeSinAddr(const TraversalInetAddress& addr) +static sockaddr_in6 MakeSinAddr(const Common::TraversalInetAddress& addr) { sockaddr_in6 result; #ifdef SIN6_LEN @@ -166,7 +170,7 @@ static sockaddr_in6 MakeSinAddr(const TraversalInetAddress& addr) return result; } -static void GetRandomHostId(TraversalHostId* hostId) +static void GetRandomHostId(Common::TraversalHostId* hostId) { char buf[9]; const u32 num = Common::Random::GenerateValue(); @@ -185,7 +189,7 @@ static const char* SenderName(sockaddr_in6* addr) static void TrySend(const void* buffer, size_t size, sockaddr_in6* addr) { #if DEBUG - const auto* packet = static_cast(buffer); + const auto* packet = static_cast(buffer); printf("-> %d %llu %s\n", static_cast(packet->type), static_cast(packet->requestId), SenderName(addr)); #endif @@ -195,16 +199,17 @@ static void TrySend(const void* buffer, size_t size, sockaddr_in6* addr) } } -static TraversalPacket* AllocPacket(const sockaddr_in6& dest, TraversalRequestId misc = 0) +static Common::TraversalPacket* AllocPacket(const sockaddr_in6& dest, + Common::TraversalRequestId misc = 0) { - TraversalRequestId requestId; + Common::TraversalRequestId requestId{}; Common::Random::Generate(&requestId, sizeof(requestId)); OutgoingPacketInfo* info = &outgoingPackets[requestId]; info->dest = dest; info->misc = misc; info->tries = 0; info->sendTime = currentTime; - TraversalPacket* result = &info->packet; + Common::TraversalPacket* result = &info->packet; memset(result, 0, sizeof(*result)); result->requestId = requestId; return result; @@ -219,7 +224,7 @@ static void SendPacket(OutgoingPacketInfo* info) static void ResendPackets() { - std::vector> todoFailures; + std::vector> todoFailures; todoFailures.clear(); for (auto it = outgoingPackets.begin(); it != outgoingPackets.end();) { @@ -228,7 +233,7 @@ static void ResendPackets() { if (info->tries >= NUMBER_OF_TRIES) { - if (info->packet.type == TraversalPacketType::PleaseSendPacket) + if (info->packet.type == Common::TraversalPacketType::PleaseSendPacket) { todoFailures.push_back(std::make_pair(info->packet.pleaseSendPacket.address, info->misc)); } @@ -245,14 +250,14 @@ static void ResendPackets() for (const auto& p : todoFailures) { - TraversalPacket* fail = AllocPacket(MakeSinAddr(p.first)); - fail->type = TraversalPacketType::ConnectFailed; + Common::TraversalPacket* fail = AllocPacket(MakeSinAddr(p.first)); + fail->type = Common::TraversalPacketType::ConnectFailed; fail->connectFailed.requestId = p.second; - fail->connectFailed.reason = TraversalConnectFailedReason::ClientDidntRespond; + fail->connectFailed.reason = Common::TraversalConnectFailedReason::ClientDidntRespond; } } -static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr) +static void HandlePacket(Common::TraversalPacket* packet, sockaddr_in6* addr) { #if DEBUG printf("<- %d %llu %s\n", static_cast(packet->type), @@ -261,7 +266,7 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr) bool packetOk = true; switch (packet->type) { - case TraversalPacketType::Ack: + case Common::TraversalPacketType::Ack: { auto it = outgoingPackets.find(packet->requestId); if (it == outgoingPackets.end()) @@ -269,42 +274,42 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr) OutgoingPacketInfo* info = &it->second; - if (info->packet.type == TraversalPacketType::PleaseSendPacket) + if (info->packet.type == Common::TraversalPacketType::PleaseSendPacket) { - TraversalPacket* ready = AllocPacket(MakeSinAddr(info->packet.pleaseSendPacket.address)); + auto* ready = AllocPacket(MakeSinAddr(info->packet.pleaseSendPacket.address)); if (packet->ack.ok) { - ready->type = TraversalPacketType::ConnectReady; + ready->type = Common::TraversalPacketType::ConnectReady; ready->connectReady.requestId = info->misc; ready->connectReady.address = MakeInetAddress(info->dest); } else { - ready->type = TraversalPacketType::ConnectFailed; + ready->type = Common::TraversalPacketType::ConnectFailed; ready->connectFailed.requestId = info->misc; - ready->connectFailed.reason = TraversalConnectFailedReason::ClientFailure; + ready->connectFailed.reason = Common::TraversalConnectFailedReason::ClientFailure; } } outgoingPackets.erase(it); break; } - case TraversalPacketType::Ping: + case Common::TraversalPacketType::Ping: { auto r = EvictFind(connectedClients, packet->ping.hostId, true); packetOk = r.found; break; } - case TraversalPacketType::HelloFromClient: + case Common::TraversalPacketType::HelloFromClient: { - u8 ok = packet->helloFromClient.protoVersion <= TraversalProtoVersion; - TraversalPacket* reply = AllocPacket(*addr); - reply->type = TraversalPacketType::HelloFromServer; + u8 ok = packet->helloFromClient.protoVersion <= Common::TraversalProtoVersion; + Common::TraversalPacket* reply = AllocPacket(*addr); + reply->type = Common::TraversalPacketType::HelloFromServer; reply->helloFromServer.ok = ok; if (ok) { - TraversalHostId hostId; - TraversalInetAddress* iaddr; + Common::TraversalHostId hostId{}; + Common::TraversalInetAddress* iaddr{}; // not that there is any significant change of // duplication, but... GetRandomHostId(&hostId); @@ -325,21 +330,21 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr) } break; } - case TraversalPacketType::ConnectPlease: + case Common::TraversalPacketType::ConnectPlease: { - TraversalHostId& hostId = packet->connectPlease.hostId; + Common::TraversalHostId& hostId = packet->connectPlease.hostId; auto r = EvictFind(connectedClients, hostId); if (!r.found) { - TraversalPacket* reply = AllocPacket(*addr); - reply->type = TraversalPacketType::ConnectFailed; + Common::TraversalPacket* reply = AllocPacket(*addr); + reply->type = Common::TraversalPacketType::ConnectFailed; reply->connectFailed.requestId = packet->requestId; - reply->connectFailed.reason = TraversalConnectFailedReason::NoSuchClient; + reply->connectFailed.reason = Common::TraversalConnectFailedReason::NoSuchClient; } else { - TraversalPacket* please = AllocPacket(MakeSinAddr(*r.value), packet->requestId); - please->type = TraversalPacketType::PleaseSendPacket; + Common::TraversalPacket* please = AllocPacket(MakeSinAddr(*r.value), packet->requestId); + please->type = Common::TraversalPacketType::PleaseSendPacket; please->pleaseSendPacket.address = MakeInetAddress(*addr); } break; @@ -349,10 +354,10 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr) SenderName(addr)); break; } - if (packet->type != TraversalPacketType::Ack) + if (packet->type != Common::TraversalPacketType::Ack) { - TraversalPacket ack = {}; - ack.type = TraversalPacketType::Ack; + Common::TraversalPacket ack = {}; + ack.type = Common::TraversalPacketType::Ack; ack.requestId = packet->requestId; ack.ack.ok = packetOk; TrySend(&ack, sizeof(ack), addr); @@ -411,7 +416,7 @@ int main() { sockaddr_in6 raddr; socklen_t addrLen = sizeof(raddr); - TraversalPacket packet; + Common::TraversalPacket packet{}; // note: switch to recvmmsg (yes, mmsg) if this becomes // expensive rv = recvfrom(sock, &packet, sizeof(packet), 0, (sockaddr*)&raddr, &addrLen); diff --git a/Source/Core/Core/NetPlayClient.cpp b/Source/Core/Core/NetPlayClient.cpp index 15f6e9e3ed..ff78728c80 100644 --- a/Source/Core/Core/NetPlayClient.cpp +++ b/Source/Core/Core/NetPlayClient.cpp @@ -179,7 +179,7 @@ NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlay } else { - if (address.size() > NETPLAY_CODE_SIZE) + if (address.size() > Common::NETPLAY_CODE_SIZE) { m_dialog->OnConnectionError( _trans("The host code is too long.\nPlease recheck that you have the correct code.")); @@ -1967,19 +1967,19 @@ void NetPlayClient::OnConnectReady(ENetAddress addr) } // called from ---NETPLAY--- thread -void NetPlayClient::OnConnectFailed(TraversalConnectFailedReason reason) +void NetPlayClient::OnConnectFailed(Common::TraversalConnectFailedReason reason) { m_connecting = false; m_connection_state = ConnectionState::Failure; switch (reason) { - case TraversalConnectFailedReason::ClientDidntRespond: + case Common::TraversalConnectFailedReason::ClientDidntRespond: PanicAlertFmtT("Traversal server timed out connecting to the host"); break; - case TraversalConnectFailedReason::ClientFailure: + case Common::TraversalConnectFailedReason::ClientFailure: PanicAlertFmtT("Server rejected traversal attempt"); break; - case TraversalConnectFailedReason::NoSuchClient: + case Common::TraversalConnectFailedReason::NoSuchClient: PanicAlertFmtT("Invalid host"); break; default: diff --git a/Source/Core/Core/NetPlayClient.h b/Source/Core/Core/NetPlayClient.h index abff9c15b1..aba54b12dd 100644 --- a/Source/Core/Core/NetPlayClient.h +++ b/Source/Core/Core/NetPlayClient.h @@ -147,7 +147,7 @@ public: void OnTraversalStateChanged() override; void OnConnectReady(ENetAddress addr) override; - void OnConnectFailed(TraversalConnectFailedReason reason) override; + void OnConnectFailed(Common::TraversalConnectFailedReason reason) override; bool IsFirstInGamePad(int ingame_pad) const; int NumLocalPads() const; diff --git a/Source/Core/Core/NetPlayServer.h b/Source/Core/Core/NetPlayServer.h index d962205a5d..0498ff12fc 100644 --- a/Source/Core/Core/NetPlayServer.h +++ b/Source/Core/Core/NetPlayServer.h @@ -143,7 +143,7 @@ private: void OnTraversalStateChanged() override; void OnConnectReady(ENetAddress) override {} - void OnConnectFailed(TraversalConnectFailedReason) override {} + void OnConnectFailed(Common::TraversalConnectFailedReason) override {} void UpdatePadMapping(); void UpdateGBAConfig(); void UpdateWiimoteMapping(); diff --git a/Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp b/Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp index 2809be1d73..c5edb9c0d9 100644 --- a/Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp +++ b/Source/Core/DolphinQt/NetPlay/NetPlayDialog.cpp @@ -65,7 +65,7 @@ namespace { -QString InetAddressToString(const TraversalInetAddress& addr) +QString InetAddressToString(const Common::TraversalInetAddress& addr) { QString ip;