void ClientThread::readClientMessage(){ this->__bufferForInputData += this->__clientSocket->readAll(); forever{ QDataStream in(&__bufferForInputData, QIODevice::ReadOnly/*this->__clientSocket*/); in.setVersion(QDataStream::Qt_4_5); if(this->__nextBlockSize == 0){ if(this->__bufferForInputData.size() < sizeof(qint64)){ return; } in >> this->__nextBlockSize; } if(this->__bufferForInputData.size() < sizeof(qint64) + this->__nextBlockSize){ return; } QString message; in >> message; this->__nextBlockSize = 0; emit gottenMessage(message); this->__bufferForInputData.clear(); return; }}void ClientThread::sendMessage(QString message){ QByteArray data; QDataStream out(&data, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_5); out << (qint64)0; out << message; out.device()->seek(0); out << (qint64)(data.size() - sizeof(qint64)); this->__clientSocket->write(data, data.size()); this->__clientSocket->waitForBytesWritten();}
void TcpClient::sendMessage(QString message){ if(this->__tcpSocket->state() != QAbstractSocket::ConnectedState){ return; } QByteArray data; QDataStream out(&data, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_5); out << (qint64)0; out << message; out.device()->seek(0); out << (qint64)(data.size() - sizeof(qint64)); this->__tcpSocket->write(data, data.size()); this->__tcpSocket->waitForBytesWritten();}void TcpClient::readServerMessage(){ this->__bufferForInputData += this->__tcpSocket->readAll(); forever{ QString message; QDataStream in(&__bufferForInputData, QIODevice::ReadOnly/*this->__tcpSocket*/); if(this->__nextBlockSize == 0){ if(this->__bufferForInputData.size() < sizeof(qint64)){ return; } in >> this->__nextBlockSize; } if(this->__bufferForInputData.size() < sizeof(qint64) + this->__nextBlockSize){ return; } in >> message; this->__nextBlockSize = 0; emit gottenMessage(message); this->__bufferForInputData.clear(); return; }}
C++ (Qt)void ClientThread::readClientMessage(){ ... if(this->__bufferForInputData.size() < sizeof(qint64) + this->__nextBlockSize){ return; } ...}
C++ (Qt)void ClientThread::readClientMessage(){ ... if(this->__bufferForInputData.size() < this->__nextBlockSize){ return; } ...}
this->__tcpSocket->waitForBytesWritten();
C++ (Qt)this->__tcpSocket->waitForBytesWritten();
C++ (Qt)this->__tcpSocket->flush();