QSslSocket and QThread
-
Hi,
I am trying to get a QSslSocket to work in QThread but I cant seem to get it to work.
The following is the code :
@
// Local includes
#include "main.h"// Constructor of the timer class
Timer::Timer(QObject *parent) : QObject(parent)
{
qDebug() << "inConstrutor";
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(startBackup()));
timer->start(5000);
}// Start the backup
int Timer::startBackup(){qDebug() << "inStart" << backingUP; //return 0; if(backingUP){ return 2; } // Start the backup thread Backup *backup = new Backup(); // Make a connection for deleting this later when done //connect(backup, SIGNAL(finished()), backup, SLOT(deleteLater())); // Set that we are running ATM backingUP = true; backup->start(); return 1;
}
// The constructor
Backup::Backup(QObject *parent)
: QThread(parent)
{// This thread can never be terminated //this->setTerminationEnabled(false);
}
// The function that does everything
void Backup::run()
{this->doit();
}
void Backup::doit(){
qDebug() << "Doit"; socket = new QSslSocket; connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead())); connect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>))); connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState))); connect(socket, SIGNAL(encrypted()), this, SLOT(socketEncrypted())); // Connect to the server socket->connectToHostEncrypted(NSettings->value("hostname").toString(), NSettings->value("port").toInt()); // Wait for it to reach the encryted and connected state if (!socket->waitForEncrypted()) { qDebug() << "waitForEncrypted Error"; return; // An error occured } // This is the protocol socket->write("ENTERHEADER\r\n"); socket->waitForBytesWritten(); QByteArray username = NSettings->value("user").toByteArray(); QByteArray device = NSettings->value("device").toByteArray(); // We first need to LOGIN this->rite("L"+ username.toBase64() +" "+ device.toBase64()); socket->waitForBytesWritten();
}
// Called whenever there is something to READ in the buffer
void Backup::socketReadyRead(){qDebug() << socket->bytesAvailable(); QString str = socket->readAll(); qDebug() << str; bool unconnected = !socket || socket->state() == QAbstractSocket::UnconnectedState; if(str == "LOGIN SUCCESSFUL"){ for(int i = 1; i <= 1000; i++){ // Was the login successful ? if(unconnected){ qDebug() << socket->state(); return; } this->rite("STORE ABC\r\n"); socket->waitForBytesWritten(); } } qDebug() << "End READ"; // Disconnect from the server //socket->disconnectFromHost();
}
// Called when the connection is fully established
void Backup::socketEncrypted()
{
qDebug() << "Connection Established";
}// Called when there are SSL errors
void Backup::sslErrors(const QList<QSslError> &errors)
{
foreach (const QSslError &error, errors){
qDebug() << error.errorString();
}// Ignore Errors socket->ignoreSslErrors();
}
// Whenever there is a change in the connection STATE, this is called
void Backup::socketStateChanged(QAbstractSocket::SocketState state)
{
qDebug() << state;
}// Socket write safe as it appends the length to the beginning of the string
qint64 Backup::rite(const QByteArray & data)
{
return socket->write(data);
}
@When I run this I get the following error :
@inConstrutor
inStart false
Doit
QObject::connect: Cannot queue arguments of type 'QAbstractSocket::SocketState'
(Make sure 'QAbstractSocket::SocketState' is registered using qRegisterMetaType().)
waitForEncrypted Error
@If I dont call the backup->start(); and call the backup->doit(); then the code works perfectly.
I would really be happy, if someone can shed any light on what I am doing wrong.