Common/TraversalProto: Move interface into Common namespace

Gets the types out of the global namespace.
This commit is contained in:
Lioncash 2023-04-24 08:29:12 -04:00
parent 27f38c6c8f
commit 30e7ab94fa
6 changed files with 61 additions and 53 deletions

View File

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

View File

@ -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<K, EvictEntry<V>>& map, const K& key)
namespace std
{
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();
return p[0] ^ ((p[1] << 13) | (p[1] >> 19));
@ -114,11 +114,15 @@ struct hash<TraversalHostId>
};
} // namespace std
static int sock;
static std::unordered_map<TraversalRequestId, OutgoingPacketInfo> outgoingPackets;
static std::unordered_map<TraversalHostId, EvictEntry<TraversalInetAddress>> connectedClients;
using ConnectedClients =
std::unordered_map<Common::TraversalHostId, EvictEntry<Common::TraversalInetAddress>>;
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)
{
@ -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<u32>();
@ -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<const TraversalPacket*>(buffer);
const auto* packet = static_cast<const Common::TraversalPacket*>(buffer);
printf("-> %d %llu %s\n", static_cast<int>(packet->type),
static_cast<long long>(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<std::pair<TraversalInetAddress, TraversalRequestId>> todoFailures;
std::vector<std::pair<Common::TraversalInetAddress, Common::TraversalRequestId>> 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<int>(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);

View File

@ -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:

View File

@ -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;

View File

@ -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();

View File

@ -65,7 +65,7 @@
namespace
{
QString InetAddressToString(const TraversalInetAddress& addr)
QString InetAddressToString(const Common::TraversalInetAddress& addr)
{
QString ip;