Пример #1
0
// Called whenever there is data available on the device.
void SignalChunker::next(QIODevice* device)
{
    // Get a pointer to the UDP socket.
    QUdpSocket* udpSocket = static_cast<QUdpSocket*>(device);
    _bytesRead = 0;

    // Get writable buffer space for the chunk.
    WritableData writableData = getDataStorage(_chunkSize);
    if (writableData.isValid()) {
        // Get pointer to start of writable memory.
        char* ptr = (char*) (writableData.ptr());

        // Read datagrams for chunk from the UDP socket.
        while (isActive() && _bytesRead < _chunkSize) {
            // Read the datagram, but avoid using pendingDatagramSize().
            if (!udpSocket->hasPendingDatagrams()) {
                // MUST WAIT for the next datagram.
                udpSocket->waitForReadyRead(100);
                continue;
            }
            qint64 maxlen = _chunkSize - _bytesRead;
            qint64 length = udpSocket->readDatagram(ptr + _bytesRead, maxlen);
            if (length > 0)
                _bytesRead += length;
        }
    }

    // Must discard the datagram if there is no available space.
    else {
        udpSocket->readDatagram(0, 0);
    }
}
void EmbraceChunker::next(QIODevice* device)
{
    QAbstractSocket* socket = static_cast<QAbstractSocket*>(device);
    WritableData writableData = getDataStorage(_sizeOfFrame);
    int readTotal = 0;
    if (writableData.isValid() ) {
        do {
            int read = socket->read((char*)writableData.ptr() + readTotal*sizeof(char) , _sizeOfFrame);
            if( read == -1 ) { 
                std::cerr << "read error";  
                // TODO - clean up and annul writableData
            }
            readTotal += read;
        } while ( readTotal < _sizeOfFrame );
    }
    else {
        // TODO dump the data here
        std::cerr << "EmbraceChunker: "
                "WritableData is not valid!" << std::endl;
    }
}