void Graph::onReadyRead() { qDebug() << "Graph::readPriborData()"; QTcpSocket *socket = (QTcpSocket *)sender(); const size_t header_size = sizeof(ident_t) + sizeof(int); while(socket->bytesAvailable() >= header_size) { char buf[header_size]; socket->peek(buf, sizeof(buf)); ident_t id = *reinterpret_cast<ident_t *>(buf); int nvals = *(int *)((ident_t *)buf + 1); qDebug() << " " << id << nvals << socket->bytesAvailable(); if(socket->bytesAvailable() < nvals*sizeof(Value) + sizeof(id) + sizeof(nvals)) break; socket->read((char *)&id, sizeof(id)); socket->read((char *)&nvals, sizeof(nvals)); QByteArray data = socket->read(nvals*sizeof(Value)); if((size_t)data.size() < nvals*sizeof(Value)) break; const Value *v = (const Value *)data.constData(); QVector<Value> vals(nvals); qCopy(v, v + nvals, vals.begin()); for(Stats::iterator s = stats.begin(); s != stats.end(); ++s) { Sourcer *sourcer = s.key(); if(sourcer->ident() == id) { if(sourcer->getReliability() == 1) vals.append(Value(QDateTime::currentDateTime().toTime_t(), sourcer->value())); s->resize(w, Stat()); fillStats(*s, currentInterval(), vals); update(); } } } }
void SatelliteServer::incomingData( QObject *client ) { QTcpSocket *socket = static_cast<QTcpSocket*>(client); while( socket->bytesAvailable() > SATELLITE_PKGINFO_HEADER_SIZE ) { SATELLITE_PKGINFO_HEADER_TYPE header( 0 ); socket->peek( (char*)(&header), SATELLITE_PKGINFO_HEADER_SIZE ); header = qFromBigEndian( header ); if( (header >> 32) != SATELLITE_PKGINFO_MAGIC_VALUE ) { /* invalid data, flush */ socket->readAll(); #if SATELLITESERVER_DEBUG emit debug( "s:got bad data, flushing" ); #endif break; } qint64 datasize = (header & 0xFFFFFFFF) + SATELLITE_PKGINFO_CHECKSUM_SIZE + SATELLITE_PKGINFO_HEADER_SIZE; if( socket->bytesAvailable() < datasize ) { /* buffer incomplete, let the try at next signal */ #if SATELLITESERVER_DEBUG emit debug( "s:got incomplete data" ); #endif break; } QByteArray msg( socket->read( datasize ) ); #if SATELLITESERVER_DEBUG /* for debugging show raw message as hex dump */ emit debug( QByteArray("s:from client: ") + msg.toHex() ); #endif for( int i = 0; i < mClientConnections.count(); i++ ) { QTcpSocket *current = mClientConnections.at(i); if( current && (client != current) ) { current->write( msg ); } } } }