From 56813fd1868b58cb0594a16ecb178c038ecd8558 Mon Sep 17 00:00:00 2001 From: Ovahlord Date: Wed, 10 Nov 2021 12:19:00 +0100 Subject: [PATCH] Core/Movement: refactored pending movement change queue to be a unordered map instead so we no longer ARE overzealous with kicks when counters or ack receiving order mismatches --- src/server/game/Entities/Unit/Unit.cpp | 68 +++++++--------- src/server/game/Entities/Unit/Unit.h | 9 +-- src/server/game/Handlers/MovementHandler.cpp | 78 ++++++------------- .../game/Movement/MovementPacketSender.cpp | 4 +- 4 files changed, 60 insertions(+), 99 deletions(-) diff --git a/src/server/game/Entities/Unit/Unit.cpp b/src/server/game/Entities/Unit/Unit.cpp index 700428c365..6afa283771 100644 --- a/src/server/game/Entities/Unit/Unit.cpp +++ b/src/server/game/Entities/Unit/Unit.cpp @@ -9068,7 +9068,7 @@ void Unit::SetSpeedRate(UnitMoveType mtype, float rate) // Update speed only on change MovementChangeType changeType = MovementPacketSender::GetChangeTypeByMoveType(mtype); - if (m_speed_rate[mtype] == rate && !HasPendingMovementChange(changeType)) //todo: is the "!HasPendingMovementChange" part necessary here? + if (m_speed_rate[mtype] == rate) return; float newSpeedFlat = rate * (IsControlledByPlayer() ? playerBaseMoveSpeed[mtype] : baseMoveSpeed[mtype]); @@ -13832,30 +13832,19 @@ void Unit::UpdateHeight(float newZ) GetVehicleKit()->RelocatePassengers(); } -PlayerMovementPendingChange& Unit::PeakFirstPendingMovementChange() +void Unit::ClearPendingMovementChangeForType(MovementChangeType changeType) { - return m_pendingMovementChanges.front(); + m_pendingMovementChanges.erase(changeType); } -PlayerMovementPendingChange Unit::PopPendingMovementChange() +void Unit::AssignPendingMovementChange(MovementChangeType changeType, PlayerMovementPendingChange&& newChange) { - PlayerMovementPendingChange result = m_pendingMovementChanges.front(); - m_pendingMovementChanges.pop_front(); - return result; + m_pendingMovementChanges[changeType] = newChange; } -void Unit::PushPendingMovementChange(PlayerMovementPendingChange newChange) +PlayerMovementPendingChange const* Unit::GetPendingMovementChange(MovementChangeType changeType) const { - m_pendingMovementChanges.emplace_back(std::move(newChange)); -} - -bool Unit::HasPendingMovementChange(MovementChangeType changeType) const -{ - return std::find_if(m_pendingMovementChanges.begin(), m_pendingMovementChanges.end(), - [changeType](PlayerMovementPendingChange const& pendingChange) - { - return pendingChange.movementChangeType == changeType; - }) != m_pendingMovementChanges.end(); + return Trinity::Containers::MapGetValuePtr(m_pendingMovementChanges, changeType); } void Unit::CheckPendingMovementAcks() @@ -13866,35 +13855,38 @@ void Unit::CheckPendingMovementAcks() if (!HasPendingMovementChange()) return; - PlayerMovementPendingChange const& oldestChangeToAck = m_pendingMovementChanges.front(); - if (GameTime::GetGameTimeMS() > oldestChangeToAck.time + sWorld->getIntConfig(CONFIG_PENDING_MOVE_CHANGES_TIMEOUT)) + for (std::pair const& pendingChange : m_pendingMovementChanges) { - /* - when players are teleported from one corner of a map to an other (example: from Dragonblight to the entrance of Naxxramas, both in the same map: Northend), - is it done through what is called a 'near' teleport. A near teleport always involve teleporting a player from one point to an other in the same map, even if - the distance is huge. When that distance is big enough, a loading screen appears on the client side. During that time, the client loads the surrounding zone - of the new location (and everything it contains). The problem is that, as long as the client hasn't finished loading the new zone, it will NOT ack the near - teleport. So if the server sends a near teleport order at a certain time and the client takes 20s to load the new zone (let's imagine a very slow computer), - even with zero latency, the server will receive an ack from the client only after 20s. + if (GameTime::GetGameTimeMS() > pendingChange.second.time + sWorld->getIntConfig(CONFIG_PENDING_MOVE_CHANGES_TIMEOUT)) + { + /* + when players are teleported from one corner of a map to an other (example: from Dragonblight to the entrance of Naxxramas, both in the same map: Northend), + is it done through what is called a 'near' teleport. A near teleport always involve teleporting a player from one point to an other in the same map, even if + the distance is huge. When that distance is big enough, a loading screen appears on the client side. During that time, the client loads the surrounding zone + of the new location (and everything it contains). The problem is that, as long as the client hasn't finished loading the new zone, it will NOT ack the near + teleport. So if the server sends a near teleport order at a certain time and the client takes 20s to load the new zone (let's imagine a very slow computer), + even with zero latency, the server will receive an ack from the client only after 20s. - For this reason and because the current implementation is simple (you dear reader, feel free to improve it if you can), we will just ignore checking for - near teleport acks (for now. @todo). - */ - if (oldestChangeToAck.movementChangeType == MovementChangeType::TELEPORT) - return; + For this reason and because the current implementation is simple (you dear reader, feel free to improve it if you can), we will just ignore checking for + near teleport acks (for now. @todo). + */ + if (pendingChange.second.movementChangeType == MovementChangeType::TELEPORT) + return; - GameClient* controller = GetGameClientMovingMe(); - controller->GetWorldSession()->KickPlayer(); - TC_LOG_INFO("cheat", "Unit::CheckPendingMovementAcks: Player GUID: %s took too long to acknowledge a movement change. He was therefore kicked.", controller->GetBasePlayer()->GetGUID().ToString().c_str()); + GameClient* controller = GetGameClientMovingMe(); + controller->GetWorldSession()->KickPlayer(); + TC_LOG_INFO("cheat", "Unit::CheckPendingMovementAcks: Player GUID: %s took too long to acknowledge a movement change. He was therefore kicked.", controller->GetBasePlayer()->GetGUID().ToString().c_str()); + break; + } } } void Unit::PurgeAndApplyPendingMovementChanges(bool informObservers /* = true */) { - for (auto pendingChange = m_pendingMovementChanges.cbegin(); pendingChange != m_pendingMovementChanges.cend(); ++pendingChange) + for (std::pair const& pendingChange : m_pendingMovementChanges) { - float speedFlat = pendingChange->newValue; - MovementChangeType changeType = pendingChange->movementChangeType; + float speedFlat = pendingChange.second.newValue; + MovementChangeType changeType = pendingChange.second.movementChangeType; UnitMoveType moveType; switch (changeType) { diff --git a/src/server/game/Entities/Unit/Unit.h b/src/server/game/Entities/Unit/Unit.h index e7340b61b5..e4ec2e8f39 100644 --- a/src/server/game/Entities/Unit/Unit.h +++ b/src/server/game/Entities/Unit/Unit.h @@ -1773,11 +1773,10 @@ class TC_GAME_API Unit : public WorldObject uint32 GetMovementCounterAndInc() { return m_movementCounter++; } uint32 GetMovementCounter() { return m_movementCounter; } - PlayerMovementPendingChange& PeakFirstPendingMovementChange(); - PlayerMovementPendingChange PopPendingMovementChange(); - void PushPendingMovementChange(PlayerMovementPendingChange newChange); + void ClearPendingMovementChangeForType(MovementChangeType changeType); + void AssignPendingMovementChange(MovementChangeType changeType, PlayerMovementPendingChange&& newChange); bool HasPendingMovementChange() const { return !m_pendingMovementChanges.empty(); } - bool HasPendingMovementChange(MovementChangeType changeType) const; + PlayerMovementPendingChange const* GetPendingMovementChange(MovementChangeType changeType) const; void PurgeAndApplyPendingMovementChanges(bool informObservers = true); void RewardRage(uint32 baseRage, bool attacker); @@ -1993,7 +1992,7 @@ class TC_GAME_API Unit : public WorldObject // when a player controls this unit, and when change is made to this unit which requires an ack from the client to be acted (change of speed for example), this movementCounter is incremented uint32 m_movementCounter; - std::deque m_pendingMovementChanges; + std::unordered_map m_pendingMovementChanges; /* Player Movement fields END*/ }; diff --git a/src/server/game/Handlers/MovementHandler.cpp b/src/server/game/Handlers/MovementHandler.cpp index 8e30304e68..d795953303 100644 --- a/src/server/game/Handlers/MovementHandler.cpp +++ b/src/server/game/Handlers/MovementHandler.cpp @@ -498,38 +498,17 @@ void WorldSession::HandleForceSpeedChangeAck(WorldPacket &recvData) return; } - float speedReceived = extras.Data.floatData; + MovementChangeType changeType = MovementPacketSender::GetChangeTypeByMoveType(move_type); - // verify that indeed the client is replying with the changes that were send to him - if (!mover->HasPendingMovementChange() || mover->PeakFirstPendingMovementChange().movementCounter > movementInfo.movementCounter) - { - TC_LOG_DEBUG("entities.unit", "Ignoring ACK. Bad or outdated movement data by Player %s", _player->GetName().c_str()); + // verify that we have a pending change for the change type and that the counter equals our expected change in case we are about to receive multiple ack messages for the same type + PlayerMovementPendingChange const* pendingChange = mover->GetPendingMovementChange(changeType); + if (!pendingChange || pendingChange->movementCounter != movementInfo.movementCounter) return; - } - PlayerMovementPendingChange pendingChange = mover->PopPendingMovementChange(); - float speedSent = pendingChange.newValue; - MovementChangeType changeType = pendingChange.movementChangeType; - UnitMoveType moveTypeSent; - switch (changeType) - { - case MovementChangeType::SPEED_CHANGE_WALK: moveTypeSent = MOVE_WALK; break; - case MovementChangeType::SPEED_CHANGE_RUN: moveTypeSent = MOVE_RUN; break; - case MovementChangeType::SPEED_CHANGE_RUN_BACK: moveTypeSent = MOVE_RUN_BACK; break; - case MovementChangeType::SPEED_CHANGE_SWIM: moveTypeSent = MOVE_SWIM; break; - case MovementChangeType::SPEED_CHANGE_SWIM_BACK: moveTypeSent = MOVE_SWIM_BACK; break; - case MovementChangeType::RATE_CHANGE_TURN: moveTypeSent = MOVE_TURN_RATE; break; - case MovementChangeType::SPEED_CHANGE_FLIGHT_SPEED: moveTypeSent = MOVE_FLIGHT; break; - case MovementChangeType::SPEED_CHANGE_FLIGHT_BACK_SPEED: moveTypeSent = MOVE_FLIGHT_BACK; break; - case MovementChangeType::RATE_CHANGE_PITCH: moveTypeSent = MOVE_PITCH_RATE; break; - default: - TC_LOG_INFO("cheat", "WorldSession::HandleForceSpeedChangeAck: Player %s from account id %u kicked for incorrect data returned in an ack", - _player->GetName().c_str(), _player->GetSession()->GetAccountId()); - _player->GetSession()->KickPlayer(); - return; - } - - if (pendingChange.movementCounter != movementInfo.movementCounter || std::fabs(speedSent - speedReceived) > 0.01f || moveTypeSent!= move_type) + // Player tried to apply a speed that has not been request. Possible cheating or malformed packet. Kick the player + float speedReceived = extras.Data.floatData; + float speedSent = pendingChange->newValue; + if (std::fabs(speedSent - speedReceived) > 0.01f) { TC_LOG_INFO("cheat", "WorldSession::HandleForceSpeedChangeAck: Player %s from account id %u kicked for incorrect data returned in an ack", _player->GetName().c_str(), _player->GetSession()->GetAccountId()); @@ -555,6 +534,8 @@ void WorldSession::HandleForceSpeedChangeAck(WorldPacket &recvData) float newSpeedRate = speedSent / (mover->IsControlledByPlayer() ? playerBaseMoveSpeed[move_type] : baseMoveSpeed[move_type]); mover->SetSpeedRateReal(move_type, newSpeedRate); MovementPacketSender::SendSpeedChangeToObservers(mover, move_type, speedSent); + + mover->ClearPendingMovementChangeForType(changeType); } void WorldSession::HandleSetActiveMoverOpcode(WorldPackets::Movement::SetActiveMover& packet) @@ -707,18 +688,15 @@ void WorldSession::HandleMoveGravityDisableAck(WorldPacket& recvData) Unit* mover = ObjectAccessor::GetUnit(*_player, movementInfo.guid); - // verify that indeed the client is replying with the changes that were send to him - if (!mover->HasPendingMovementChange() || mover->PeakFirstPendingMovementChange().movementCounter > movementInfo.movementCounter) - { - TC_LOG_DEBUG("entities.unit", "Ignoring ACK. Bad or outdated movement data by Player %s", _player->GetName().c_str()); - return; - } - + // verify that we have a pending change for the change type and that the counter equals our expected change in case we are about to receive multiple ack messages for the same type + PlayerMovementPendingChange const* pendingChange = mover->GetPendingMovementChange(MovementChangeType::GRAVITY_DISABLE); bool disable = recvData.GetOpcode() == CMSG_MOVE_GRAVITY_DISABLE_ACK; - PlayerMovementPendingChange pendingChange = mover->PopPendingMovementChange(); - if (pendingChange.movementChangeType != MovementChangeType::GRAVITY_DISABLE || pendingChange.apply != disable) + if (!pendingChange || pendingChange->movementCounter != movementInfo.movementCounter) + return; + + if (pendingChange->apply != disable) { - TC_LOG_INFO("cheat", "WorldSession::HandleMoveSetCanFlyAckOpcode: Player %s from account id %u kicked for incorrect data returned in an ack", + TC_LOG_INFO("cheat", "WorldSession::HandleMoveGravityDisableAck: Player %s from account id %u kicked for incorrect data returned in an ack", _player->GetName().c_str(), _player->GetSession()->GetAccountId()); _player->GetSession()->KickPlayer(); return; @@ -752,6 +730,7 @@ void WorldSession::HandleMoveGravityDisableAck(WorldPacket& recvData) } MovementPacketSender::SendMovementFlagChangeToObservers(mover); + mover->ClearPendingMovementChangeForType(MovementChangeType::GRAVITY_DISABLE); } void WorldSession::HandleSetCollisionHeightAck(WorldPacket& recvData) @@ -774,21 +753,11 @@ void WorldSession::HandleMoveSetCanFlyAckOpcode(WorldPacket& recvData) Unit* mover = ObjectAccessor::GetUnit(*_player, movementInfo.guid); - // verify that indeed the client is replying with the changes that were send to him - if (!mover->HasPendingMovementChange() || mover->PeakFirstPendingMovementChange().movementCounter > movementInfo.movementCounter) - { - TC_LOG_DEBUG("entities.unit", "Ignoring ACK. Bad or outdated movement data by Player %s", _player->GetName().c_str()); + // verify that we have a pending change for the change type and that the counter equals our expected change in case we are about to receive multiple ack messages for the same type + PlayerMovementPendingChange const* pendingChange = mover->GetPendingMovementChange(MovementChangeType::SET_CAN_FLY); + bool disable = recvData.GetOpcode() == CMSG_MOVE_GRAVITY_DISABLE_ACK; + if (!pendingChange || pendingChange->movementCounter != movementInfo.movementCounter) return; - } - - PlayerMovementPendingChange pendingChange = mover->PopPendingMovementChange(); - if (pendingChange.movementChangeType != MovementChangeType::SET_CAN_FLY) - { - TC_LOG_INFO("cheat", "WorldSession::HandleMoveSetCanFlyAckOpcode: Player %s from account id %u kicked for incorrect data returned in an ack", - _player->GetName().c_str(), _player->GetSession()->GetAccountId()); - _player->GetSession()->KickPlayer(); - return; - } int64 movementTime = (int64)movementInfo.time + _timeSyncClockDelta; if (_timeSyncClockDelta == 0 || movementTime < 0 || movementTime > 0xFFFFFFFF) @@ -804,7 +773,7 @@ void WorldSession::HandleMoveSetCanFlyAckOpcode(WorldPacket& recvData) mover->m_movementInfo = movementInfo; mover->UpdatePosition(movementInfo.pos); - if (pendingChange.apply) + if (pendingChange->apply) { mover->AddUnitMovementFlag(MOVEMENTFLAG_CAN_FLY); mover->RemoveUnitMovementFlag(MOVEMENTFLAG_SWIMMING | MOVEMENTFLAG_SPLINE_ELEVATION); @@ -818,6 +787,7 @@ void WorldSession::HandleMoveSetCanFlyAckOpcode(WorldPacket& recvData) } MovementPacketSender::SendMovementFlagChangeToObservers(mover); + mover->ClearPendingMovementChangeForType(MovementChangeType::SET_CAN_FLY); } void WorldSession::HandleMoveTimeSkippedOpcode(WorldPacket& recvData) { diff --git a/src/server/game/Movement/MovementPacketSender.cpp b/src/server/game/Movement/MovementPacketSender.cpp index c662bd83cf..ab83fd35f7 100644 --- a/src/server/game/Movement/MovementPacketSender.cpp +++ b/src/server/game/Movement/MovementPacketSender.cpp @@ -78,7 +78,7 @@ void MovementPacketSender::SendSpeedChangeToMover(Unit* unit, UnitMoveType mtype pendingChange.movementCounter = mCounter; pendingChange.newValue = newSpeedFlat; pendingChange.movementChangeType = MovementPacketSender::GetChangeTypeByMoveType(mtype); - unit->PushPendingMovementChange(pendingChange); + unit->AssignPendingMovementChange(pendingChange.movementChangeType, std::move(pendingChange)); WorldPacket data; data.Initialize(moveTypeToOpcode[mtype][1]); @@ -134,7 +134,7 @@ void MovementPacketSender::SendMovementFlagChangeToMover(Unit* unit, MovementFla pendingChange.movementCounter = mCounter; pendingChange.apply = apply; pendingChange.movementChangeType = MovementPacketSender::GetChangeTypeByMovementFlag(mFlag); - unit->PushPendingMovementChange(pendingChange); + unit->AssignPendingMovementChange(pendingChange.movementChangeType, std::move(pendingChange)); OpcodeServer opcode; switch (mFlag)