try a method/class like (untested)
class InputProcessor
: public QObject{
Q_OBJECT
qint64 blocksize_;
public:
, blocksize_(0)
, input_(input)
, datastream(input)
{
connect(input_, SIGNAL(readyRead()), SLOT(slotProcessInput()));
}
public slots:
// connect tcp socket readyRead() here
void slotProcessInput()
{
// if we do not have a block size, wait for enough bytes
if (blocksize_==0)
{ // wait for blocksize
if (input_->bytesAvailable() < sizeof(qint64))
return;
datastream_ >> blocksize_;
}
// now we know the size of the transmitted block: wait for the data
if (input_->bytesAvailable() < blocksize_)
return; // we will be called again
// all data here, read block:
// datastream_ >> ...;
blocksize_ = 0; // wait for next block (size)
}
};
class InputProcessor : public QObject
{
Q_OBJECT
QIODevice *input_;
qint64 blocksize_;
QDataStream datastream_;
public:
InputProcessor(QIODevice *input, QObject *parent=0)
: QObject(parent)
, blocksize_(0)
, input_(input)
, datastream(input)
{
connect(input_, SIGNAL(readyRead()), SLOT(slotProcessInput()));
}
public slots:
// connect tcp socket readyRead() here
void slotProcessInput()
{
// if we do not have a block size, wait for enough bytes
if (blocksize_==0)
{ // wait for blocksize
if (input_->bytesAvailable() < sizeof(qint64))
return;
datastream_ >> blocksize_;
}
// now we know the size of the transmitted block: wait for the data
if (input_->bytesAvailable() < blocksize_)
return; // we will be called again
// all data here, read block:
// datastream_ >> ...;
blocksize_ = 0; // wait for next block (size)
}
};
To copy to clipboard, switch view to plain text mode
(I have not tried to compile that. Please post if it is buggy.)
HTH
Bookmarks