BodyWriter.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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();
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 }