how to use qdatatstream startTransaction properly with qtcpsocket ?
-
after reading documentation, i still have the following questions:
- this Fortune Client example use qdatastream and
startTransaction
function.because it's just a client,so simple class field tcpsocket ,qdatastream in works well .But if use it in server has many sockets to handle ,like following code inreadyRead
slots:
QTcpSocket *socket = qobject_cast<QTcpSocket *>(socketObject); if (!socket || !socket->bytesAvailable()) return; QDataStream in; in.setDevice(socket); in.setVersion(QDataStream::Qt_5_7); qint32 requestType = 0; in.startTransaction(); in >> requestType; switch (requestType) { case TransmissionType::clientConfig: { //operations code use: // in>>other type value break; } //other case } if (!in.commitTransaction()) { return; }
in above code,the qdatstream and socket are function field ,if once transfer doesn't have enough bytes,then the following bytes comes trigger the
readyread
slots,the coming bytes in datastream will doin >> requestType;
again?or just write the coming bytes into other type value?
2. the second question is that if!in.commitTransaction()
triggered, how to handle it?use above codereturn
orrollbackTransaction
?
3. 5.7 version introduce this function means that we can don't pass the block size anymore in transfer(just case that only write qt inner type value datastream ) ? is this function replace old functionforever
in readyread slots - this Fortune Client example use qdatastream and
-
once transfer doesn't have enough bytes [...] coming bytes in datastream will do in >> requestType; again?or just write the coming bytes into other type value?
If the previous transaction failed the read bytes are stored in memory so a future call to
in >> requestType
will still use the previous partial datathe second question is that if !in.commitTransaction() triggered, how to handle it?use above code return
or rollbackTransaction?from http://doc.qt.io/qt-5/qdatastream.html#commitTransaction
Otherwise, if the stream status indicates reading past the end of the data, this function restores the stream data to the point of the startTransaction() call.
no need to call rollbackTransaction
5.7 version introduce this function means that we can don't pass the block size anymore in transfer(just case that only write qt inner type value datastream ) ?
If you always use datastream and never use the raw data write on the QIODevice then yes
is this function replace old function forever in readyread slots
forever
is justfor(;;)
(akawhile(true)
) so an infinite loop, there is no link between this and the QDataStream transaction