PDA

View Full Version : A question about a code



Qiieha
13th January 2012, 12:28
Hi,
I found this code at a linux program. It is a client - server application and this is a part of the client.
The communication is ssl encrypted.



Client::Client(QString host,long port,bool fs)
{
/*if(!serverConnection)*/ serverConnection = new QSslSocket(this);

serverConnection -> setPeerVerifyMode(QSslSocket::QueryPeer);
serverConnection -> setLocalCertificate("mycert.pem");
serverConnection -> setPrivateKey("mycert.pem");
serverConnection -> connectToHostEncrypted( host, port);
peerPort = port;
connected=0;
fileSend=fs;
connect(serverConnection, SIGNAL(encrypted()), SLOT(connectionAccepted()));
connect(serverConnection,SIGNAL(stateChanged(QAbst ractSocket::SocketState)),this, SLOT(socketStateChanged(QAbstractSocket::SocketSta te)));
connect(serverConnection, SIGNAL(readyRead()), this, SLOT(readDataFromServer()));
connect(serverConnection, SIGNAL(disconnected()), serverConnection, SLOT(deleteLater()));
connect(serverConnection, SIGNAL(sslErrors(QList<QSslError>)),SLOT(sslErrors(QList<QSslError>)) );
}

The programmer calls serverConnection -> connectToHostEncrypted( host, port); and after that he connects the signal encrypted() to the slot connectionAccepted();
But it should be connected first, shouldn't it?

wysota
13th January 2012, 12:48
No, the signal won't be emitted before the control returns back to the event loop.

Qiieha
13th January 2012, 12:49
thank u for the reply!

amleto
13th January 2012, 23:34
but this is not the general case. If you call a method and something inside it emits synchronously before you have connected, oops, you missed the signal.

I believe it is better practice to connect before calling methods that can emit.

wysota
14th January 2012, 02:52
The thing is connectToHostEncrypted() doesn't emit. It schedules a connection attempt, just like QWidget::show() doesn't show anything but rather schedules showing the widget.