Changeset - r25848:1aa50567d7ba
[Not reviewed]
master
0 3 0
Patric Stout - 3 years ago 2021-07-19 17:32:22
truebrain@openttd.org
Fix: [Network] rework when to close connections and inform Game Coordinator if needed
3 files changed with 32 insertions and 33 deletions:
0 comments (0 inline, 0 general)
src/network/network.cpp
Show inline comments
 
@@ -595,7 +595,7 @@ void NetworkClose(bool close_admins)
 
			MyClient::my_client->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT);
 
		}
 

	
 
		_network_coordinator_client.CloseAllTokens();
 
		_network_coordinator_client.CloseAllConnections();
 
	}
 

	
 
	TCPConnecter::KillAll();
src/network/network_coordinator.cpp
Show inline comments
 
@@ -144,7 +144,11 @@ bool ClientNetworkCoordinatorSocketHandl
 
			return false;
 

	
 
		case NETWORK_COORDINATOR_ERROR_INVALID_INVITE_CODE: {
 
			this->CloseToken(detail);
 
			auto connecter_pre_it = this->connecter_pre.find(detail);
 
			if (connecter_pre_it != this->connecter_pre.end()) {
 
				connecter_pre_it->second->SetFailure();
 
				this->connecter_pre.erase(connecter_pre_it);
 
			}
 

	
 
			/* Mark the server as offline. */
 
			NetworkGameList *item = NetworkGameListAddItem(detail);
 
@@ -258,15 +262,15 @@ bool ClientNetworkCoordinatorSocketHandl
 
	std::string invite_code = p->Recv_string(NETWORK_INVITE_CODE_LENGTH);
 

	
 
	/* Find the connecter based on the invite code. */
 
	auto connecter_it = this->connecter_pre.find(invite_code);
 
	if (connecter_it == this->connecter_pre.end()) {
 
	auto connecter_pre_it = this->connecter_pre.find(invite_code);
 
	if (connecter_pre_it == this->connecter_pre.end()) {
 
		this->CloseConnection();
 
		return false;
 
	}
 

	
 
	/* Now store it based on the token. */
 
	this->connecter[token] = connecter_it->second;
 
	this->connecter_pre.erase(connecter_it);
 
	this->connecter[token] = connecter_pre_it->second;
 
	this->connecter_pre.erase(connecter_pre_it);
 

	
 
	return true;
 
}
 
@@ -274,14 +278,6 @@ bool ClientNetworkCoordinatorSocketHandl
 
bool ClientNetworkCoordinatorSocketHandler::Receive_GC_CONNECT_FAILED(Packet *p)
 
{
 
	std::string token = p->Recv_string(NETWORK_TOKEN_LENGTH);
 

	
 
	auto connecter_it = this->connecter.find(token);
 
	if (connecter_it != this->connecter.end()) {
 
		connecter_it->second->SetFailure();
 
		this->connecter.erase(connecter_it);
 
	}
 

	
 
	/* Close all remaining connections. */
 
	this->CloseToken(token);
 

	
 
	return true;
 
@@ -382,7 +378,7 @@ NetworkRecvStatus ClientNetworkCoordinat
 
	_network_server_connection_type = CONNECTION_TYPE_UNKNOWN;
 
	this->next_update = {};
 

	
 
	this->CloseAllTokens();
 
	this->CloseAllConnections();
 

	
 
	SetWindowDirty(WC_CLIENT_LIST, 0);
 

	
 
@@ -524,11 +520,14 @@ void ClientNetworkCoordinatorSocketHandl
 
		p->Send_string(token);
 
		this->SendPacket(p);
 

	
 
		/* Find the connecter; it can happen it no longer exist, in cases where
 
		 * we aborted the connect but the Game Coordinator was already in the
 
		 * processes of connecting us. */
 
		auto connecter_it = this->connecter.find(token);
 
		assert(connecter_it != this->connecter.end());
 

	
 
		connecter_it->second->SetConnected(sock);
 
		this->connecter.erase(connecter_it);
 
		if (connecter_it != this->connecter.end()) {
 
			connecter_it->second->SetConnected(sock);
 
			this->connecter.erase(connecter_it);
 
		}
 
	}
 

	
 
	/* Close all remaining connections. */
 
@@ -552,6 +551,11 @@ void ClientNetworkCoordinatorSocketHandl
 
	this->SendPacket(p);
 
}
 

	
 
/**
 
 * Close the STUN handler.
 
 * @param token The token used for the STUN handlers.
 
 * @param family The family of STUN handlers to close. AF_UNSPEC to close all STUN handlers for this token.
 
 */
 
void ClientNetworkCoordinatorSocketHandler::CloseStunHandler(const std::string &token, uint8 family)
 
{
 
	auto stun_it = this->stun_handlers.find(token);
 
@@ -581,12 +585,6 @@ void ClientNetworkCoordinatorSocketHandl
 
 */
 
void ClientNetworkCoordinatorSocketHandler::CloseToken(const std::string &token)
 
{
 
	/* Ensure all other pending connection attempts are also killed. */
 
	if (this->game_connecter != nullptr) {
 
		this->game_connecter->Kill();
 
		this->game_connecter = nullptr;
 
	}
 

	
 
	/* Close all remaining STUN connections. */
 
	this->CloseStunHandler(token);
 

	
 
@@ -596,17 +594,12 @@ void ClientNetworkCoordinatorSocketHandl
 
		connecter_it->second->SetFailure();
 
		this->connecter.erase(connecter_it);
 
	}
 
	auto connecter_pre_it = this->connecter_pre.find(token);
 
	if (connecter_pre_it != this->connecter_pre.end()) {
 
		connecter_pre_it->second->SetFailure();
 
		this->connecter_pre.erase(connecter_pre_it);
 
	}
 
}
 

	
 
/**
 
 * Close all pending connection tokens.
 
 */
 
void ClientNetworkCoordinatorSocketHandler::CloseAllTokens()
 
void ClientNetworkCoordinatorSocketHandler::CloseAllConnections()
 
{
 
	/* Ensure all other pending connection attempts are also killed. */
 
	if (this->game_connecter != nullptr) {
 
@@ -618,11 +611,17 @@ void ClientNetworkCoordinatorSocketHandl
 
	for (auto &[token, it] : this->connecter) {
 
		this->CloseStunHandler(token);
 
		it->SetFailure();
 

	
 
		/* Inform the Game Coordinator he can stop trying to connect us to the server. */
 
		this->ConnectFailure(token, 0);
 
	}
 
	this->stun_handlers.clear();
 
	this->connecter.clear();
 

	
 
	/* Also close any pending invite-code requests. */
 
	for (auto &[invite_code, it] : this->connecter_pre) {
 
		it->SetFailure();
 
	}
 
	this->connecter.clear();
 
	this->connecter_pre.clear();
 
}
 

	
src/network/network_coordinator.h
Show inline comments
 
@@ -86,7 +86,7 @@ public:
 

	
 
	void Connect();
 
	void CloseToken(const std::string &token);
 
	void CloseAllTokens();
 
	void CloseAllConnections();
 
	void CloseStunHandler(const std::string &token, uint8 family = AF_UNSPEC);
 

	
 
	void Register();
0 comments (0 inline, 0 general)