Check again for max players if it's pending connection

Should fix #3874
This commit is contained in:
Benau 2019-05-12 13:39:16 +08:00
parent 4fe2fc5f58
commit d7bffa4ade
2 changed files with 43 additions and 18 deletions

View File

@ -2828,14 +2828,16 @@ void ServerLobby::connectionRequested(Event* event)
core::stringw online_name; core::stringw online_name;
if (online_id > 0) if (online_id > 0)
data.decodeStringW(&online_name); data.decodeStringW(&online_name);
handleUnencryptedConnection(peer, data, online_id, online_name); handleUnencryptedConnection(peer, data, online_id, online_name,
false/*is_pending_connection*/);
} }
} // connectionRequested } // connectionRequested
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void ServerLobby::handleUnencryptedConnection(std::shared_ptr<STKPeer> peer, void ServerLobby::handleUnencryptedConnection(std::shared_ptr<STKPeer> peer,
BareNetworkString& data, uint32_t online_id, BareNetworkString& data, uint32_t online_id,
const core::stringw& online_name, std::string country_code) const core::stringw& online_name, bool is_pending_connection,
std::string country_code)
{ {
if (data.size() < 2) return; if (data.size() < 2) return;
@ -2856,7 +2858,28 @@ void ServerLobby::handleUnencryptedConnection(std::shared_ptr<STKPeer> peer,
return; return;
} }
// Check again for duplicated player in ranked server // Check again max players and duplicated player in ranked server,
// if this is a pending connection
unsigned total_players = 0;
unsigned player_count = data.getUInt8();
if (is_pending_connection)
{
STKHost::get()->updatePlayers(NULL, NULL, &total_players);
if (total_players + player_count >
(unsigned)ServerConfig::m_server_max_players)
{
NetworkString *message = getNetworkString(2);
message->setSynchronous(true);
message->addUInt8(LE_CONNECTION_REFUSED)
.addUInt8(RR_TOO_MANY_PLAYERS);
peer->sendPacket(message, true/*reliable*/, false/*encrypted*/);
peer->reset();
delete message;
Log::verbose("ServerLobby", "Player refused: too many players");
return;
}
std::set<uint32_t> all_online_ids = std::set<uint32_t> all_online_ids =
STKHost::get()->getAllPlayerOnlineIds(); STKHost::get()->getAllPlayerOnlineIds();
bool duplicated_ranked_player = bool duplicated_ranked_player =
@ -2865,20 +2888,21 @@ void ServerLobby::handleUnencryptedConnection(std::shared_ptr<STKPeer> peer,
{ {
NetworkString* message = getNetworkString(2); NetworkString* message = getNetworkString(2);
message->setSynchronous(true); message->setSynchronous(true);
message->addUInt8(LE_CONNECTION_REFUSED).addUInt8(RR_INVALID_PLAYER); message->addUInt8(LE_CONNECTION_REFUSED)
.addUInt8(RR_INVALID_PLAYER);
peer->sendPacket(message, true/*reliable*/, false/*encrypted*/); peer->sendPacket(message, true/*reliable*/, false/*encrypted*/);
peer->reset(); peer->reset();
delete message; delete message;
Log::verbose("ServerLobby", "Player refused: invalid player"); Log::verbose("ServerLobby", "Player refused: invalid player");
return; return;
} }
}
#ifdef ENABLE_SQLITE3 #ifdef ENABLE_SQLITE3
if (country_code.empty()) if (country_code.empty())
country_code = ip2Country(peer->getAddress()); country_code = ip2Country(peer->getAddress());
#endif #endif
unsigned player_count = data.getUInt8();
auto red_blue = STKHost::get()->getAllPlayersTeamInfo(); auto red_blue = STKHost::get()->getAllPlayersTeamInfo();
for (unsigned i = 0; i < player_count; i++) for (unsigned i = 0; i < player_count; i++)
{ {
@ -3566,7 +3590,7 @@ bool ServerLobby::decryptConnectionRequest(std::shared_ptr<STKPeer> peer,
Log::info("ServerLobby", "%s validated", Log::info("ServerLobby", "%s validated",
StringUtils::wideToUtf8(online_name).c_str()); StringUtils::wideToUtf8(online_name).c_str());
handleUnencryptedConnection(peer, data, online_id, handleUnencryptedConnection(peer, data, online_id,
online_name, country_code); online_name, true/*is_pending_connection*/, country_code);
return true; return true;
} }
return false; return false;

View File

@ -282,6 +282,7 @@ private:
BareNetworkString& data, BareNetworkString& data,
uint32_t online_id, uint32_t online_id,
const irr::core::stringw& online_name, const irr::core::stringw& online_name,
bool is_pending_connection,
std::string country_code = ""); std::string country_code = "");
bool decryptConnectionRequest(std::shared_ptr<STKPeer> peer, bool decryptConnectionRequest(std::shared_ptr<STKPeer> peer,
BareNetworkString& data, BareNetworkString& data,