Merge pull request #11791 from lioncash/traverse

Common/Traversal: Move interface into Common namespace
This commit is contained in:
Léo Lam 2023-04-25 01:05:41 +01:00 committed by GitHub
commit 77b334b47b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 127 additions and 105 deletions

View File

@ -12,6 +12,8 @@
#include "Common/Random.h" #include "Common/Random.h"
#include "Core/NetPlayProto.h" #include "Core/NetPlayProto.h"
namespace Common
{
TraversalClient::TraversalClient(ENetHost* netHost, const std::string& server, const u16 port) TraversalClient::TraversalClient(ENetHost* netHost, const std::string& server, const u16 port)
: m_NetHost(netHost), m_Server(server), m_port(port) : m_NetHost(netHost), m_Server(server), m_port(port)
{ {
@ -304,7 +306,7 @@ int ENET_CALLBACK TraversalClient::InterceptCallback(ENetHost* host, ENetEvent*
} }
std::unique_ptr<TraversalClient> g_TraversalClient; std::unique_ptr<TraversalClient> g_TraversalClient;
Common::ENet::ENetHostPtr g_MainNetHost; ENet::ENetHostPtr g_MainNetHost;
// The settings at the previous TraversalClient reset - notably, we // The settings at the previous TraversalClient reset - notably, we
// need to know not just what port it's on, but whether it was // need to know not just what port it's on, but whether it was
@ -348,3 +350,4 @@ void ReleaseTraversalClient()
g_TraversalClient.reset(); g_TraversalClient.reset();
g_MainNetHost.reset(); g_MainNetHost.reset();
} }
} // namespace Common

View File

@ -15,6 +15,8 @@
#include "Common/Thread.h" #include "Common/Thread.h"
#include "Common/TraversalProto.h" #include "Common/TraversalProto.h"
namespace Common
{
class TraversalClientClient class TraversalClientClient
{ {
public: public:
@ -90,9 +92,12 @@ private:
u16 m_port; u16 m_port;
u32 m_PingTime = 0; u32 m_PingTime = 0;
}; };
extern std::unique_ptr<TraversalClient> g_TraversalClient; extern std::unique_ptr<TraversalClient> g_TraversalClient;
// the NetHost connected to the TraversalClient. // the NetHost connected to the TraversalClient.
extern Common::ENet::ENetHostPtr g_MainNetHost; extern ENet::ENetHostPtr g_MainNetHost;
// Create g_TraversalClient and g_MainNetHost if necessary. // Create g_TraversalClient and g_MainNetHost if necessary.
bool EnsureTraversalClient(const std::string& server, u16 server_port, u16 listen_port = 0); bool EnsureTraversalClient(const std::string& server, u16 server_port, u16 listen_port = 0);
void ReleaseTraversalClient(); void ReleaseTraversalClient();
} // namespace Common

View File

@ -6,6 +6,8 @@
#include <cstddef> #include <cstddef>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
namespace Common
{
constexpr size_t NETPLAY_CODE_SIZE = 8; constexpr size_t NETPLAY_CODE_SIZE = 8;
using TraversalHostId = std::array<char, NETPLAY_CODE_SIZE>; using TraversalHostId = std::array<char, NETPLAY_CODE_SIZE>;
using TraversalRequestId = u64; using TraversalRequestId = u64;
@ -92,3 +94,4 @@ struct TraversalPacket
}; };
}; };
#pragma pack(pop) #pragma pack(pop)
} // namespace Common

View File

