BodyWriter.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "BodyWriter.h"
00025 #include "Imports.cpp"
00026 
00027 using namespace Http;
00028 
00029 BodyWriter::BodyWriter (Connection *connection)
00030  :  p()
00031 {
00032     Q_ASSERT (connection != 0);
00033 
00034     p.connection    = connection;
00035     p.file          = 0;
00036     p.bodySize      = 0;
00037     p.bytesWritten  = 0;
00038     p.bufferRead    = 0;
00039     p.bufferWritten = 0;
00040 
00041     QObject::connect (p.connection, SIGNAL  (bytesWritten (qint64)),
00042                       this,         SLOT    (bytesWritten (qint64)));
00043 }
00044 
00045 BodyWriter::~BodyWriter()
00046 {
00047 }
00048 
00049 void BodyWriter::write (QIODevice *file, qint64 bodySize)
00050 {
00051     p.file          = file;
00052     p.bodySize      = bodySize;
00053     p.bytesWritten  = 0;
00054     p.bufferRead    = 0;
00055     p.bufferWritten = 0;
00056 
00057     QMetaObject::invokeMethod (this, "bytesWritten", Qt::QueuedConnection,
00058                 QGenericReturnArgument(), Q_ARG (qint64, 0));
00059 }
00060 
00061 void BodyWriter::bytesWritten (qint64 bytes)
00062 {
00063     // Is there more to read?
00064     if (p.bytesWritten == p.bodySize)
00065         return;
00066 
00067     if (bytes > 0) {
00068         p.bytesWritten += bytes;
00069         if (p.bytesWritten == p.bodySize) {
00070             emit bodyWritten();
00071             return;
00072         }
00073     }
00074 
00075     qint64 written = 0;
00076     do {
00077         if (p.bufferWritten == p.bufferRead) {
00078             p.bufferWritten = 0;
00079             qint64 bytesLeft = p.bodySize - p.bytesWritten;
00080             if (bytesLeft > BufferSize)
00081                 p.bufferRead = BufferSize;
00082             else
00083                 p.bufferRead = bytesLeft;
00084             p.bufferRead = p.file->read (p.buffer, p.bufferRead);
00085             if (p.bufferRead == -1) {
00086                 emit readError();
00087             }
00088         }
00089 
00090         written = p.connection->write (p.buffer + p.bufferWritten,
00091                                             p.bufferRead - p.bufferWritten);
00092         if (written == -1) {
00093             emit writeError(); // would be Error::DeviceError
00094         }
00095         if (written > 0) {
00096             p.bufferWritten += written;
00097         }
00098     } while (written > 0);
00099 }
00100 
00101 bool BodyWriter::canWrite() const
00102 {
00103     return p.bytesWritten == p.bodySize;
00104 }