Codechange: don't check things outside the lock, that could change while waiting on the lock

pull/688/head
Rubidium 3 months ago committed by rubidium42
parent ec4104ec6c
commit 3c488e1eb8

@ -138,13 +138,13 @@ struct PacketWriter : SaveFilter {
void Write(byte *buf, size_t size) override
{
std::lock_guard<std::mutex> 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>(PACKET_SERVER_MAP_DATA, TCP_MTU);
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> lock(this->mutex);
/* Make sure the last packet is flushed. */
if (this->current != nullptr) this->packets.push_back(std::move(this->current));

Loading…
Cancel
Save