From: Wilfried Goesgens Date: Thu, 2 Jan 2014 13:08:36 +0000 (+0100) Subject: FileSendChunked(): add mode which operates without known transmission size X-Git-Tag: v9.01~156 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=4ac15ab9435ec289998b7082a84ff0d4d8ea280d FileSendChunked(): add mode which operates without known transmission size --- diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 90c8cf352..6bcbaf1fa 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -4186,15 +4186,23 @@ inline static void FDIOBufferFlush(FDIOBuffer *FDB) void FDIOBufferInit(FDIOBuffer *FDB, IOBuffer *IO, int FD, long TotalSendSize) { FDIOBufferFlush(FDB); - FDB->ChunkSize = - FDB->TotalSendSize = TotalSendSize; + + FDB->TotalSendSize = TotalSendSize; + if (TotalSendSize > 0) + FDB->ChunkSize = TotalSendSize; + else + { + TotalSendSize = SIZ * 10; + FDB->ChunkSize = TotalSendSize; + } FDB->IOB = IO; + #ifdef LINUX_SPLICE if (EnableSplice) pipe(FDB->SplicePipe); else #endif - FDB->ChunkBuffer = NewStrBufPlain(NULL, TotalSendSize + 1); + FDB->ChunkBuffer = NewStrBufPlain(NULL, TotalSendSize+ 1); FDB->OtherFD = FD; } @@ -4221,68 +4229,159 @@ void FDIOBufferDelete(FDIOBuffer *FDB) int FileSendChunked(FDIOBuffer *FDB, const char **Err) { ssize_t sent, pipesize; -#ifdef LINUX_SPLICE - if (EnableSplice) + + if (FDB->TotalSendSize > 0) { - if (FDB->PipeSize == 0) +#ifdef LINUX_SPLICE + if (EnableSplice) { - pipesize = splice(FDB->OtherFD, - &FDB->TotalSentAlready, - FDB->SplicePipe[1], - NULL, - FDB->ChunkSendRemain, - SPLICE_F_MOVE); + if (FDB->PipeSize == 0) + { + pipesize = splice(FDB->OtherFD, + &FDB->TotalSentAlready, + FDB->SplicePipe[1], + NULL, + FDB->ChunkSendRemain, + SPLICE_F_MOVE); - if (pipesize == -1) + if (pipesize == -1) + { + *Err = strerror(errno); + return pipesize; + } + FDB->PipeSize = pipesize; + } + sent = splice(FDB->SplicePipe[0], + NULL, + FDB->IOB->fd, + NULL, + FDB->PipeSize, + SPLICE_F_MORE | SPLICE_F_MOVE | SPLICE_F_NONBLOCK); + if (sent == -1) { *Err = strerror(errno); - return pipesize; + return sent; } - FDB->PipeSize = pipesize; + FDB->PipeSize -= sent; + FDB->ChunkSendRemain -= sent; + return sent; } - sent = splice(FDB->SplicePipe[0], - NULL, - FDB->IOB->fd, - NULL, - FDB->PipeSize, - SPLICE_F_MORE | SPLICE_F_MOVE | SPLICE_F_NONBLOCK); - if (sent == -1) + else +#endif { - *Err = strerror(errno); - return sent; + char *pRead; + long nRead = 0; + + pRead = FDB->ChunkBuffer->buf; + while ((FDB->ChunkBuffer->BufUsed < FDB->TotalSendSize) && (nRead >= 0)) + { + nRead = read(FDB->OtherFD, pRead, FDB->TotalSendSize - FDB->ChunkBuffer->BufUsed); + if (nRead > 0) { + FDB->ChunkBuffer->BufUsed += nRead; + FDB->ChunkBuffer->buf[FDB->ChunkBuffer->BufUsed] = '\0'; + } + else if (nRead == 0) {} + else return nRead; + } + + nRead = write(FDB->IOB->fd, + FDB->ChunkBuffer->buf + FDB->TotalSentAlready, + FDB->ChunkBuffer->BufUsed - FDB->TotalSentAlready); + + if (nRead >= 0) { + FDB->TotalSentAlready += nRead; + FDB->ChunkSendRemain -= nRead; + return FDB->ChunkSendRemain; + } + else { + return nRead; + } } - FDB->PipeSize -= sent; - FDB->ChunkSendRemain -= sent; - return sent; } else -#endif { - char *pRead; - long nRead = 0; - - pRead = FDB->ChunkBuffer->buf; - while ((FDB->ChunkBuffer->BufUsed < FDB->TotalSendSize) && (nRead >= 0)) +#ifdef LINUX_SPLICE + if (EnableSplice) { - nRead = read(FDB->OtherFD, pRead, FDB->TotalSendSize - FDB->ChunkBuffer->BufUsed); - if (nRead > 0) { - FDB->ChunkBuffer->BufUsed += nRead; - FDB->ChunkBuffer->buf[FDB->ChunkBuffer->BufUsed] = '\0'; + if (FDB->PipeSize == 0) + { + pipesize = splice(FDB->OtherFD, + &FDB->TotalSentAlready, + FDB->SplicePipe[1], + NULL, + SIZ * 10, + SPLICE_F_MOVE); + + if (pipesize == -1) + { + *Err = strerror(errno); + return pipesize; + } + FDB->PipeSize = pipesize; + if (pipesize == 0) + return -1; } - else if (nRead == 0) {} - else return nRead; - + sent = splice(FDB->SplicePipe[0], + NULL, + FDB->IOB->fd, + NULL, + FDB->PipeSize, + SPLICE_F_MORE | SPLICE_F_MOVE | SPLICE_F_NONBLOCK); + if (sent == -1) + { + *Err = strerror(errno); + return sent; + } + FDB->PipeSize -= sent; + FDB->ChunkSendRemain -= sent; + return sent; } + else +#endif + { + char *pRead; + long nRead = 0; - nRead = write(FDB->IOB->fd, FDB->ChunkBuffer->buf + FDB->TotalSentAlready, FDB->ChunkSendRemain); + pRead = FDB->ChunkBuffer->buf; + while ((FDB->ChunkSendRemain == 0) && + (FDB->ChunkBuffer->BufUsed < FDB->ChunkBuffer->BufSize) && + (nRead >= 0)) + { + FDB->TotalSentAlready = 0; + nRead = read(FDB->OtherFD, pRead, FDB->ChunkBuffer->BufSize - FDB->ChunkBuffer->BufUsed); + if (nRead > 0) { + FDB->ChunkBuffer->BufUsed += nRead; + FDB->ChunkBuffer->buf[FDB->ChunkBuffer->BufUsed] = '\0'; + FDB->ChunkSendRemain += nRead; + } + else if (nRead == 0) + { + return -1; + } + else + { + *Err = strerror(errno); + return nRead; + } + } - if (nRead >= 0) { - FDB->TotalSentAlready += nRead; - FDB->ChunkSendRemain -= nRead; - return FDB->ChunkSendRemain; - } - else { - return nRead; + nRead = write(FDB->IOB->fd, + FDB->ChunkBuffer->buf + FDB->TotalSentAlready, + FDB->ChunkBuffer->BufUsed - FDB->TotalSentAlready); + + if (nRead >= 0) { + FDB->TotalSentAlready += nRead; + FDB->ChunkSendRemain -= nRead; + if (FDB->ChunkSendRemain == 0) + { + FDB->ChunkBuffer->BufUsed = 0; + FDB->TotalSentAlready = 0; + } + return FDB->ChunkSendRemain; + } + else { + return nRead; + } } } }