Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to use qdatatstream startTransaction properly with qtcpsocket ?

how to use qdatatstream startTransaction properly with qtcpsocket ?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SpartaWHY117
    wrote on 3 Jan 2017, 19:57 last edited by
    #1

    after reading documentation, i still have the following questions:

    1. 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 in readyRead 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 do in >> 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 code return
    or rollbackTransaction?
    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 function forever in readyread slots

    V 1 Reply Last reply 4 Jan 2017, 08:26
    1
    • S SpartaWHY117
      3 Jan 2017, 19:57

      after reading documentation, i still have the following questions:

      1. 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 in readyRead 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 do in >> 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 code return
      or rollbackTransaction?
      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 function forever in readyread slots

      V Offline
      V Offline
      VRonin
      wrote on 4 Jan 2017, 08:26 last edited by
      #2

      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 data

      the 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 just for(;;) (aka while(true)) so an infinite loop, there is no link between this and the QDataStream transaction

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1

      1/2

      3 Jan 2017, 19:57

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved