Merge pull request #6658 from lioncash/traversal

TraversalClient: Minor organizational changes
This commit is contained in:
Léo Lam 2018-04-17 09:09:48 +02:00 committed by GitHub
commit eb97f2d5c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 31 deletions

View File

@ -22,8 +22,7 @@ static void GetRandomishBytes(u8* buf, size_t size)
}
TraversalClient::TraversalClient(ENetHost* netHost, const std::string& server, const u16 port)
: m_NetHost(netHost), m_Client(nullptr), m_ConnectRequestId(0), m_PendingConnect(false),
m_Server(server), m_port(port), m_PingTime(0)
: m_NetHost(netHost), m_Server(server), m_port(port)
{
netHost->intercept = TraversalClient::InterceptCallback;
@ -32,8 +31,21 @@ TraversalClient::TraversalClient(ENetHost* netHost, const std::string& server, c
ReconnectToServer();
}
TraversalClient::~TraversalClient()
TraversalClient::~TraversalClient() = default;
TraversalHostId TraversalClient::GetHostID() const
{
return m_HostId;
}
TraversalClient::State TraversalClient::GetState() const
{
return m_State;
}
TraversalClient::FailureReason TraversalClient::GetFailureReason() const
{
return m_FailureReason;
}
void TraversalClient::ReconnectToServer()
@ -231,7 +243,7 @@ void TraversalClient::ResendPacket(OutgoingTraversalPacketInfo* info)
void TraversalClient::HandleResends()
{
enet_uint32 now = enet_time_get();
const u32 now = enet_time_get();
for (auto& tpi : m_OutgoingTraversalPackets)
{
if (now - tpi.sendTime >= (u32)(300 * tpi.tries))
@ -253,7 +265,7 @@ void TraversalClient::HandleResends()
void TraversalClient::HandlePing()
{
enet_uint32 now = enet_time_get();
const u32 now = enet_time_get();
if (m_State == Connected && now - m_PingTime >= 500)
{
TraversalPacket ping = {};

View File

@ -41,40 +41,46 @@ public:
};
TraversalClient(ENetHost* netHost, const std::string& server, const u16 port);
~TraversalClient();
TraversalHostId GetHostID() const;
State GetState() const;
FailureReason GetFailureReason() const;
void Reset();
void ConnectToClient(const std::string& host);
void ReconnectToServer();
void Update();
// called from NetHost
bool TestPacket(u8* data, size_t size, ENetAddress* from);
void HandleResends();
ENetHost* m_NetHost;
TraversalClientClient* m_Client;
TraversalHostId m_HostId;
State m_State;
FailureReason m_FailureReason;
TraversalClientClient* m_Client = nullptr;
private:
struct OutgoingTraversalPacketInfo
{
TraversalPacket packet;
int tries;
enet_uint32 sendTime;
u32 sendTime;
};
void HandleServerPacket(TraversalPacket* packet);
// called from NetHost
bool TestPacket(u8* data, size_t size, ENetAddress* from);
void ResendPacket(OutgoingTraversalPacketInfo* info);
TraversalRequestId SendTraversalPacket(const TraversalPacket& packet);
void OnFailure(FailureReason reason);
void HandlePing();
static int ENET_CALLBACK InterceptCallback(ENetHost* host, ENetEvent* event);
TraversalRequestId m_ConnectRequestId;
bool m_PendingConnect;
ENetHost* m_NetHost;
TraversalHostId m_HostId{};
State m_State{};
FailureReason m_FailureReason{};
TraversalRequestId m_ConnectRequestId = 0;
bool m_PendingConnect = false;
std::list<OutgoingTraversalPacketInfo> m_OutgoingTraversalPackets;
ENetAddress m_ServerAddress;
ENetAddress m_ServerAddress{};
std::string m_Server;
u16 m_port;
enet_uint32 m_PingTime;
u32 m_PingTime = 0;
};
extern std::unique_ptr<TraversalClient> g_TraversalClient;
// the NetHost connected to the TraversalClient.

View File

@ -134,7 +134,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->m_State == TraversalClient::Failure)
if (m_traversal_client->GetState() == TraversalClient::Failure)
m_traversal_client->ReconnectToServer();
m_traversal_client->m_Client = this;
m_host_spec = address;
@ -909,17 +909,18 @@ void NetPlayClient::ClearBuffers()
// called from ---NETPLAY--- thread
void NetPlayClient::OnTraversalStateChanged()
{
const TraversalClient::State state = m_traversal_client->GetState();
if (m_connection_state == ConnectionState::WaitingForTraversalClientConnection &&
m_traversal_client->m_State == TraversalClient::Connected)
state == TraversalClient::Connected)
{
m_connection_state = ConnectionState::WaitingForTraversalClientConnectReady;
m_traversal_client->ConnectToClient(m_host_spec);
}
else if (m_connection_state != ConnectionState::Failure &&
m_traversal_client->m_State == TraversalClient::Failure)
else if (m_connection_state != ConnectionState::Failure && state == TraversalClient::Failure)
{
Disconnect();
m_dialog->OnTraversalError(m_traversal_client->m_FailureReason);
m_dialog->OnTraversalError(m_traversal_client->GetFailureReason());
}
}

View File

@ -90,7 +90,7 @@ NetPlayServer::NetPlayServer(const u16 port, const bool forward_port,
m_server = g_MainNetHost.get();
if (g_TraversalClient->m_State == TraversalClient::Failure)
if (g_TraversalClient->GetState() == TraversalClient::Failure)
g_TraversalClient->ReconnectToServer();
}
else
@ -736,8 +736,8 @@ unsigned int NetPlayServer::OnData(sf::Packet& packet, Client& player)
void NetPlayServer::OnTraversalStateChanged()
{
if (m_dialog && m_traversal_client->m_State == TraversalClient::Failure)
m_dialog->OnTraversalError(m_traversal_client->m_FailureReason);
if (m_dialog && m_traversal_client->GetState() == TraversalClient::Failure)
m_dialog->OnTraversalError(m_traversal_client->GetFailureReason());
}
// called from ---GUI--- thread

View File

@ -360,19 +360,22 @@ void NetPlayDialog::UpdateGUI()
// Update Room ID / IP label
if (m_use_traversal && m_room_box->currentIndex() == 0)
{
switch (g_TraversalClient->m_State)
switch (g_TraversalClient->GetState())
{
case TraversalClient::Connecting:
m_hostcode_label->setText(tr("..."));
m_hostcode_action_button->setEnabled(false);
break;
case TraversalClient::Connected:
m_hostcode_label->setText(QString::fromStdString(
std::string(g_TraversalClient->m_HostId.data(), g_TraversalClient->m_HostId.size())));
{
const auto host_id = g_TraversalClient->GetHostID();
m_hostcode_label->setText(
QString::fromStdString(std::string(host_id.begin(), host_id.end())));
m_hostcode_action_button->setEnabled(true);
m_hostcode_action_button->setText(tr("Copy"));
m_is_copy_button_retry = false;
break;
}
case TraversalClient::Failure:
m_hostcode_label->setText(tr("Error"));
m_hostcode_action_button->setText(tr("Retry"));

View File

@ -777,7 +777,7 @@ void NetPlayDialog::UpdateHostLabel()
if (sel == 0)
{
// the traversal ID
switch (g_TraversalClient->m_State)
switch (g_TraversalClient->GetState())
{
case TraversalClient::Connecting:
m_host_label->SetForegroundColour(*wxLIGHT_GREY);
@ -786,13 +786,15 @@ void NetPlayDialog::UpdateHostLabel()
m_host_copy_btn->Disable();
break;
case TraversalClient::Connected:
{
const auto host_id = g_TraversalClient->GetHostID();
m_host_label->SetForegroundColour(*wxBLACK);
m_host_label->SetLabel(
wxString(g_TraversalClient->m_HostId.data(), g_TraversalClient->m_HostId.size()));
m_host_label->SetLabel(wxString(host_id.data(), host_id.size()));
m_host_copy_btn->SetLabel(_("Copy"));
m_host_copy_btn->Enable();
m_host_copy_btn_is_retry = false;
break;
}
case TraversalClient::Failure:
m_host_label->SetForegroundColour(*wxBLACK);
m_host_label->SetLabel("...");