@ -31,8 +31,8 @@ static u64 currentTime;
struct OutgoingPacketInfo struct OutgoingPacketInfo
{ {
TraversalPacket packet; Common::TraversalPacket packet;
TraversalRequestId misc; Common::TraversalRequestId misc;
sockaddr_in6 dest; sockaddr_in6 dest;
int tries; int tries;
u64 sendTime; u64 sendTime;
@ -104,9 +104,9 @@ V* EvictSet(std::unordered_map<K, EvictEntry<V>>& map, const K& key)
namespace std namespace std
{ {
template <> template <>
struct hash<TraversalHostId> struct hash<Common::TraversalHostId>
{ {
size_t operator()(const TraversalHostId& id) const size_t operator()(const Common::TraversalHostId& id) const noexcept
{ {
auto p = (u32*)id.data(); auto p = (u32*)id.data();
return p[0] ^ ((p[1] << 13) | (p[1] >> 19)); return p[0] ^ ((p[1] << 13) | (p[1] >> 19));
@ -114,11 +114,15 @@ struct hash<TraversalHostId>
}; };
} // namespace std } // namespace std
static int sock; using ConnectedClients =
static std::unordered_map<TraversalRequestId, OutgoingPacketInfo> outgoingPackets; std::unordered_map<Common::TraversalHostId, EvictEntry<Common::TraversalInetAddress>>;
static std::unordered_map<TraversalHostId, EvictEntry<TraversalInetAddress>> connectedClients; using OutgoingPackets = std::unordered_map<Common::TraversalRequestId, OutgoingPacketInfo>;
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) if (addr.sin6_family != AF_INET6)
{ {
@ -126,7 +130,7 @@ static TraversalInetAddress MakeInetAddress(const sockaddr_in6& addr)
exit(1); exit(1);
} }
u32* words = (u32*)addr.sin6_addr.s6_addr; u32* words = (u32*)addr.sin6_addr.s6_addr;
TraversalInetAddress result = {0}; Common::TraversalInetAddress result = {};
if (words[0] == 0 && words[1] == 0 && words[2] == 0xffff0000) if (words[0] == 0 && words[1] == 0 && words[2] == 0xffff0000)
{ {
result.isIPV6 = false; result.isIPV6 = false;
@ -141,7 +145,7 @@ static TraversalInetAddress MakeInetAddress(const sockaddr_in6& addr)
return result; return result;
} }
static sockaddr_in6 MakeSinAddr(const TraversalInetAddress& addr) static sockaddr_in6 MakeSinAddr(const Common::TraversalInetAddress& addr)
{ {
sockaddr_in6 result; sockaddr_in6 result;
#ifdef SIN6_LEN #ifdef SIN6_LEN
@ -166,7 +170,7 @@ static sockaddr_in6 MakeSinAddr(const TraversalInetAddress& addr)
return result; return result;
} }
static void GetRandomHostId(TraversalHostId* hostId) static void GetRandomHostId(Common::TraversalHostId* hostId)
{ {
char buf[9]; char buf[9];
const u32 num = Common::Random::GenerateValue<u32>(); const u32 num = Common::Random::GenerateValue<u32>();
@ -185,7 +189,7 @@ static const char* SenderName(sockaddr_in6* addr)
static void TrySend(const void* buffer, size_t size, sockaddr_in6* addr) static void TrySend(const void* buffer, size_t size, sockaddr_in6* addr)
{ {
#if DEBUG #if DEBUG
const auto* packet = static_cast<const TraversalPacket*>(buffer); const auto* packet = static_cast<const Common::TraversalPacket*>(buffer);
printf("-> %d %llu %s\n", static_cast<int>(packet->type), printf("-> %d %llu %s\n", static_cast<int>(packet->type),
static_cast<long long>(packet->requestId), SenderName(addr)); static_cast<long long>(packet->requestId), SenderName(addr));
#endif #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)); Common::Random::Generate(&requestId, sizeof(requestId));
OutgoingPacketInfo* info = &outgoingPackets[requestId]; OutgoingPacketInfo* info = &outgoingPackets[requestId];
info->dest = dest; info->dest = dest;
info->misc = misc; info->misc = misc;
info->tries = 0; info->tries = 0;
info->sendTime = currentTime; info->sendTime = currentTime;
TraversalPacket* result = &info->packet; Common::TraversalPacket* result = &info->packet;
memset(result, 0, sizeof(*result)); memset(result, 0, sizeof(*result));
result->requestId = requestId; result->requestId = requestId;
return result; return result;
@ -219,7 +224,7 @@ static void SendPacket(OutgoingPacketInfo* info)
static void ResendPackets() static void ResendPackets()
{ {
std::vector<std::pair<TraversalInetAddress, TraversalRequestId>> todoFailures; std::vector<std::pair<Common::TraversalInetAddress, Common::TraversalRequestId>> todoFailures;
todoFailures.clear(); todoFailures.clear();
for (auto it = outgoingPackets.begin(); it != outgoingPackets.end();) for (auto it = outgoingPackets.begin(); it != outgoingPackets.end();)
{ {
@ -228,7 +233,7 @@ static void ResendPackets()
{ {
if (info->tries >= NUMBER_OF_TRIES) 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)); todoFailures.push_back(std::make_pair(info->packet.pleaseSendPacket.address, info->misc));
} }
@ -245,14 +250,14 @@ static void ResendPackets()
for (const auto& p : todoFailures) for (const auto& p : todoFailures)
{ {
TraversalPacket* fail = AllocPacket(MakeSinAddr(p.first)); Common::TraversalPacket* fail = AllocPacket(MakeSinAddr(p.first));
fail->type = TraversalPacketType::ConnectFailed; fail->type = Common::TraversalPacketType::ConnectFailed;
fail->connectFailed.requestId = p.second; 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 #if DEBUG
printf("<- %d %llu %s\n", static_cast<int>(packet->type), printf("<- %d %llu %s\n", static_cast<int>(packet->type),
@ -261,7 +266,7 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
bool packetOk = true; bool packetOk = true;
switch (packet->type) switch (packet->type)
{ {
case TraversalPacketType::Ack: case Common::TraversalPacketType::Ack:
{ {
auto it = outgoingPackets.find(packet->requestId); auto it = outgoingPackets.find(packet->requestId);
if (it == outgoingPackets.end()) if (it == outgoingPackets.end())
@ -269,42 +274,42 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
OutgoingPacketInfo* info = &it->second; 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) if (packet->ack.ok)
{ {
ready->type = TraversalPacketType::ConnectReady; ready->type = Common::TraversalPacketType::ConnectReady;
ready->connectReady.requestId = info->misc; ready->connectReady.requestId = info->misc;
ready->connectReady.address = MakeInetAddress(info->dest); ready->connectReady.address = MakeInetAddress(info->dest);
} }
else else
{ {
ready->type = TraversalPacketType::ConnectFailed; ready->type = Common::TraversalPacketType::ConnectFailed;
ready->connectFailed.requestId = info->misc; ready->connectFailed.requestId = info->misc;
ready->connectFailed.reason = TraversalConnectFailedReason::ClientFailure; ready->connectFailed.reason = Common::TraversalConnectFailedReason::ClientFailure;
} }
} }
outgoingPackets.erase(it); outgoingPackets.erase(it);
break; break;
} }
case TraversalPacketType::Ping: case Common::TraversalPacketType::Ping:
{ {
auto r = EvictFind(connectedClients, packet->ping.hostId, true); auto r = EvictFind(connectedClients, packet->ping.hostId, true);
packetOk = r.found; packetOk = r.found;
break; break;
} }
case TraversalPacketType::HelloFromClient: case Common::TraversalPacketType::HelloFromClient:
{ {
u8 ok = packet->helloFromClient.protoVersion <= TraversalProtoVersion; u8 ok = packet->helloFromClient.protoVersion <= Common::TraversalProtoVersion;
TraversalPacket* reply = AllocPacket(*addr); Common::TraversalPacket* reply = AllocPacket(*addr);
reply->type = TraversalPacketType::HelloFromServer; reply->type = Common::TraversalPacketType::HelloFromServer;
reply->helloFromServer.ok = ok; reply->helloFromServer.ok = ok;
if (ok) if (ok)
{ {
TraversalHostId hostId; Common::TraversalHostId hostId{};
TraversalInetAddress* iaddr; Common::TraversalInetAddress* iaddr{};
// not that there is any significant change of // not that there is any significant change of
// duplication, but... // duplication, but...
GetRandomHostId(&hostId); GetRandomHostId(&hostId);
@ -325,21 +330,21 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
} }
break; break;
} }
case TraversalPacketType::ConnectPlease: case Common::TraversalPacketType::ConnectPlease:
{ {
TraversalHostId& hostId = packet->connectPlease.hostId; Common::TraversalHostId& hostId = packet->connectPlease.hostId;
auto r = EvictFind(connectedClients, hostId); auto r = EvictFind(connectedClients, hostId);
if (!r.found) if (!r.found)
{ {
TraversalPacket* reply = AllocPacket(*addr); Common::TraversalPacket* reply = AllocPacket(*addr);
reply->type = TraversalPacketType::ConnectFailed; reply->type = Common::TraversalPacketType::ConnectFailed;
reply->connectFailed.requestId = packet->requestId; reply->connectFailed.requestId = packet->requestId;
reply->connectFailed.reason = TraversalConnectFailedReason::NoSuchClient; reply->connectFailed.reason = Common::TraversalConnectFailedReason::NoSuchClient;
} }
else else
{ {
TraversalPacket* please = AllocPacket(MakeSinAddr(*r.value), packet->requestId); Common::TraversalPacket* please = AllocPacket(MakeSinAddr(*r.value), packet->requestId);
please->type = TraversalPacketType::PleaseSendPacket; please->type = Common::TraversalPacketType::PleaseSendPacket;
please->pleaseSendPacket.address = MakeInetAddress(*addr); please->pleaseSendPacket.address = MakeInetAddress(*addr);
} }
break; break;
@ -349,10 +354,10 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
SenderName(addr)); SenderName(addr));
break; break;
} }
if (packet->type != TraversalPacketType::Ack) if (packet->type != Common::TraversalPacketType::Ack)
{ {
TraversalPacket ack = {}; Common::TraversalPacket ack = {};
ack.type = TraversalPacketType::Ack; ack.type = Common::TraversalPacketType::Ack;
ack.requestId = packet->requestId; ack.requestId = packet->requestId;
ack.ack.ok = packetOk; ack.ack.ok = packetOk;
TrySend(&ack, sizeof(ack), addr); TrySend(&ack, sizeof(ack), addr);
@ -411,7 +416,7 @@ int main()
{ {
sockaddr_in6 raddr; sockaddr_in6 raddr;
socklen_t addrLen = sizeof(raddr); socklen_t addrLen = sizeof(raddr);
TraversalPacket packet; Common::TraversalPacket packet{};
// note: switch to recvmmsg (yes, mmsg) if this becomes // note: switch to recvmmsg (yes, mmsg) if this becomes
// expensive // expensive
rv = recvfrom(sock, &packet, sizeof(packet), 0, (sockaddr*)&raddr, &addrLen); rv = recvfrom(sock, &packet, sizeof(packet), 0, (sockaddr*)&raddr, &addrLen);

View File

@ -110,9 +110,9 @@ NetPlayClient::~NetPlayClient()
Disconnect(); Disconnect();
} }
if (g_MainNetHost.get() == m_client) if (Common::g_MainNetHost.get() == m_client)
{ {
g_MainNetHost.release(); Common::g_MainNetHost.release();
} }
if (m_client) if (m_client)
{ {
@ -122,7 +122,7 @@ NetPlayClient::~NetPlayClient()
if (m_traversal_client) if (m_traversal_client)
{ {
ReleaseTraversalClient(); Common::ReleaseTraversalClient();
} }
} }
@ -179,18 +179,21 @@ NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlay
} }
else else
{ {
if (address.size() > NETPLAY_CODE_SIZE) if (address.size() > Common::NETPLAY_CODE_SIZE)
{ {
m_dialog->OnConnectionError( m_dialog->OnConnectionError(
_trans("The host code is too long.\nPlease recheck that you have the correct code.")); _trans("The host code is too long.\nPlease recheck that you have the correct code."));
return; return;
} }
if (!EnsureTraversalClient(traversal_config.traversal_host, traversal_config.traversal_port)) if (!Common::EnsureTraversalClient(traversal_config.traversal_host,
traversal_config.traversal_port))
{
return; return;
m_client = g_MainNetHost.get(); }
m_client = Common::g_MainNetHost.get();
m_traversal_client = g_TraversalClient.get(); m_traversal_client = Common::g_TraversalClient.get();
// If we were disconnected in the background, reconnect. // If we were disconnected in the background, reconnect.
if (m_traversal_client->HasFailed()) if (m_traversal_client->HasFailed())
@ -1936,16 +1939,16 @@ void NetPlayClient::ClearBuffers()
// called from ---NETPLAY--- thread // called from ---NETPLAY--- thread
void NetPlayClient::OnTraversalStateChanged() void NetPlayClient::OnTraversalStateChanged()
{ {
const TraversalClient::State state = m_traversal_client->GetState(); const Common::TraversalClient::State state = m_traversal_client->GetState();
if (m_connection_state == ConnectionState::WaitingForTraversalClientConnection && if (m_connection_state == ConnectionState::WaitingForTraversalClientConnection &&
state == TraversalClient::State::Connected) state == Common::TraversalClient::State::Connected)
{ {
m_connection_state = ConnectionState::WaitingForTraversalClientConnectReady; m_connection_state = ConnectionState::WaitingForTraversalClientConnectReady;
m_traversal_client->ConnectToClient(m_host_spec); m_traversal_client->ConnectToClient(m_host_spec);
} }
else if (m_connection_state != ConnectionState::Failure && else if (m_connection_state != ConnectionState::Failure &&
state == TraversalClient::State::Failure) state == Common::TraversalClient::State::Failure)
{ {
Disconnect(); Disconnect();
m_dialog->OnTraversalError(m_traversal_client->GetFailureReason()); m_dialog->OnTraversalError(m_traversal_client->GetFailureReason());
@ -1964,19 +1967,19 @@ void NetPlayClient::OnConnectReady(ENetAddress addr)
} }
// called from ---NETPLAY--- thread // called from ---NETPLAY--- thread
void NetPlayClient::OnConnectFailed(TraversalConnectFailedReason reason) void NetPlayClient::OnConnectFailed(Common::TraversalConnectFailedReason reason)
{ {
m_connecting = false; m_connecting = false;
m_connection_state = ConnectionState::Failure; m_connection_state = ConnectionState::Failure;
switch (reason) switch (reason)
{ {
case TraversalConnectFailedReason::ClientDidntRespond: case Common::TraversalConnectFailedReason::ClientDidntRespond:
PanicAlertFmtT("Traversal server timed out connecting to the host"); PanicAlertFmtT("Traversal server timed out connecting to the host");
break; break;
case TraversalConnectFailedReason::ClientFailure: case Common::TraversalConnectFailedReason::ClientFailure:
PanicAlertFmtT("Server rejected traversal attempt"); PanicAlertFmtT("Server rejected traversal attempt");
break; break;
case TraversalConnectFailedReason::NoSuchClient: case Common::TraversalConnectFailedReason::NoSuchClient:
PanicAlertFmtT("Invalid host"); PanicAlertFmtT("Invalid host");
break; break;
default: default:

View File

@ -68,8 +68,8 @@ public:
virtual void OnDesync(u32 frame, const std::string& player) = 0; virtual void OnDesync(u32 frame, const std::string& player) = 0;
virtual void OnConnectionLost() = 0; virtual void OnConnectionLost() = 0;
virtual void OnConnectionError(const std::string& message) = 0; virtual void OnConnectionError(const std::string& message) = 0;
virtual void OnTraversalError(TraversalClient::FailureReason error) = 0; virtual void OnTraversalError(Common::TraversalClient::FailureReason error) = 0;
virtual void OnTraversalStateChanged(TraversalClient::State state) = 0; virtual void OnTraversalStateChanged(Common::TraversalClient::State state) = 0;
virtual void OnGameStartAborted() = 0; virtual void OnGameStartAborted() = 0;
virtual void OnGolferChanged(bool is_golfer, const std::string& golfer_name) = 0; virtual void OnGolferChanged(bool is_golfer, const std::string& golfer_name) = 0;
@ -107,7 +107,7 @@ public:
bool IsHost() const { return pid == 1; } bool IsHost() const { return pid == 1; }
}; };
class NetPlayClient : public TraversalClientClient class NetPlayClient : public Common::TraversalClientClient
{ {
public: public:
void ThreadFunc(); void ThreadFunc();
@ -147,7 +147,7 @@ public:
void OnTraversalStateChanged() override; void OnTraversalStateChanged() override;
void OnConnectReady(ENetAddress addr) override; void OnConnectReady(ENetAddress addr) override;
void OnConnectFailed(TraversalConnectFailedReason reason) override; void OnConnectFailed(Common::TraversalConnectFailedReason reason) override;
bool IsFirstInGamePad(int ingame_pad) const; bool IsFirstInGamePad(int ingame_pad) const;
int NumLocalPads() const; int NumLocalPads() const;
@ -325,7 +325,7 @@ private:
std::string m_host_spec; std::string m_host_spec;
std::string m_player_name; std::string m_player_name;
bool m_connecting = false; bool m_connecting = false;
TraversalClient* m_traversal_client = nullptr; Common::TraversalClient* m_traversal_client = nullptr;
std::thread m_game_digest_thread; std::thread m_game_digest_thread;
bool m_should_compute_game_digest = false; bool m_should_compute_game_digest = false;
Common::Event m_gc_pad_event; Common::Event m_gc_pad_event;

View File

@ -98,15 +98,15 @@ NetPlayServer::~NetPlayServer()
m_thread.join(); m_thread.join();
enet_host_destroy(m_server); enet_host_destroy(m_server);
if (g_MainNetHost.get() == m_server) if (Common::g_MainNetHost.get() == m_server)
{ {
g_MainNetHost.release(); Common::g_MainNetHost.release();
} }
if (m_traversal_client) if (m_traversal_client)
{ {
g_TraversalClient->m_Client = nullptr; Common::g_TraversalClient->m_Client = nullptr;
ReleaseTraversalClient(); Common::ReleaseTraversalClient();
} }
} }
@ -132,17 +132,19 @@ NetPlayServer::NetPlayServer(const u16 port, const bool forward_port, NetPlayUI*
if (traversal_config.use_traversal) if (traversal_config.use_traversal)
{ {
if (!EnsureTraversalClient(traversal_config.traversal_host, traversal_config.traversal_port, if (!Common::EnsureTraversalClient(traversal_config.traversal_host,
port)) traversal_config.traversal_port, port))
{
return; return;
}
g_TraversalClient->m_Client = this; Common::g_TraversalClient->m_Client = this;
m_traversal_client = g_TraversalClient.get(); m_traversal_client = Common::g_TraversalClient.get();
m_server = g_MainNetHost.get(); m_server = Common::g_MainNetHost.get();
if (g_TraversalClient->HasFailed()) if (Common::g_TraversalClient->HasFailed())
g_TraversalClient->ReconnectToServer(); Common::g_TraversalClient->ReconnectToServer();
} }
else else
{ {
@ -211,7 +213,7 @@ void NetPlayServer::SetupIndex()
if (!m_traversal_client->IsConnected()) if (!m_traversal_client->IsConnected())
return; return;
session.server_id = std::string(g_TraversalClient->GetHostID().data(), 8); session.server_id = std::string(Common::g_TraversalClient->GetHostID().data(), 8);
} }
else else
{ {
@ -1248,15 +1250,15 @@ unsigned int NetPlayServer::OnData(sf::Packet& packet, Client& player)
void NetPlayServer::OnTraversalStateChanged() void NetPlayServer::OnTraversalStateChanged()
{ {
const TraversalClient::State state = m_traversal_client->GetState(); const Common::TraversalClient::State state = m_traversal_client->GetState();
if (g_TraversalClient->GetHostID()[0] != '\0') if (Common::g_TraversalClient->GetHostID()[0] != '\0')
SetupIndex(); SetupIndex();
if (!m_dialog) if (!m_dialog)
return; return;
if (state == TraversalClient::State::Failure) if (state == Common::TraversalClient::State::Failure)
m_dialog->OnTraversalError(m_traversal_client->GetFailureReason()); m_dialog->OnTraversalError(m_traversal_client->GetFailureReason());
m_dialog->OnTraversalStateChanged(state); m_dialog->OnTraversalStateChanged(state);

View File

@ -31,7 +31,7 @@ namespace NetPlay
class NetPlayUI; class NetPlayUI;
struct SaveSyncInfo; struct SaveSyncInfo;
class NetPlayServer : public TraversalClientClient class NetPlayServer : public Common::TraversalClientClient
{ {
public: public:
void ThreadFunc(); void ThreadFunc();
@ -143,7 +143,7 @@ private:
void OnTraversalStateChanged() override; void OnTraversalStateChanged() override;
void OnConnectReady(ENetAddress) override {} void OnConnectReady(ENetAddress) override {}
void OnConnectFailed(TraversalConnectFailedReason) override {} void OnConnectFailed(Common::TraversalConnectFailedReason) override {}
void UpdatePadMapping(); void UpdatePadMapping();
void UpdateGBAConfig(); void UpdateGBAConfig();
void UpdateWiimoteMapping(); void UpdateWiimoteMapping();
@ -210,7 +210,7 @@ private:
bool m_abort_chunked_data = false; bool m_abort_chunked_data = false;
ENetHost* m_server = nullptr; ENetHost* m_server = nullptr;
TraversalClient* m_traversal_client = nullptr; Common::TraversalClient* m_traversal_client = nullptr;
NetPlayUI* m_dialog = nullptr; NetPlayUI* m_dialog = nullptr;
NetPlayIndex m_index; NetPlayIndex m_index;
}; };

View File

@ -65,7 +65,7 @@
namespace namespace
{ {
QString InetAddressToString(const TraversalInetAddress& addr) QString InetAddressToString(const Common::TraversalInetAddress& addr)
{ {
QString ip; QString ip;
@ -321,7 +321,7 @@ void NetPlayDialog::ConnectWidgets()
&NetPlayDialog::UpdateGUI); &NetPlayDialog::UpdateGUI);
connect(m_hostcode_action_button, &QPushButton::clicked, [this] { connect(m_hostcode_action_button, &QPushButton::clicked, [this] {
if (m_is_copy_button_retry) if (m_is_copy_button_retry)
g_TraversalClient->ReconnectToServer(); Common::g_TraversalClient->ReconnectToServer();
else else
QApplication::clipboard()->setText(m_hostcode_label->text()); QApplication::clipboard()->setText(m_hostcode_label->text());
}); });
@ -581,9 +581,9 @@ void NetPlayDialog::UpdateDiscordPresence()
if (IsHosting()) if (IsHosting())
{ {
if (g_TraversalClient) if (Common::g_TraversalClient)
{ {
const auto host_id = g_TraversalClient->GetHostID(); const auto host_id = Common::g_TraversalClient->GetHostID();
if (host_id[0] == '\0') if (host_id[0] == '\0')
return use_default(); return use_default();
@ -706,26 +706,27 @@ void NetPlayDialog::UpdateGUI()
} }
else if (m_use_traversal) else if (m_use_traversal)
{ {
switch (g_TraversalClient->GetState()) switch (Common::g_TraversalClient->GetState())
{ {
case TraversalClient::State::Connecting: case Common::TraversalClient::State::Connecting:
m_hostcode_label->setText(tr("Connecting")); m_hostcode_label->setText(tr("Connecting"));
m_hostcode_action_button->setEnabled(false); m_hostcode_action_button->setEnabled(false);
m_hostcode_action_button->setText(tr("...")); m_hostcode_action_button->setText(tr("..."));
break; break;
case TraversalClient::State::Connected: case Common::TraversalClient::State::Connected:
{ {
if (m_room_box->currentIndex() == 0) if (m_room_box->currentIndex() == 0)
{ {
// Display Room ID. // Display Room ID.
const auto host_id = g_TraversalClient->GetHostID(); const auto host_id = Common::g_TraversalClient->GetHostID();
m_hostcode_label->setText( m_hostcode_label->setText(
QString::fromStdString(std::string(host_id.begin(), host_id.end()))); QString::fromStdString(std::string(host_id.begin(), host_id.end())));
} }
else else
{ {
// Externally mapped IP and port are known when using the traversal server. // Externally mapped IP and port are known when using the traversal server.
m_hostcode_label->setText(InetAddressToString(g_TraversalClient->GetExternalAddress())); m_hostcode_label->setText(
InetAddressToString(Common::g_TraversalClient->GetExternalAddress()));
} }
m_hostcode_action_button->setEnabled(true); m_hostcode_action_button->setEnabled(true);
@ -733,7 +734,7 @@ void NetPlayDialog::UpdateGUI()
m_is_copy_button_retry = false; m_is_copy_button_retry = false;
break; break;
} }
case TraversalClient::State::Failure: case Common::TraversalClient::State::Failure:
m_hostcode_label->setText(tr("Error")); m_hostcode_label->setText(tr("Error"));
m_hostcode_action_button->setText(tr("Retry")); m_hostcode_action_button->setText(tr("Retry"));
m_hostcode_action_button->setEnabled(true); m_hostcode_action_button->setEnabled(true);
@ -981,35 +982,35 @@ void NetPlayDialog::OnConnectionError(const std::string& message)
}); });
} }
void NetPlayDialog::OnTraversalError(TraversalClient::FailureReason error) void NetPlayDialog::OnTraversalError(Common::TraversalClient::FailureReason error)
{ {
QueueOnObject(this, [this, error] { QueueOnObject(this, [this, error] {
switch (error) switch (error)
{ {
case TraversalClient::FailureReason::BadHost: case Common::TraversalClient::FailureReason::BadHost:
ModalMessageBox::critical(this, tr("Traversal Error"), tr("Couldn't look up central server")); ModalMessageBox::critical(this, tr("Traversal Error"), tr("Couldn't look up central server"));
QDialog::reject(); QDialog::reject();
break; break;
case TraversalClient::FailureReason::VersionTooOld: case Common::TraversalClient::FailureReason::VersionTooOld:
ModalMessageBox::critical(this, tr("Traversal Error"), ModalMessageBox::critical(this, tr("Traversal Error"),
tr("Dolphin is too old for traversal server")); tr("Dolphin is too old for traversal server"));
QDialog::reject(); QDialog::reject();
break; break;
case TraversalClient::FailureReason::ServerForgotAboutUs: case Common::TraversalClient::FailureReason::ServerForgotAboutUs:
case TraversalClient::FailureReason::SocketSendError: case Common::TraversalClient::FailureReason::SocketSendError:
case TraversalClient::FailureReason::ResendTimeout: case Common::TraversalClient::FailureReason::ResendTimeout:
UpdateGUI(); UpdateGUI();
break; break;
} }
}); });
} }
void NetPlayDialog::OnTraversalStateChanged(TraversalClient::State state) void NetPlayDialog::OnTraversalStateChanged(Common::TraversalClient::State state)
{ {
switch (state) switch (state)
{ {
case TraversalClient::State::Connected: case Common::TraversalClient::State::Connected:
case TraversalClient::State::Failure: case Common::TraversalClient::State::Failure:
UpdateDiscordPresence(); UpdateDiscordPresence();
break; break;
default: default:

View File

@ -67,8 +67,8 @@ public:
void OnDesync(u32 frame, const std::string& player) override; void OnDesync(u32 frame, const std::string& player) override;
void OnConnectionLost() override; void OnConnectionLost() override;
void OnConnectionError(const std::string& message) override; void OnConnectionError(const std::string& message) override;
void OnTraversalError(TraversalClient::FailureReason error) override; void OnTraversalError(Common::TraversalClient::FailureReason error) override;
void OnTraversalStateChanged(TraversalClient::State state) override; void OnTraversalStateChanged(Common::TraversalClient::State state) override;
void OnGameStartAborted() override; void OnGameStartAborted() override;
void OnGolferChanged(bool is_golfer, const std::string& golfer_name) override; void OnGolferChanged(bool is_golfer, const std::string& golfer_name) override;