From 005454ba7bd9aa794cf9171a196cd05e8c41e98c Mon Sep 17 00:00:00 2001 From: Benau Date: Thu, 22 Feb 2018 16:38:53 +0800 Subject: [PATCH] Use the recieved port to connect, also fix a possible crash --- src/network/protocols/connect_to_peer.cpp | 14 -------------- src/network/protocols/connect_to_peer.hpp | 1 + src/network/protocols/connect_to_server.cpp | 5 +++-- 3 files changed, 4 insertions(+), 16 deletions(-) diff --git a/src/network/protocols/connect_to_peer.cpp b/src/network/protocols/connect_to_peer.cpp index 879824377..0b97eab8f 100644 --- a/src/network/protocols/connect_to_peer.cpp +++ b/src/network/protocols/connect_to_peer.cpp @@ -120,20 +120,6 @@ void ConnectToPeer::asynchronousUpdate() if (StkTime::getRealTime() > m_timer + 2.0) { m_timer = StkTime::getRealTime(); - // Now we know the peer address. If it's a non-local host, start - // the Ping protocol to keep the port available. We can't rely - // on STKHost::isLAN(), since we might get a LAN connection even - // if the server itself accepts connections from anywhere. - if ((!m_is_lan && - m_peer_address.getIP() != - STKHost::get()->getPublicAddress().getIP()) || - NetworkConfig::m_disable_lan) - { - BareNetworkString data; - data.addUInt8(0); - STKHost::get()->sendRawPacket(data, m_peer_address); - } - // Send a broadcast packet with the string aloha_stk inside, // the client will know our ip address and will connect // The wan remote should already start its ping message to us now diff --git a/src/network/protocols/connect_to_peer.hpp b/src/network/protocols/connect_to_peer.hpp index d26c5ad86..88e7c53e5 100644 --- a/src/network/protocols/connect_to_peer.hpp +++ b/src/network/protocols/connect_to_peer.hpp @@ -47,6 +47,7 @@ protected: /** Timer use for tracking broadcast. */ double m_timer = 0.0; + /** If greater than a certain value, terminate this protocol. */ unsigned m_tried_connection = 0; enum STATE diff --git a/src/network/protocols/connect_to_server.cpp b/src/network/protocols/connect_to_server.cpp index 5eebdc9b2..859883992 100644 --- a/src/network/protocols/connect_to_server.cpp +++ b/src/network/protocols/connect_to_server.cpp @@ -211,7 +211,8 @@ void ConnectToServer::asynchronousUpdate() } m_state = DONE; // lobby room protocol if we're connected only - if (STKHost::get()->getPeers()[0]->isConnected() && + if (STKHost::get()->getPeerCount() > 0 && + STKHost::get()->getPeers()[0]->isConnected() && !m_server_address.isUnset()) { auto cl = LobbyProtocol::create(); @@ -400,8 +401,8 @@ void ConnectToServer::waitingAloha(bool is_wan) } delete[] table; #endif - m_server_address.copy(sender); } + m_server_address.copy(sender); m_state = CONNECTING; // Reset timer for next usage m_timer = 0.0;