void MyClass::sendDataToSocket(const QString &header, const QVariant &data){ if (! ( state() == QAbstractSocket::UnconnectedState ) ) { QByteArray block; QDataStream out(&block, QIODevice::ReadWrite); out.setVersion(QDataStream::Qt_4_1); out << quint16(0); out << header; out << data; out.device()->seek(0); out << quint16(block.size() - sizeof(quint16)); write(block); }//void sendData
void Server::getDataFromSocket(){ QDataStream in(this); QString header; QVariant data; in.setVersion(QDataStream::Qt_4_1); logFile.writeString("[thread] - new data recieved"); forever { if (nextBlockSize == 0) { if (bytesAvailable() < sizeof(quint16)) break; in >> nextBlockSize; } if (bytesAvailable() < nextBlockSize) break; in >> header; in >> data; logFile.writeString( "[thread] - received header " + header + data.toString()); nextBlockSize = 0; }}
void Server::getDataFromSocket(){ ring_buffer << readAll();forever{ if ( ring_buffer.size() < sizeof(quint16) ) return; QDataStream stream( &ring_buffer ); quint16 size; stream >> size; if ( ring_buffer.size() < size + sizeof(quint16) ) return; // read useful data stream.close(); ring_buffer.remove( 0, sizeof(quint16) + size );}}