Ver Fonte

Fix crash on disconnecting from the game

Ivan Savenko há 1 ano atrás
pai
commit
dcbfea018b
1 ficheiros alterados com 7 adições e 1 exclusões
  1. 7 1
      server/CVCMIServer.cpp

+ 7 - 1
server/CVCMIServer.cpp

@@ -165,6 +165,9 @@ void CVCMIServer::onNewConnection(const std::shared_ptr<INetworkConnection> & co
 void CVCMIServer::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message)
 {
 	std::shared_ptr<CConnection> c = findConnection(connection);
+	if (c == nullptr)
+		throw std::out_of_range("Unknown connection received in CVCMIServer::findConnection");
+
 	auto pack = c->retrievePack(message);
 	pack->c = c;
 	CVCMIServerPackVisitor visitor(*this, this->gh);
@@ -197,7 +200,7 @@ std::shared_ptr<CConnection> CVCMIServer::findConnection(const std::shared_ptr<I
 			return gameConnection;
 	}
 
-	throw std::runtime_error("Unknown connection received in CVCMIServer::findConnection");
+	return nullptr;
 }
 
 bool CVCMIServer::wasStartedByClient() const
@@ -342,6 +345,9 @@ void CVCMIServer::onDisconnected(const std::shared_ptr<INetworkConnection> & con
 	logNetwork->error("Network error receiving a pack. Connection has been closed");
 
 	std::shared_ptr<CConnection> c = findConnection(connection);
+	if (!c)
+		return; // player have already disconnected via clientDisconnected call
+
 	vstd::erase(activeConnections, c);
 
 	if(activeConnections.empty() || hostClientId == c->connectionID)