]> git.ipfire.org Git - pakfire.git/commitdiff
xfer: Ensure that we will always send the entire message before starting the next one
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 13 Feb 2025 16:59:02 +0000 (16:59 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 13 Feb 2025 16:59:02 +0000 (16:59 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/xfer.c

index e126edcccb0c19a6ceb29fe12b54adabe5797ef1..a1bd174f9047bf12d46af14847e507d74586bb3d 100644 (file)
@@ -1675,16 +1675,12 @@ static int pakfire_xfer_store_message(struct pakfire_xfer* self,
 /*
        This function sends a WebSocket message
 */
-int pakfire_xfer_send_message(struct pakfire_xfer* xfer,
+static int __pakfire_xfer_send_message(struct pakfire_xfer* xfer,
                const char* message, const size_t length) {
        size_t bytes_sent = 0;
        size_t offset = 0;
        int r = 0;
 
-       // XXX We need to protect against sending a new message
-       // when there is still something in the send buffer
-       // Should we just send the previous message before we send this one?
-
        // Send the message
        while (offset < length) {
                r = curl_ws_send(xfer->handle, message + offset, length - offset,
@@ -1720,6 +1716,23 @@ int pakfire_xfer_send_message(struct pakfire_xfer* xfer,
        return r;
 }
 
+int pakfire_xfer_send_message(struct pakfire_xfer* self,
+               const char* message, const size_t length) {
+       int r;
+
+       // Send any left over fragments from a previous message before sending a new one
+       if (self->send_buffer.length) {
+               r = __pakfire_xfer_send_message(self, self->send_buffer.data, self->send_buffer.length);
+               if (r < 0) {
+                       ERROR(self->ctx, "Failed to send previous fragment: %s\n", strerror(-r));
+                       return r;
+               }
+       }
+
+       // Send the actual message
+       return __pakfire_xfer_send_message(self, message, length);
+}
+
 int pakfire_xfer_is_ready_to_send(struct pakfire_xfer* self) {
        // This function is only supported for sockets
        switch (self->direction) {