diff --git a/source/network/NetServer.cpp b/source/network/NetServer.cpp index 8e351e3224..248277a1c1 100644 --- a/source/network/NetServer.cpp +++ b/source/network/NetServer.cpp @@ -486,6 +486,10 @@ bool CNetServerWorker::RunStep() CNetServerSession* session = new CNetServerSession(*this, event.peer); + // Prevent the local client of the host from timing out too quickly + if (session->GetIPAddress() == "127.0.0.1") + enet_peer_timeout(event.peer, 0, MAXIMUM_HOST_TIMEOUT, MAXIMUM_HOST_TIMEOUT); + m_Sessions.push_back(session); SetupSession(session); @@ -679,8 +683,7 @@ void CNetServerWorker::OnUserJoin(CNetServerSession* session) { AddPlayer(session->GetGUID(), session->GetUserName()); - // Host is the first to join - if (m_HostGUID.empty()) + if (m_HostGUID.empty() && session->GetIPAddress() == "127.0.0.1") m_HostGUID = session->GetGUID(); CGameSetupMessage gameSetupMessage(GetScriptInterface()); diff --git a/source/network/NetSession.cpp b/source/network/NetSession.cpp index e9a1925b29..e97e6b31aa 100644 --- a/source/network/NetSession.cpp +++ b/source/network/NetSession.cpp @@ -27,6 +27,8 @@ const u32 NETWORK_WARNING_TIMEOUT = 4000; +const u32 MAXIMUM_HOST_TIMEOUT = std::numeric_limits::max(); + static const int CHANNEL_COUNT = 1; CNetClientSession::CNetClientSession(CNetClient& client) : @@ -73,6 +75,10 @@ bool CNetClientSession::Connect(u16 port, const CStr& server) m_Host = host; m_Server = peer; + // Prevent the local client of the host from timing out too quickly. + if (GetIPAddress() == "127.0.0.1") + enet_peer_timeout(peer, 1, MAXIMUM_HOST_TIMEOUT, MAXIMUM_HOST_TIMEOUT); + m_Stats = new CNetStatsTable(m_Server); if (CProfileViewer::IsInitialised()) g_ProfileViewer.AddRootTable(m_Stats); @@ -170,6 +176,15 @@ bool CNetClientSession::SendMessage(const CNetMessage* message) return CNetHost::SendMessage(message, m_Server, "server"); } +CStr CNetClientSession::GetIPAddress() const +{ + char ipAddress[256] = ""; + if (enet_address_get_host_ip(&m_Server->address, ipAddress, ARRAY_SIZE(ipAddress)) < 0) + LOGMESSAGE("Could not get IP address of the server!"); + + return ipAddress; +} + u32 CNetClientSession::GetLastReceivedTime() const { if (!m_Server) diff --git a/source/network/NetSession.h b/source/network/NetSession.h index a86db64804..e74b90c189 100644 --- a/source/network/NetSession.h +++ b/source/network/NetSession.h @@ -29,6 +29,11 @@ */ extern const u32 NETWORK_WARNING_TIMEOUT; +/** + * Maximum timeout of the local client of the host (milliseconds). + */ +extern const u32 MAXIMUM_HOST_TIMEOUT; + class CNetClient; class CNetServerWorker; @@ -88,6 +93,8 @@ public: */ virtual bool SendMessage(const CNetMessage* message); + CStr GetIPAddress() const; + /** * Number of milliseconds since the most recent packet of the server was received. */