TraversalProto: Convert TraversalPacketType into an enum class

Makes for strong typing and prevents namespace pollution.
This commit is contained in:
Lioncash 2021-01-19 14:11:39 -05:00
parent 2f0fb2056e
commit d2ea94195c
3 changed files with 43 additions and 39 deletions

View File

@ -51,7 +51,7 @@ void TraversalClient::ReconnectToServer()
m_State = State::Connecting;
TraversalPacket hello = {};
hello.type = TraversalPacketHelloFromClient;
hello.type = TraversalPacketType::HelloFromClient;
hello.helloFromClient.protoVersion = TraversalProtoVersion;
SendTraversalPacket(hello);
if (m_Client)
@ -81,7 +81,7 @@ void TraversalClient::ConnectToClient(const std::string& host)
return;
}
TraversalPacket packet = {};
packet.type = TraversalPacketConnectPlease;
packet.type = TraversalPacketType::ConnectPlease;
memcpy(packet.connectPlease.hostId.data(), host.c_str(), host.size());
m_ConnectRequestId = SendTraversalPacket(packet);
m_PendingConnect = true;
@ -129,7 +129,7 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
u8 ok = 1;
switch (packet->type)
{
case TraversalPacketAck:
case TraversalPacketType::Ack:
if (!packet->ack.ok)
{
OnFailure(FailureReason::ServerForgotAboutUs);
@ -144,7 +144,7 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
}
}
break;
case TraversalPacketHelloFromServer:
case TraversalPacketType::HelloFromServer:
if (!IsConnecting())
break;
if (!packet->helloFromServer.ok)
@ -157,7 +157,7 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
if (m_Client)
m_Client->OnTraversalStateChanged();
break;
case TraversalPacketPleaseSendPacket:
case TraversalPacketType::PleaseSendPacket:
{
// security is overrated.
ENetAddress addr = MakeENetAddress(&packet->pleaseSendPacket.address);
@ -176,8 +176,8 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
}
break;
}
case TraversalPacketConnectReady:
case TraversalPacketConnectFailed:
case TraversalPacketType::ConnectReady:
case TraversalPacketType::ConnectFailed:
{
if (!m_PendingConnect || packet->connectReady.requestId != m_ConnectRequestId)
break;
@ -187,7 +187,7 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
if (!m_Client)
break;
if (packet->type == TraversalPacketConnectReady)
if (packet->type == TraversalPacketType::ConnectReady)
m_Client->OnConnectReady(MakeENetAddress(&packet->connectReady.address));
else
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);
break;
}
if (packet->type != TraversalPacketAck)
if (packet->type != TraversalPacketType::Ack)
{
TraversalPacket ack = {};
ack.type = TraversalPacketAck;
ack.type = TraversalPacketType::Ack;
ack.requestId = packet->requestId;
ack.ack.ok = ok;
@ -260,7 +260,7 @@ void TraversalClient::HandlePing()
if (IsConnected() && now - m_PingTime >= 500)
{
TraversalPacket ping = {};
ping.type = TraversalPacketPing;
ping.type = TraversalPacketType::Ping;
ping.ping.hostId = m_HostId;
SendTraversalPacket(ping);
m_PingTime = now;

View File

@ -8,25 +8,25 @@
typedef std::array<char, NETPLAY_CODE_SIZE> TraversalHostId;
typedef u64 TraversalRequestId;
enum TraversalPacketType
enum class TraversalPacketType : u8
{
// [*->*]
TraversalPacketAck = 0,
Ack = 0,
// [c->s]
TraversalPacketPing = 1,
Ping = 1,
// [c->s]
TraversalPacketHelloFromClient = 2,
HelloFromClient = 2,
// [s->c]
TraversalPacketHelloFromServer = 3,
HelloFromServer = 3,
// [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
// client... (an ack implies success)
TraversalPacketPleaseSendPacket = 5,
PleaseSendPacket = 5,
// [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.
TraversalPacketConnectFailed = 7
ConnectFailed = 7,
};
enum
@ -50,7 +50,7 @@ struct TraversalInetAddress
};
struct TraversalPacket
{
u8 type;
TraversalPacketType type;
TraversalRequestId requestId;
union
{

View File

@ -185,8 +185,9 @@ static const char* SenderName(sockaddr_in6* addr)
static void TrySend(const void* buffer, size_t size, sockaddr_in6* addr)
{
#if DEBUG
printf("-> %d %llu %s\n", ((TraversalPacket*)buffer)->type,
(long long)((TraversalPacket*)buffer)->requestId, SenderName(addr));
const auto* packet = static_cast<const TraversalPacket*>(buffer);
printf("-> %d %llu %s\n", static_cast<int>(packet->type),
static_cast<long long>(packet->requestId), SenderName(addr));
#endif
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->packet.type == TraversalPacketPleaseSendPacket)
if (info->packet.type == TraversalPacketType::PleaseSendPacket)
{
todoFailures.push_back(std::make_pair(info->packet.pleaseSendPacket.address, info->misc));
}
@ -245,7 +246,7 @@ static void ResendPackets()
for (const auto& p : todoFailures)
{
TraversalPacket* fail = AllocPacket(MakeSinAddr(p.first));
fail->type = TraversalPacketConnectFailed;
fail->type = TraversalPacketType::ConnectFailed;
fail->connectFailed.requestId = p.second;
fail->connectFailed.reason = TraversalConnectFailedReason::ClientDidntRespond;
}
@ -254,12 +255,13 @@ static void ResendPackets()
static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
{
#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
bool packetOk = true;
switch (packet->type)
{
case TraversalPacketAck:
case TraversalPacketType::Ack:
{
auto it = outgoingPackets.find(packet->requestId);
if (it == outgoingPackets.end())
@ -267,18 +269,18 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
OutgoingPacketInfo* info = &it->second;
if (info->packet.type == TraversalPacketPleaseSendPacket)
if (info->packet.type == TraversalPacketType::PleaseSendPacket)
{
TraversalPacket* ready = AllocPacket(MakeSinAddr(info->packet.pleaseSendPacket.address));
if (packet->ack.ok)
{
ready->type = TraversalPacketConnectReady;
ready->type = TraversalPacketType::ConnectReady;
ready->connectReady.requestId = info->misc;
ready->connectReady.address = MakeInetAddress(info->dest);
}
else
{
ready->type = TraversalPacketConnectFailed;
ready->type = TraversalPacketType::ConnectFailed;
ready->connectFailed.requestId = info->misc;
ready->connectFailed.reason = TraversalConnectFailedReason::ClientFailure;
}
@ -287,17 +289,17 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
outgoingPackets.erase(it);
break;
}
case TraversalPacketPing:
case TraversalPacketType::Ping:
{
auto r = EvictFind(connectedClients, packet->ping.hostId, true);
packetOk = r.found;
break;
}
case TraversalPacketHelloFromClient:
case TraversalPacketType::HelloFromClient:
{
u8 ok = packet->helloFromClient.protoVersion <= TraversalProtoVersion;
TraversalPacket* reply = AllocPacket(*addr);
reply->type = TraversalPacketHelloFromServer;
reply->type = TraversalPacketType::HelloFromServer;
reply->helloFromServer.ok = ok;
if (ok)
{
@ -323,32 +325,34 @@ static void HandlePacket(TraversalPacket* packet, sockaddr_in6* addr)
}
break;
}
case TraversalPacketConnectPlease:
case TraversalPacketType::ConnectPlease:
{
TraversalHostId& hostId = packet->connectPlease.hostId;
auto r = EvictFind(connectedClients, hostId);
if (!r.found)
{
TraversalPacket* reply = AllocPacket(*addr);
reply->type = TraversalPacketConnectFailed;
reply->type = TraversalPacketType::ConnectFailed;
reply->connectFailed.requestId = packet->requestId;
reply->connectFailed.reason = TraversalConnectFailedReason::NoSuchClient;
}
else
{
TraversalPacket* please = AllocPacket(MakeSinAddr(*r.value), packet->requestId);
please->type = TraversalPacketPleaseSendPacket;
please->type = TraversalPacketType::PleaseSendPacket;
please->pleaseSendPacket.address = MakeInetAddress(*addr);
}
break;
}
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 = {};
ack.type = TraversalPacketAck;
ack.type = TraversalPacketType::Ack;
ack.requestId = packet->requestId;
ack.ack.ok = packetOk;
TrySend(&ack, sizeof(ack), addr);