mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-01-25 07:21:14 +01:00
TraversalProto: Convert TraversalPacketType into an enum class
Makes for strong typing and prevents namespace pollution.
This commit is contained in:
parent
2f0fb2056e
commit
d2ea94195c
@ -51,7 +51,7 @@ void TraversalClient::ReconnectToServer()
|
|||||||
m_State = State::Connecting;
|
m_State = State::Connecting;
|
||||||
|
|
||||||
TraversalPacket hello = {};
|
TraversalPacket hello = {};
|
||||||
hello.type = TraversalPacketHelloFromClient;
|
hello.type = TraversalPacketType::HelloFromClient;
|
||||||
hello.helloFromClient.protoVersion = TraversalProtoVersion;
|
hello.helloFromClient.protoVersion = TraversalProtoVersion;
|
||||||
SendTraversalPacket(hello);
|
SendTraversalPacket(hello);
|
||||||
if (m_Client)
|
if (m_Client)
|
||||||
@ -81,7 +81,7 @@ void TraversalClient::ConnectToClient(const std::string& host)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
TraversalPacket packet = {};
|
TraversalPacket packet = {};
|
||||||
packet.type = TraversalPacketConnectPlease;
|
packet.type = TraversalPacketType::ConnectPlease;
|
||||||
memcpy(packet.connectPlease.hostId.data(), host.c_str(), host.size());
|
memcpy(packet.connectPlease.hostId.data(), host.c_str(), host.size());
|
||||||
m_ConnectRequestId = SendTraversalPacket(packet);
|
m_ConnectRequestId = SendTraversalPacket(packet);
|
||||||
m_PendingConnect = true;
|
m_PendingConnect = true;
|
||||||
@ -129,7 +129,7 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
|
|||||||
u8 ok = 1;
|
u8 ok = 1;
|
||||||
switch (packet->type)
|
switch (packet->type)
|
||||||
{
|
{
|
||||||
case TraversalPacketAck:
|
case TraversalPacketType::Ack:
|
||||||
if (!packet->ack.ok)
|
if (!packet->ack.ok)
|
||||||
{
|
{
|
||||||
OnFailure(FailureReason::ServerForgotAboutUs);
|
OnFailure(FailureReason::ServerForgotAboutUs);
|
||||||
@ -144,7 +144,7 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TraversalPacketHelloFromServer:
|
case TraversalPacketType::HelloFromServer:
|
||||||
if (!IsConnecting())
|
if (!IsConnecting())
|
||||||
break;
|
break;
|
||||||
if (!packet->helloFromServer.ok)
|
if (!packet->helloFromServer.ok)
|
||||||
@ -157,7 +157,7 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
|
|||||||
if (m_Client)
|
if (m_Client)
|
||||||
m_Client->OnTraversalStateChanged();
|
m_Client->OnTraversalStateChanged();
|
||||||
break;
|
break;
|
||||||
case TraversalPacketPleaseSendPacket:
|
case TraversalPacketType::PleaseSendPacket:
|
||||||
{
|
{
|
||||||
// security is overrated.
|
// security is overrated.
|
||||||
ENetAddress addr = MakeENetAddress(&packet->pleaseSendPacket.address);
|
ENetAddress addr = MakeENetAddress(&packet->pleaseSendPacket.address);
|
||||||
@ -176,8 +176,8 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TraversalPacketConnectReady:
|
case TraversalPacketType::ConnectReady:
|
||||||
case TraversalPacketConnectFailed:
|
case TraversalPacketType::ConnectFailed:
|
||||||
{
|
{
|
||||||
if (!m_PendingConnect || packet->connectReady.requestId != m_ConnectRequestId)
|
if (!m_PendingConnect || packet->connectReady.requestId != m_ConnectRequestId)
|
||||||
break;
|
break;
|
||||||
@ -187,7 +187,7 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
|
|||||||
if (!m_Client)
|
if (!m_Client)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (packet->type == TraversalPacketConnectReady)
|
if (packet->type == TraversalPacketType::ConnectReady)
|
||||||
m_Client->OnConnectReady(MakeENetAddress(&packet->connectReady.address));
|
m_Client->OnConnectReady(MakeENetAddress(&packet->connectReady.address));
|
||||||
else
|
else
|
||||||
m_Client->OnConnectFailed(packet->connectFailed.reason);
|
m_Client->OnConnectFailed(packet->connectFailed.reason);
|
||||||
@ -197,10 +197,10 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
|
|||||||
WARN_LOG_FMT(NETPLAY, "Received unknown packet with type {}", packet->type);
|
WARN_LOG_FMT(NETPLAY, "Received unknown packet with type {}", packet->type);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (packet->type != TraversalPacketAck)
|
if (packet->type != TraversalPacketType::Ack)
|
||||||
{
|
{
|
||||||
TraversalPacket ack = {};
|
TraversalPacket ack = {};
|
||||||
ack.type = TraversalPacketAck;
|
ack.type = TraversalPacketType::Ack;
|
||||||
ack.requestId = packet->requestId;
|
ack.requestId = packet->requestId;
|
||||||
ack.ack.ok = ok;
|
ack.ack.ok = ok;
|
||||||
|
|
||||||
@ -260,7 +260,7 @@ void TraversalClient::HandlePing()
|
|||||||
if (IsConnected() && now - m_PingTime >= 500)
|
if (IsConnected() && now - m_PingTime >= 500)
|
||||||
{
|
{
|
||||||
TraversalPacket ping = {};
|
TraversalPacket ping = {};
|
||||||
ping.type = TraversalPacketPing;
|
ping.type = TraversalPacketType::Ping;
|
||||||
ping.ping.hostId = m_HostId;
|
ping.ping.hostId = m_HostId;
|
||||||
SendTraversalPacket(ping);
|
SendTraversalPacket(ping);
|
||||||
m_PingTime = now;
|
m_PingTime = now;
|
||||||
|
@ -8,25 +8,25 @@
|
|||||||
typedef std::array<char, NETPLAY_CODE_SIZE> TraversalHostId;
|
typedef std::array<char, NETPLAY_CODE_SIZE> TraversalHostId;
|
||||||
typedef u64 TraversalRequestId;
|
typedef u64 TraversalRequestId;
|
||||||
|
|
||||||
enum TraversalPacketType
|
enum class TraversalPacketType : u8
|
||||||
{
|
{
|
||||||
// [*->*]
|
// [*->*]
|
||||||
TraversalPacketAck = 0,
|
Ack = 0,
|
||||||
// [c->s]
|
// [c->s]
|
||||||
TraversalPacketPing = 1,
|
Ping = 1,
|
||||||
// [c->s]
|
// [c->s]
|
||||||
TraversalPacketHelloFromClient = 2,
|
HelloFromClient = 2,
|
||||||
// [s->c]
|
// [s->c]
|
||||||
TraversalPacketHelloFromServer = 3,
|
HelloFromServer = 3,
|
||||||
// [c->s] When connecting, first the client asks the central server...
|
// [c->s] When connecting, first the client asks the central server...
|
||||||
TraversalPacketConnectPlease = 4,
|
ConnectPlease = 4,
|
||||||
// [s->c] ...who asks the game host to send a UDP packet to the
|
// [s->c] ...who asks the game host to send a UDP packet to the
|
||||||
// client... (an ack implies success)
|
// client... (an ack implies success)
|
||||||
TraversalPacketPleaseSendPacket = 5,
|
PleaseSendPacket = 5,
|
||||||
// [s->c] ...which the central server relays back to the client.
|
// [s->c] ...which the central server relays back to the client.
|
||||||
TraversalPacketConnectReady = 6,
|
ConnectReady = 6,
|
||||||
// [s->c] Alternately, the server might not have heard of this host.
|
// [s->c] Alternately, the server might not have heard of this host.
|
||||||
TraversalPacketConnectFailed = 7
|
ConnectFailed = 7,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum
|
enum
|
||||||
@ -50,7 +50,7 @@ struct TraversalInetAddress
|
|||||||
};
|
};
|
||||||
struct TraversalPacket
|
struct TraversalPacket
|
||||||
{
|
{
|
||||||
u8 type;
|
TraversalPacketType type;
|
||||||
TraversalRequestId requestId;
|
TraversalRequestId requestId;
|
||||||
union
|
union
|
||||||
{
|
{
|
||||||
|
@ -185,8 +185,9 @@ 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
|
||||||
printf("-> %d %llu %s\n", ((TraversalPacket*)buffer)->type,
|
const auto* packet = static_cast<const TraversalPacket*>(buffer);
|
||||||
(long long)((TraversalPacket*)buffer)->requestId, SenderName(addr));
|
printf("-> %d %llu %s\n", static_cast<int>(packet->type),
|
||||||
|
static_cast<long long>(packet->requestId), SenderName(addr));
|
||||||
#endif
|
#endif
|
||||||
if ((size_t)sendto(sock, buffer, size, 0, (sockaddr*)addr, sizeof(*addr)) != size)
|
if ((size_t)sendto(sock, buffer, size, 0, (sockaddr*)addr, sizeof(*addr)) != size)
|
||||||
{
|
{
|
||||||
@ -227,7 +228,7 @@ static void ResendPackets()
|
|||||||
{
|
{
|
||||||
if (info->tries >= NUMBER_OF_TRIES)
|
if (info->tries >= NUMBER_OF_TRIES)
|
||||||
{
|
{
|
||||||
if (info->packet.type == TraversalPacketPleaseSendPacket)
|
if (info->packet.type == 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,7 +246,7 @@ static void ResendPackets()
|
|||||||
for (const auto& p : todoFailures)
|
for (const auto& p : todoFailures)
|
||||||
{
|
{
|
||||||
TraversalPacket* fail = AllocPacket(MakeSinAddr(p.first));
|
TraversalPacket* fail = AllocPacket(MakeSinAddr(p.first));
|
||||||
fail->type = TraversalPacketConnectFailed;
|
fail->type = TraversalPacketType::ConnectFailed;
|
||||||
fail->connectFailed.requestId = p.second;
|
fail->connectFailed.requestId = p.second;
|
||||||
fail->connectFailed.reason = TraversalConnectFailedReason::ClientDidntRespond;
|
fail->connectFailed.reason = TraversalConnectFailedReason::ClientDidntRespond;
|
||||||
}
|
}
|
||||||
@ -254,12 +255,13 @@ static void ResendPackets()
|
|||||||
static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
|
static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
printf("<- %d %llu %s\n", packet->type, (long long)packet->requestId, SenderName(addr));
|
printf("<- %d %llu %s\n", static_cast<int>(packet->type),
|
||||||
|
static_cast<long long>(packet->requestId), SenderName(addr));
|
||||||
#endif
|
#endif
|
||||||
bool packetOk = true;
|
bool packetOk = true;
|
||||||
switch (packet->type)
|
switch (packet->type)
|
||||||
{
|
{
|
||||||
case TraversalPacketAck:
|
case TraversalPacketType::Ack:
|
||||||
{
|
{
|
||||||
auto it = outgoingPackets.find(packet->requestId);
|
auto it = outgoingPackets.find(packet->requestId);
|
||||||
if (it == outgoingPackets.end())
|
if (it == outgoingPackets.end())
|
||||||
@ -267,18 +269,18 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
|
|||||||
|
|
||||||
OutgoingPacketInfo* info = &it->second;
|
OutgoingPacketInfo* info = &it->second;
|
||||||
|
|
||||||
if (info->packet.type == TraversalPacketPleaseSendPacket)
|
if (info->packet.type == TraversalPacketType::PleaseSendPacket)
|
||||||
{
|
{
|
||||||
TraversalPacket* ready = AllocPacket(MakeSinAddr(info->packet.pleaseSendPacket.address));
|
TraversalPacket* ready = AllocPacket(MakeSinAddr(info->packet.pleaseSendPacket.address));
|
||||||
if (packet->ack.ok)
|
if (packet->ack.ok)
|
||||||
{
|
{
|
||||||
ready->type = TraversalPacketConnectReady;
|
ready->type = 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 = TraversalPacketConnectFailed;
|
ready->type = TraversalPacketType::ConnectFailed;
|
||||||
ready->connectFailed.requestId = info->misc;
|
ready->connectFailed.requestId = info->misc;
|
||||||
ready->connectFailed.reason = TraversalConnectFailedReason::ClientFailure;
|
ready->connectFailed.reason = TraversalConnectFailedReason::ClientFailure;
|
||||||
}
|
}
|
||||||
@ -287,17 +289,17 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
|
|||||||
outgoingPackets.erase(it);
|
outgoingPackets.erase(it);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TraversalPacketPing:
|
case 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 TraversalPacketHelloFromClient:
|
case TraversalPacketType::HelloFromClient:
|
||||||
{
|
{
|
||||||
u8 ok = packet->helloFromClient.protoVersion <= TraversalProtoVersion;
|
u8 ok = packet->helloFromClient.protoVersion <= TraversalProtoVersion;
|
||||||
TraversalPacket* reply = AllocPacket(*addr);
|
TraversalPacket* reply = AllocPacket(*addr);
|
||||||
reply->type = TraversalPacketHelloFromServer;
|
reply->type = TraversalPacketType::HelloFromServer;
|
||||||
reply->helloFromServer.ok = ok;
|
reply->helloFromServer.ok = ok;
|
||||||
if (ok)
|
if (ok)
|
||||||
{
|
{
|
||||||
@ -323,32 +325,34 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TraversalPacketConnectPlease:
|
case TraversalPacketType::ConnectPlease:
|
||||||
{
|
{
|
||||||
TraversalHostId& hostId = packet->connectPlease.hostId;
|
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);
|
TraversalPacket* reply = AllocPacket(*addr);
|
||||||
reply->type = TraversalPacketConnectFailed;
|
reply->type = TraversalPacketType::ConnectFailed;
|
||||||
reply->connectFailed.requestId = packet->requestId;
|
reply->connectFailed.requestId = packet->requestId;
|
||||||
reply->connectFailed.reason = TraversalConnectFailedReason::NoSuchClient;
|
reply->connectFailed.reason = TraversalConnectFailedReason::NoSuchClient;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
TraversalPacket* please = AllocPacket(MakeSinAddr(*r.value), packet->requestId);
|
TraversalPacket* please = AllocPacket(MakeSinAddr(*r.value), packet->requestId);
|
||||||
please->type = TraversalPacketPleaseSendPacket;
|
please->type = TraversalPacketType::PleaseSendPacket;
|
||||||
please->pleaseSendPacket.address = MakeInetAddress(*addr);
|
please->pleaseSendPacket.address = MakeInetAddress(*addr);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "received unknown packet type %d from %s\n", packet->type, SenderName(addr));
|
fprintf(stderr, "received unknown packet type %d from %s\n", static_cast<int>(packet->type),
|
||||||
|
SenderName(addr));
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (packet->type != TraversalPacketAck)
|
if (packet->type != TraversalPacketType::Ack)
|
||||||
{
|
{
|
||||||
TraversalPacket ack = {};
|
TraversalPacket ack = {};
|
||||||
ack.type = TraversalPacketAck;
|
ack.type = 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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user