From 9fbc4c0ae2acab3ceba717b33c72e2382a1e5bb8 Mon Sep 17 00:00:00 2001 From: Spp Date: Wed, 24 Oct 2012 15:34:23 +0200 Subject: Core/Misc: reduced amount of string memory allocations (Step II) --- src/server/game/Server/WorldSession.cpp | 59 ++++++++++++++++----------------- src/server/game/Server/WorldSession.h | 26 ++++++++------- src/server/game/Server/WorldSocket.cpp | 4 +-- 3 files changed, 44 insertions(+), 45 deletions(-) (limited to 'src/server/game/Server') diff --git a/src/server/game/Server/WorldSession.cpp b/src/server/game/Server/WorldSession.cpp index 855ff036619..18ff3b32d67 100755 --- a/src/server/game/Server/WorldSession.cpp +++ b/src/server/game/Server/WorldSession.cpp @@ -45,6 +45,12 @@ #include "WardenWin.h" #include "WardenMac.h" +namespace { + +std::string const DefaultPlayerName = ""; + +} // namespace + bool MapSessionFilter::Process(WorldPacket* packet) { OpcodeHandler const& opHandle = opcodeTable[packet->GetOpcode()]; @@ -137,29 +143,20 @@ WorldSession::~WorldSession() LoginDatabase.PExecute("UPDATE account SET online = 0 WHERE id = %u;", GetAccountId()); // One-time query } -/// Get the player name -std::string WorldSession::GetPlayerName(bool simple /* = true */) const - { - std::string name = "[Player: "; - uint32 guidLow = 0; +std::string const & WorldSession::GetPlayerName() const +{ + return _player != NULL ? _player->GetName() : DefaultPlayerName; +} - if (Player* player = GetPlayer()) - { - name.append(player->GetName()); - guidLow = player->GetGUIDLow(); - } - else - name.append(""); +std::string WorldSession::GetPlayerInfo() const +{ + std::ostringstream ss; - if (!simple) - { - std::ostringstream ss; - ss << " (Guid: " << guidLow << ", Account: " << GetAccountId() << ")"; - name.append(ss.str()); - } + ss << "[Player: " << GetPlayerName() + << " (Guid: " << (_player != NULL ? _player->GetGUID() : 0) + << ", Account: " << GetAccountId() << ")]"; - name.append("]"); - return name; + return ss.str(); } /// Get player guid if available. Use for logging purposes only @@ -222,14 +219,14 @@ void WorldSession::QueuePacket(WorldPacket* new_packet) void WorldSession::LogUnexpectedOpcode(WorldPacket* packet, const char* status, const char *reason) { sLog->outError(LOG_FILTER_OPCODES, "Received unexpected opcode %s Status: %s Reason: %s from %s", - GetOpcodeNameForLogging(packet->GetOpcode()).c_str(), status, reason, GetPlayerName(false).c_str()); + GetOpcodeNameForLogging(packet->GetOpcode()).c_str(), status, reason, GetPlayerInfo().c_str()); } /// Logging helper for unexpected opcodes void WorldSession::LogUnprocessedTail(WorldPacket* packet) { sLog->outError(LOG_FILTER_OPCODES, "Unprocessed tail data (read stop at %u from %u) Opcode %s from %s", - uint32(packet->rpos()), uint32(packet->wpos()), GetOpcodeNameForLogging(packet->GetOpcode()).c_str(), GetPlayerName(false).c_str()); + uint32(packet->rpos()), uint32(packet->wpos()), GetOpcodeNameForLogging(packet->GetOpcode()).c_str(), GetPlayerInfo().c_str()); packet->print_storage(); } @@ -263,7 +260,7 @@ bool WorldSession::Update(uint32 diff, PacketFilter& updater) if (packet->GetOpcode() >= NUM_MSG_TYPES) { sLog->outError(LOG_FILTER_OPCODES, "Received non-existed opcode %s from %s", GetOpcodeNameForLogging(packet->GetOpcode()).c_str() - , GetPlayerName(false).c_str()); + , GetPlayerInfo().c_str()); sScriptMgr->OnUnknownPacketReceive(m_Socket, WorldPacket(*packet)); } else @@ -347,11 +344,11 @@ bool WorldSession::Update(uint32 diff, PacketFilter& updater) break; case STATUS_NEVER: sLog->outError(LOG_FILTER_OPCODES, "Received not allowed opcode %s from %s", GetOpcodeNameForLogging(packet->GetOpcode()).c_str() - , GetPlayerName(false).c_str()); + , GetPlayerInfo().c_str()); break; case STATUS_UNHANDLED: sLog->outDebug(LOG_FILTER_OPCODES, "Received not handled opcode %s from %s", GetOpcodeNameForLogging(packet->GetOpcode()).c_str() - , GetPlayerName(false).c_str()); + , GetPlayerInfo().c_str()); break; } } @@ -620,25 +617,25 @@ const char *WorldSession::GetTrinityString(int32 entry) const void WorldSession::Handle_NULL(WorldPacket& recvPacket) { sLog->outError(LOG_FILTER_OPCODES, "Received unhandled opcode %s from %s" - , GetOpcodeNameForLogging(recvPacket.GetOpcode()).c_str(), GetPlayerName(false).c_str()); + , GetOpcodeNameForLogging(recvPacket.GetOpcode()).c_str(), GetPlayerInfo().c_str()); } void WorldSession::Handle_EarlyProccess(WorldPacket& recvPacket) { sLog->outError(LOG_FILTER_OPCODES, "Received opcode %s that must be processed in WorldSocket::OnRead from %s" - , GetOpcodeNameForLogging(recvPacket.GetOpcode()).c_str(), GetPlayerName(false).c_str()); + , GetOpcodeNameForLogging(recvPacket.GetOpcode()).c_str(), GetPlayerInfo().c_str()); } void WorldSession::Handle_ServerSide(WorldPacket& recvPacket) { sLog->outError(LOG_FILTER_OPCODES, "Received server-side opcode %s from %s" - , GetOpcodeNameForLogging(recvPacket.GetOpcode()).c_str(), GetPlayerName(false).c_str()); + , GetOpcodeNameForLogging(recvPacket.GetOpcode()).c_str(), GetPlayerInfo().c_str()); } void WorldSession::Handle_Deprecated(WorldPacket& recvPacket) { sLog->outError(LOG_FILTER_OPCODES, "Received deprecated opcode %s from %s" - , GetOpcodeNameForLogging(recvPacket.GetOpcode()).c_str(), GetPlayerName(false).c_str()); + , GetOpcodeNameForLogging(recvPacket.GetOpcode()).c_str(), GetPlayerInfo().c_str()); } void WorldSession::SendAuthWaitQue(uint32 position) @@ -699,7 +696,7 @@ void WorldSession::LoadAccountData(PreparedQueryResult result, uint32 mask) while (result->NextRow()); } -void WorldSession::SetAccountData(AccountDataType type, time_t tm, std::string data) +void WorldSession::SetAccountData(AccountDataType type, time_t tm, std::string const& data) { uint32 id = 0; uint32 index = 0; @@ -1179,7 +1176,7 @@ void WorldSession::ProcessQueryCallbacks() } } -void WorldSession::InitWarden(BigNumber* k, std::string os) +void WorldSession::InitWarden(BigNumber* k, std::string const& os) { if (os == "Win") { diff --git a/src/server/game/Server/WorldSession.h b/src/server/game/Server/WorldSession.h index 3ff45bff3b1..e91ef9530c2 100755 --- a/src/server/game/Server/WorldSession.h +++ b/src/server/game/Server/WorldSession.h @@ -197,7 +197,7 @@ class CharacterCreateInfo friend class Player; protected: - CharacterCreateInfo(std::string name, uint8 race, uint8 cclass, uint8 gender, uint8 skin, uint8 face, uint8 hairStyle, uint8 hairColor, uint8 facialHair, uint8 outfitId, + CharacterCreateInfo(std::string const& name, uint8 race, uint8 cclass, uint8 gender, uint8 skin, uint8 face, uint8 hairStyle, uint8 hairColor, uint8 facialHair, uint8 outfitId, WorldPacket& data) : Name(name), Race(race), Class(cclass), Gender(gender), Skin(skin), Face(face), HairStyle(hairStyle), HairColor(hairColor), FacialHair(facialHair), OutfitId(outfitId), Data(data), CharCount(0) {} @@ -243,8 +243,8 @@ class WorldSession void SendPacket(WorldPacket const* packet); void SendNotification(const char *format, ...) ATTR_PRINTF(2, 3); void SendNotification(uint32 string_id, ...); - void SendPetNameInvalid(uint32 error, const std::string& name, DeclinedName *declinedName); - void SendPartyResult(PartyOperation operation, const std::string& member, PartyResult res, uint32 val = 0); + void SendPetNameInvalid(uint32 error, std::string const& name, DeclinedName *declinedName); + void SendPartyResult(PartyOperation operation, std::string const& member, PartyResult res, uint32 val = 0); void SendAreaTriggerMessage(const char* Text, ...) ATTR_PRINTF(2, 3); void SendSetPhaseShift(uint32 phaseShift); void SendQueryTimeResponse(); @@ -255,14 +255,16 @@ class WorldSession AccountTypes GetSecurity() const { return _security; } uint32 GetAccountId() const { return _accountId; } Player* GetPlayer() const { return _player; } - std::string GetPlayerName(bool simple = true) const; + std::string const& GetPlayerName() const; + std::string GetPlayerInfo() const; + uint32 GetGuidLow() const; void SetSecurity(AccountTypes security) { _security = security; } std::string const& GetRemoteAddress() { return m_Address; } void SetPlayer(Player* player); uint8 Expansion() const { return m_expansion; } - void InitWarden(BigNumber* k, std::string os); + void InitWarden(BigNumber* k, std::string const& os); /// Session in auth.queue currently void SetInQueue(bool state) { m_inQueue = state; } @@ -295,7 +297,7 @@ class WorldSession void SendNameQueryOpcode(uint64 guid); void SendTrainerList(uint64 guid); - void SendTrainerList(uint64 guid, const std::string& strTitle); + void SendTrainerList(uint64 guid, std::string const& strTitle); void SendListInventory(uint64 guid); void SendShowBank(uint64 guid); void SendTabardVendorActivate(uint64 guid); @@ -324,7 +326,7 @@ class WorldSession // Account Data AccountData* GetAccountData(AccountDataType type) { return &m_accountData[type]; } - void SetAccountData(AccountDataType type, time_t tm, std::string data); + void SetAccountData(AccountDataType type, time_t tm, std::string const& data); void SendAccountDataTimes(uint32 mask); void LoadGlobalAccountData(); void LoadAccountData(PreparedQueryResult result, uint32 mask); @@ -361,7 +363,7 @@ class WorldSession void SendDiscoverNewTaxiNode(uint32 nodeid); // Guild/Arena Team - void SendArenaTeamCommandResult(uint32 team_action, const std::string& team, const std::string& player, uint32 error_id); + void SendArenaTeamCommandResult(uint32 team_action, std::string const& team, std::string const& player, uint32 error_id); void SendNotInArenaTeamPacket(uint8 type); void SendPetitionShowList(uint64 guid); @@ -483,7 +485,7 @@ class WorldSession void HandleEmoteOpcode(WorldPacket& recvPacket); void HandleContactListOpcode(WorldPacket& recvPacket); void HandleAddFriendOpcode(WorldPacket& recvPacket); - void HandleAddFriendOpcodeCallBack(PreparedQueryResult result, std::string friendNote); + void HandleAddFriendOpcodeCallBack(PreparedQueryResult result, std::string const& friendNote); void HandleDelFriendOpcode(WorldPacket& recvPacket); void HandleAddIgnoreOpcode(WorldPacket& recvPacket); void HandleAddIgnoreOpcodeCallBack(PreparedQueryResult result); @@ -702,8 +704,8 @@ class WorldSession bool processChatmessageFurtherAfterSecurityChecks(std::string&, uint32); void HandleMessagechatOpcode(WorldPacket& recvPacket); - void SendPlayerNotFoundNotice(std::string name); - void SendPlayerAmbiguousNotice(std::string name); + void SendPlayerNotFoundNotice(std::string const& name); + void SendPlayerAmbiguousNotice(std::string const& name); void SendWrongFactionNotice(); void SendChatRestrictedNotice(ChatRestrictionType restriction); void HandleTextEmoteOpcode(WorldPacket& recvPacket); @@ -763,7 +765,7 @@ class WorldSession void HandleSetActionBarToggles(WorldPacket& recv_data); void HandleCharRenameOpcode(WorldPacket& recv_data); - void HandleChangePlayerNameOpcodeCallBack(PreparedQueryResult result, std::string newName); + void HandleChangePlayerNameOpcodeCallBack(PreparedQueryResult result, std::string const& newName); void HandleSetPlayerDeclinedNames(WorldPacket& recv_data); void HandleTotemDestroyed(WorldPacket& recv_data); diff --git a/src/server/game/Server/WorldSocket.cpp b/src/server/game/Server/WorldSocket.cpp index 9b4465aa6e1..ee54c8d662c 100755 --- a/src/server/game/Server/WorldSocket.cpp +++ b/src/server/game/Server/WorldSocket.cpp @@ -683,7 +683,7 @@ int WorldSocket::ProcessIncoming(WorldPacket* new_pct) case CMSG_AUTH_SESSION: if (m_Session) { - sLog->outError(LOG_FILTER_NETWORKIO, "WorldSocket::ProcessIncoming: received duplicate CMSG_AUTH_SESSION from %s", m_Session->GetPlayerName(false).c_str()); + sLog->outError(LOG_FILTER_NETWORKIO, "WorldSocket::ProcessIncoming: received duplicate CMSG_AUTH_SESSION from %s", m_Session->GetPlayerInfo().c_str()); return -1; } @@ -1006,7 +1006,7 @@ int WorldSocket::HandlePing (WorldPacket& recvPacket) if (m_Session && AccountMgr::IsPlayerAccount(m_Session->GetSecurity())) { sLog->outError(LOG_FILTER_NETWORKIO, "WorldSocket::HandlePing: %s kicked for over-speed pings (address: %s)", - m_Session->GetPlayerName(false).c_str(), GetRemoteAddress().c_str()); + m_Session->GetPlayerInfo().c_str(), GetRemoteAddress().c_str()); return -1; } -- cgit v1.2.3