From 7956546836904402ad18a00a1aa033c7a9c5c5ab Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Thu, 30 Sep 2021 22:03:58 +0200 Subject: [PATCH] Fix getSendBuffer reordering bug --- src/AsyncSocket.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/AsyncSocket.h b/src/AsyncSocket.h index 1b28a85..c1fddd9 100644 --- a/src/AsyncSocket.h +++ b/src/AsyncSocket.h @@ -128,9 +128,22 @@ protected: return {sendBuffer, SendBufferAttribute::NEEDS_UNCORK}; } } else { + + /* If we are corked and there is already data in the cork buffer, + mark how much is ours and reset it */ + unsigned int ourCorkOffset = 0; + if (isCorked() && loopData->corkOffset) { + ourCorkOffset = loopData->corkOffset; + loopData->corkOffset = 0; + } + /* Fallback is to use the backpressure as buffer */ - backPressure.resize(existingBackpressure + size); - return {(char *) backPressure.data() + existingBackpressure, SendBufferAttribute::NEEDS_DRAIN}; + backPressure.resize(ourCorkOffset + existingBackpressure + size); + + /* And copy corkbuffer in front */ + memcpy((char *) backPressure.data() + existingBackpressure, loopData->corkBuffer, ourCorkOffset); + + return {(char *) backPressure.data() + ourCorkOffset + existingBackpressure, SendBufferAttribute::NEEDS_DRAIN}; } }