diff options
Diffstat (limited to 'src/ClientHandle.cpp')
-rw-r--r-- | src/ClientHandle.cpp | 101 |
1 files changed, 77 insertions, 24 deletions
diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 765ccdee2..9ed89e0a3 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -381,8 +381,8 @@ void cClientHandle::Authenticate(const AString & a_Name, const AString & a_UUID, // Send player list items SendPlayerListAddPlayer(*m_Player); - World->BroadcastPlayerListAddPlayer(*m_Player); - World->SendPlayerList(m_Player); + cRoot::Get()->BroadcastPlayerListsAddPlayer(*m_Player); + cRoot::Get()->SendPlayerLists(m_Player); m_Player->Initialize(*World); m_State = csAuthenticated; @@ -456,7 +456,7 @@ bool cClientHandle::StreamNextChunk(void) // If the chunk already loading / loaded -> skip if ( - (std::find(m_ChunksToSend.begin(), m_ChunksToSend.end(), Coords) != m_ChunksToSend.end()) || + (m_ChunksToSend.find(Coords) != m_ChunksToSend.end()) || (std::find(m_LoadedChunks.begin(), m_LoadedChunks.end(), Coords) != m_LoadedChunks.end()) ) { @@ -494,7 +494,7 @@ bool cClientHandle::StreamNextChunk(void) // If the chunk already loading / loaded -> skip if ( - (std::find(m_ChunksToSend.begin(), m_ChunksToSend.end(), Coords) != m_ChunksToSend.end()) || + (m_ChunksToSend.find(Coords) != m_ChunksToSend.end()) || (std::find(m_LoadedChunks.begin(), m_LoadedChunks.end(), Coords) != m_LoadedChunks.end()) ) { @@ -541,7 +541,7 @@ void cClientHandle::UnloadOutOfRangeChunks(void) } } - for (cChunkCoordsList::iterator itr = m_ChunksToSend.begin(); itr != m_ChunksToSend.end();) + for (auto itr = m_ChunksToSend.begin(); itr != m_ChunksToSend.end();) { int DiffX = Diff((*itr).m_ChunkX, ChunkPosX); int DiffZ = Diff((*itr).m_ChunkZ, ChunkPosZ); @@ -583,7 +583,7 @@ void cClientHandle::StreamChunk(int a_ChunkX, int a_ChunkZ, cChunkSender::eChunk { cCSLock Lock(m_CSChunkLists); m_LoadedChunks.push_back(cChunkCoords(a_ChunkX, a_ChunkZ)); - m_ChunksToSend.push_back(cChunkCoords(a_ChunkX, a_ChunkZ)); + m_ChunksToSend.emplace(a_ChunkX, a_ChunkZ); } World->SendChunkTo(a_ChunkX, a_ChunkZ, a_Priority, this); } @@ -1475,7 +1475,7 @@ void cClientHandle::HandleChat(const AString & a_Message) Msg.AddTextPart(AString("<") + m_Player->GetName() + "> ", Color); Msg.ParseText(Message); Msg.UnderlineUrls(); - m_Player->GetWorld()->BroadcastChat(Msg); + cRoot::Get()->BroadcastChat(Msg); } @@ -1902,7 +1902,7 @@ void cClientHandle::Tick(float a_Dt) if (m_TicksSinceLastPacket > 600) // 30 seconds time-out { SendDisconnect("Nooooo!! You timed out! D: Come back!"); - Destroy(); + return; } if (m_Player == nullptr) @@ -2013,7 +2013,6 @@ void cClientHandle::ServerTick(float a_Dt) if (m_TicksSinceLastPacket > 600) // 30 seconds { SendDisconnect("Nooooo!! You timed out! D: Come back!"); - Destroy(); } } @@ -2114,6 +2113,64 @@ void cClientHandle::SendChat(const cCompositeChat & a_Message) +void cClientHandle::SendChatAboveActionBar(const AString & a_Message, eMessageType a_ChatPrefix, const AString & a_AdditionalData) +{ + cWorld * World = GetPlayer()->GetWorld(); + if (World == nullptr) + { + World = cRoot::Get()->GetWorld(GetPlayer()->GetLoadedWorldName()); + if (World == nullptr) + { + World = cRoot::Get()->GetDefaultWorld(); + } + } + + AString Message = FormatMessageType(World->ShouldUseChatPrefixes(), a_ChatPrefix, a_AdditionalData); + m_Protocol->SendChatAboveActionBar(Message.append(a_Message)); +} + + + + + +void cClientHandle::SendChatAboveActionBar(const cCompositeChat & a_Message) +{ + m_Protocol->SendChatAboveActionBar(a_Message); +} + + + + + +void cClientHandle::SendChatSystem(const AString & a_Message, eMessageType a_ChatPrefix, const AString & a_AdditionalData) +{ + cWorld * World = GetPlayer()->GetWorld(); + if (World == nullptr) + { + World = cRoot::Get()->GetWorld(GetPlayer()->GetLoadedWorldName()); + if (World == nullptr) + { + World = cRoot::Get()->GetDefaultWorld(); + } + } + + AString Message = FormatMessageType(World->ShouldUseChatPrefixes(), a_ChatPrefix, a_AdditionalData); + m_Protocol->SendChatSystem(Message.append(a_Message)); +} + + + + + +void cClientHandle::SendChatSystem(const cCompositeChat & a_Message) +{ + m_Protocol->SendChatSystem(a_Message); +} + + + + + void cClientHandle::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer) { ASSERT(m_Player != nullptr); @@ -2122,15 +2179,12 @@ void cClientHandle::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializ bool Found = false; { cCSLock Lock(m_CSChunkLists); - for (cChunkCoordsList::iterator itr = m_ChunksToSend.begin(); itr != m_ChunksToSend.end(); ++itr) + auto itr = m_ChunksToSend.find(cChunkCoords{a_ChunkX, a_ChunkZ}); + if (itr != m_ChunksToSend.end()) { - if ((itr->m_ChunkX == a_ChunkX) && (itr->m_ChunkZ == a_ChunkZ)) - { - m_ChunksToSend.erase(itr); - Found = true; - break; - } - } // for itr - m_ChunksToSend[] + m_ChunksToSend.erase(itr); + Found = true; + } } if (!Found) { @@ -2182,6 +2236,8 @@ void cClientHandle::SendDestroyEntity(const cEntity & a_Entity) void cClientHandle::SendDisconnect(const AString & a_Reason) { + // Destruction (Destroy()) is called when the client disconnects, not when a disconnect packet (or anything else) is sent + // Otherwise, the cClientHandle instance is can be unexpectedly removed from the associated player - Core/#142 if (!m_HasSentDC) { LOGD("Sending a DC: \"%s\"", StripColorCodes(a_Reason).c_str()); @@ -2891,7 +2947,7 @@ bool cClientHandle::WantsSendChunk(int a_ChunkX, int a_ChunkZ) } cCSLock Lock(m_CSChunkLists); - return (std::find(m_ChunksToSend.begin(), m_ChunksToSend.end(), cChunkCoords(a_ChunkX, a_ChunkZ)) != m_ChunksToSend.end()); + return m_ChunksToSend.find(cChunkCoords(a_ChunkX, a_ChunkZ)) != m_ChunksToSend.end(); } @@ -2907,9 +2963,9 @@ void cClientHandle::AddWantedChunk(int a_ChunkX, int a_ChunkZ) LOGD("Adding chunk [%d, %d] to wanted chunks for client %p", a_ChunkX, a_ChunkZ, this); cCSLock Lock(m_CSChunkLists); - if (std::find(m_ChunksToSend.begin(), m_ChunksToSend.end(), cChunkCoords(a_ChunkX, a_ChunkZ)) == m_ChunksToSend.end()) + if (m_ChunksToSend.find(cChunkCoords(a_ChunkX, a_ChunkZ)) == m_ChunksToSend.end()) { - m_ChunksToSend.push_back(cChunkCoords(a_ChunkX, a_ChunkZ)); + m_ChunksToSend.emplace(a_ChunkX, a_ChunkZ); } } @@ -2922,7 +2978,6 @@ void cClientHandle::PacketBufferFull(void) // Too much data in the incoming queue, the server is probably too busy, kick the client: LOGERROR("Too much data in queue for client \"%s\" @ %s, kicking them.", m_Username.c_str(), m_IPString.c_str()); SendDisconnect("Server busy"); - Destroy(); } @@ -2936,7 +2991,6 @@ void cClientHandle::PacketUnknown(UInt32 a_PacketType) AString Reason; Printf(Reason, "Unknown [C->S] PacketType: 0x%x", a_PacketType); SendDisconnect(Reason); - Destroy(); } @@ -2947,7 +3001,6 @@ void cClientHandle::PacketError(UInt32 a_PacketType) { LOGERROR("Protocol error while parsing packet type 0x%02x; disconnecting client \"%s\"", a_PacketType, m_Username.c_str()); SendDisconnect("Protocol error"); - Destroy(); } @@ -2963,7 +3016,7 @@ void cClientHandle::SocketClosed(void) LOGD("Client %s @ %s disconnected", m_Username.c_str(), m_IPString.c_str()); cRoot::Get()->GetPluginManager()->CallHookDisconnect(*this, "Player disconnected"); } - if (m_State < csDestroying) + if ((m_State < csDestroying) && (m_Player != nullptr)) { cWorld * World = m_Player->GetWorld(); if (World != nullptr) |