I have a web server in Qt that will read a very large ( ~1Gb ) file and return the data to the requestor through a QTcpSocket. This socket is created by the main server thread. I want to use QtConcurrent to hand off this socket to a worker thread and send the data back there.
// Using QtConcurrent
BackgroundConcurrent childThreadToReturnLotsOfData;
QFuture<void> futureObject = QtConcurrent::run(&childThreadToReturnLotsOfData, &BackgroundConcurrent::returnPartialLargeFile, resp , &fileToCheckExistence);
My 'returnPartialLargeFile' function looks like:
void BackgroundConcurrent::returnPartialLargeFile( QHttpResponse *resp , QFile *fileToCheckExistence ) const
{
// We need an event loop for the QTCPSocket slots
QEventLoop loop;
//QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
// Execute our event loop
//loop.exec();
// To do this in another thread from the one created, you must
// move that socket to this thread, reparent and move
resp->m_connection->m_socket->setParent( 0 );
resp->m_connection->m_socket->moveToThread( QThread::currentThread() );
// Read in chunks until we have sent all data back to the requestor
QByteArray dataToWriteToSocket; // Store the data to send back
while ( dataReadFromFileInBytes > 0 ) {
// Read some Data into the byte array
// Write each chunk to the socket
resp->write(dataToWriteToSocket); // <----- Here is our data from the content of the file
resp->flushData(); // <----- Flush data to the socket
}
// End our response and close the connection
resp->end();
return;
}
The error I get , is that if I leave the 'loop.exec()' line commented out, I get the following error:
ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread c2630. Receiver '' (of type 'QTcpServer') was created in thread 910a8", file kernel/qcoreapplication.cpp, line 501
If I uncomment it, then my function here short circuits at the exec() line and never writes and data to the socket, but I do not get any errors, I just get a truncated response that does not include any data from the while loop.
I am reparenting the socket and moving it to the new thread, so I hope my issues are only with the event loop and the socket signals and slots. Any ideas on what I am doing wrong here? How can I get this to work? If a signals/slots issue, which do I need to connect here? Thanks -