From 3c488e1eb81e3ff7e4b90f03da533d6aa5019d9f Mon Sep 17 00:00:00 2001 From: Rubidium Date: Tue, 6 Feb 2024 18:59:58 +0100 Subject: [PATCH] Codechange: don't check things outside the lock, that could change while waiting on the lock --- src/network/network_server.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 683774858e..f79f0a7ff3 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -138,13 +138,13 @@ struct PacketWriter : SaveFilter { void Write(byte *buf, size_t size) override { + std::lock_guard lock(this->mutex); + /* We want to abort the saving when the socket is closed. */ if (this->cs == nullptr) SlError(STR_NETWORK_ERROR_LOSTCONNECTION); if (this->current == nullptr) this->current = std::make_unique(PACKET_SERVER_MAP_DATA, TCP_MTU); - std::lock_guard lock(this->mutex); - byte *bufe = buf + size; while (buf != bufe) { size_t written = this->current->Send_bytes(buf, bufe); @@ -161,11 +161,11 @@ struct PacketWriter : SaveFilter { void Finish() override { + std::lock_guard lock(this->mutex); + /* We want to abort the saving when the socket is closed. */ if (this->cs == nullptr) SlError(STR_NETWORK_ERROR_LOSTCONNECTION); - std::lock_guard lock(this->mutex); - /* Make sure the last packet is flushed. */ if (this->current != nullptr) this->packets.push_back(std::move(this->current));