Could you help me understand the fortune client example? Here's the readFortune() code with a few of my comments and questions, can you tell me if I understood the code correctly?
void Client::readFortune()
{
if (blockSize == 0) {
/*
If the size of the data received is less than what is required to store the block size, do nothing
*/
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
/*
Is this where the buffering takes place?
bytesAvailable() return will grow each time readyRead() is emitted, until its value is equal to blockSize
*/
if (tcpSocket->bytesAvailable() < blockSize)
return;
/*
This is where the data is read.
If we reach this part of the code, it means we have the complete data, and its safe to read and use it.
*/
in >> nextFortune;
/*
Why use QTimer and not call requestNewFortune() directly?
*/
if (nextFortune == currentFortune) {
QTimer::singleShot(0,
this,
SLOT(requestNewFortune
()));
return;
}
currentFortune = nextFortune;
statusLabel->setText(currentFortune);
getFortuneButton->setEnabled(true);
}
void Client::readFortune()
{
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);
if (blockSize == 0) {
/*
If the size of the data received is less than what is required to store the block size, do nothing
*/
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
/*
Is this where the buffering takes place?
bytesAvailable() return will grow each time readyRead() is emitted, until its value is equal to blockSize
*/
if (tcpSocket->bytesAvailable() < blockSize)
return;
QString nextFortune;
/*
This is where the data is read.
If we reach this part of the code, it means we have the complete data, and its safe to read and use it.
*/
in >> nextFortune;
/*
Why use QTimer and not call requestNewFortune() directly?
*/
if (nextFortune == currentFortune) {
QTimer::singleShot(0, this, SLOT(requestNewFortune()));
return;
}
currentFortune = nextFortune;
statusLabel->setText(currentFortune);
getFortuneButton->setEnabled(true);
}
To copy to clipboard, switch view to plain text mode
Bookmarks