Fix lan game in localhost, also improve timer behaviour

This commit is contained in:
Benau 2018-02-22 16:03:54 +08:00
parent 4559fd6a2c
commit 33435de026
5 changed files with 21 additions and 26 deletions

View File

@ -40,7 +40,6 @@ ConnectToPeer::ConnectToPeer(uint32_t peer_id) : Protocol(PROTOCOL_CONNECTION)
m_state = NONE;
m_is_lan = false;
setHandleConnections(true);
resetTimer();
} // ConnectToPeer(peer_id)
// ----------------------------------------------------------------------------
@ -54,7 +53,6 @@ ConnectToPeer::ConnectToPeer(const TransportAddress &address)
// We don't need to find the peer address, so we can start
// with the state when we found the peer address.
m_state = WAIT_FOR_CONNECTION;
resetTimer();
m_is_lan = true;
setHandleConnections(true);
} // ConnectToPeers(TransportAddress)
@ -113,15 +111,15 @@ void ConnectToPeer::asynchronousUpdate()
}
m_state = WAIT_FOR_CONNECTION;
resetTimer();
m_timer = 0.0;
break;
}
case WAIT_FOR_CONNECTION:
{
// Each 2 second for a ping or broadcast
if (m_timer > m_timer + std::chrono::seconds(2))
if (StkTime::getRealTime() > m_timer + 2.0)
{
resetTimer();
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
@ -153,8 +151,8 @@ void ConnectToPeer::asynchronousUpdate()
STKHost::get()->sendRawPacket(aloha, broadcast_address);
Log::info("ConnectToPeer", "Broadcast aloha to self.");
// 30 seconds timeout
if (m_tried_connection++ > 15)
// 10 seconds timeout
if (m_tried_connection++ > 5)
{
// Not much we can do about if we don't receive the client
// connection - it could have stopped, lost network, ...

View File

@ -45,7 +45,7 @@ protected:
bool m_is_lan;
/** Timer use for tracking broadcast. */
std::chrono::system_clock::time_point m_timer;
double m_timer = 0.0;
unsigned m_tried_connection = 0;
@ -60,8 +60,6 @@ protected:
EXITING
} m_state;
void resetTimer() { m_timer = std::chrono::system_clock::now(); }
public:
ConnectToPeer(uint32_t peer_id);
ConnectToPeer(const TransportAddress &address);

View File

@ -120,7 +120,7 @@ void ConnectToServer::asynchronousUpdate()
request_connection->requestStart();
m_current_protocol = request_connection;
// Reset timer for next usage
resetTimer();
m_timer = 0.0;
break;
}
case REQUESTING_CONNECTION:
@ -161,9 +161,9 @@ void ConnectToServer::asynchronousUpdate()
{
// Send a 1-byte datagram, the remote host can simply ignore
// this datagram, to keep the port open (2 second each)
if (m_timer > m_timer + std::chrono::seconds(2))
if (StkTime::getRealTime() > m_timer + 2.0)
{
resetTimer();
m_timer = StkTime::getRealTime();
BareNetworkString data;
data.addUInt8(0);
STKHost::get()->sendRawPacket(data, m_server_address);
@ -173,10 +173,11 @@ void ConnectToServer::asynchronousUpdate()
break;
case CONNECTING: // waiting the server to answer our connection
{
if (m_timer > m_timer + std::chrono::seconds(5)) // every 5 seconds
// Every 5 seconds
if (StkTime::getRealTime() > m_timer + 5.0)
{
m_timer = StkTime::getRealTime();
STKHost::get()->connect(m_server_address);
resetTimer();
Log::info("ConnectToServer", "Trying to connect to %s",
m_server_address.toString().c_str());
if (m_tried_connection++ > 3)
@ -403,7 +404,7 @@ void ConnectToServer::waitingAloha(bool is_wan)
}
m_state = CONNECTING;
// Reset timer for next usage
resetTimer();
m_timer = 0.0;
m_tried_connection = 0;
}
} // waitingAloha

View File

@ -22,13 +22,12 @@
#include "network/protocol.hpp"
#include "network/transport_address.hpp"
#include "utils/cpp2011.hpp"
#include <chrono>
#include <string>
class ConnectToServer : public Protocol
{
private:
std::chrono::system_clock::time_point m_timer;
double m_timer = 0.0;
TransportAddress m_server_address;
uint32_t m_server_id;
uint32_t m_host_id;
@ -54,7 +53,6 @@ private:
void registerWithSTKServer();
void handleQuickConnect();
void waitingAloha(bool is_wan);
void resetTimer() { m_timer = std::chrono::system_clock::now(); }
public:
ConnectToServer();

View File

@ -313,10 +313,9 @@ STKHost::STKHost(const irr::core::stringw &server_name)
}
setPrivatePort();
if (NetworkConfig::get()->isWAN())
{
setPublicAddress();
}
// We need the public address for server no matter what to determine
// local lan connection
setPublicAddress();
// Don't construct server if no public address in WAN game
if (!m_public_address.isUnset() || NetworkConfig::get()->isLAN())
{
@ -717,9 +716,10 @@ void STKHost::mainLoop()
// A separate network connection (socket) to handle LAN requests.
Network* lan_network = NULL;
if (NetworkConfig::get()->isLAN())
if (NetworkConfig::get()->isLAN() && NetworkConfig::get()->isServer())
{
TransportAddress address(0, NetworkConfig::get()->getServerDiscoveryPort());
TransportAddress address(0,
NetworkConfig::get()->getServerDiscoveryPort());
ENetAddress eaddr = address.toEnetAddress();
lan_network = new Network(1, 1, 0, 0, &eaddr);
}
@ -830,7 +830,7 @@ void STKHost::handleDirectSocketRequest(Network* lan_network)
{
// In case of a LAN connection, we only allow connections from
// a LAN address (192.168*, ..., and 127.*).
if (!sender.isLAN())
if (!sender.isLAN() && sender.getIP() != m_public_address.getIP())
{
Log::error("STKHost", "Client trying to connect from '%s'",
sender.toString().c_str());