TraversalClient: Convert state enum into an enum class

Prevents implicit conversions and prevents identifiers from polluting
the class scope.
This commit is contained in:
Lioncash 2021-01-19 14:00:01 -05:00
parent ebf3b5faf4
commit 2021175809
5 changed files with 22 additions and 16 deletions

View File

@ -48,7 +48,7 @@ void TraversalClient::ReconnectToServer()
}
m_ServerAddress.port = m_port;
m_State = Connecting;
m_State = State::Connecting;
TraversalPacket hello = {};
hello.type = TraversalPacketHelloFromClient;
@ -145,7 +145,7 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
}
break;
case TraversalPacketHelloFromServer:
if (m_State != Connecting)
if (!IsConnecting())
break;
if (!packet->helloFromServer.ok)
{
@ -153,7 +153,7 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
break;
}
m_HostId = packet->helloFromServer.yourHostId;
m_State = Connected;
m_State = State::Connected;
if (m_Client)
m_Client->OnTraversalStateChanged();
break;
@ -214,7 +214,7 @@ void TraversalClient::HandleServerPacket(TraversalPacket* packet)
void TraversalClient::OnFailure(FailureReason reason)
{
m_State = Failure;
m_State = State::Failure;
m_FailureReason = reason;
if (m_Client)
@ -257,7 +257,7 @@ void TraversalClient::HandleResends()
void TraversalClient::HandlePing()
{
const u32 now = enet_time_get();
if (m_State == Connected && now - m_PingTime >= 500)
if (IsConnected() && now - m_PingTime >= 500)
{
TraversalPacket ping = {};
ping.type = TraversalPacketPing;

View File

@ -25,11 +25,11 @@ public:
class TraversalClient
{
public:
enum State
enum class State
{
Connecting,
Connected,
Failure
Failure,
};
enum class FailureReason
{
@ -46,6 +46,10 @@ public:
State GetState() const;
FailureReason GetFailureReason() const;
bool HasFailed() const { return m_State == State::Failure; }
bool IsConnecting() const { return m_State == State::Connecting; }
bool IsConnected() const { return m_State == State::Connected; }
void Reset();
void ConnectToClient(const std::string& host);
void ReconnectToServer();

View File

@ -174,7 +174,7 @@ NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlay
m_traversal_client = g_TraversalClient.get();
// If we were disconnected in the background, reconnect.
if (m_traversal_client->GetState() == TraversalClient::Failure)
if (m_traversal_client->HasFailed())
m_traversal_client->ReconnectToServer();
m_traversal_client->m_Client = this;
m_host_spec = address;
@ -1755,12 +1755,13 @@ void NetPlayClient::OnTraversalStateChanged()
const TraversalClient::State state = m_traversal_client->GetState();
if (m_connection_state == ConnectionState::WaitingForTraversalClientConnection &&
state == TraversalClient::Connected)
state == TraversalClient::State::Connected)
{
m_connection_state = ConnectionState::WaitingForTraversalClientConnectReady;
m_traversal_client->ConnectToClient(m_host_spec);
}
else if (m_connection_state != ConnectionState::Failure && state == TraversalClient::Failure)
else if (m_connection_state != ConnectionState::Failure &&
state == TraversalClient::State::Failure)
{
Disconnect();
m_dialog->OnTraversalError(m_traversal_client->GetFailureReason());

View File

@ -126,7 +126,7 @@ NetPlayServer::NetPlayServer(const u16 port, const bool forward_port, NetPlayUI*
m_server = g_MainNetHost.get();
if (g_TraversalClient->GetState() == TraversalClient::Failure)
if (g_TraversalClient->HasFailed())
g_TraversalClient->ReconnectToServer();
}
else
@ -190,7 +190,7 @@ void NetPlayServer::SetupIndex()
if (m_traversal_client)
{
if (m_traversal_client->GetState() != TraversalClient::Connected)
if (!m_traversal_client->IsConnected())
return;
session.server_id = std::string(g_TraversalClient->GetHostID().data(), 8);
@ -1149,7 +1149,7 @@ void NetPlayServer::OnTraversalStateChanged()
if (!m_dialog)
return;
if (state == TraversalClient::Failure)
if (state == TraversalClient::State::Failure)
m_dialog->OnTraversalError(m_traversal_client->GetFailureReason());
m_dialog->OnTraversalStateChanged(state);

View File

@ -711,11 +711,11 @@ void NetPlayDialog::UpdateGUI()
{
switch (g_TraversalClient->GetState())
{
case TraversalClient::Connecting:
case TraversalClient::State::Connecting:
m_hostcode_label->setText(tr("..."));
m_hostcode_action_button->setEnabled(false);
break;
case TraversalClient::Connected:
case TraversalClient::State::Connected:
{
const auto host_id = g_TraversalClient->GetHostID();
m_hostcode_label->setText(
@ -725,7 +725,7 @@ void NetPlayDialog::UpdateGUI()
m_is_copy_button_retry = false;
break;
}
case TraversalClient::Failure:
case TraversalClient::State::Failure:
m_hostcode_label->setText(tr("Error"));
m_hostcode_action_button->setText(tr("Retry"));
m_hostcode_action_button->setEnabled(true);
@ -1003,6 +1003,7 @@ void NetPlayDialog::OnTraversalStateChanged(TraversalClient::State state)
case TraversalClient::State::Connected:
case TraversalClient::State::Failure:
UpdateDiscordPresence();
break;
default:
break;
}