Best practice: how to interpret/process QDataStream?

前提是你 提交于 2019-12-14 02:33:05

问题


I need to process streamed binary data (QDataStream) of defined structure created by another non-Qt program. I wonder what is the best practice to interpret that data. Let's say the data is structured (struct definition provided) in telegrams as follows and cannot be changed by myself:

4 bytes header | 2 bytes sequence number | 1 byte checksum | 10 bytes data

I see the following possibilities to handle the data in a "Telegram" class:

  1. The Telegram class has a private QByteArray member variable. All data (17 bytes) is read at once from the stream with the readRawData method and stored in there. The variables are accessed and interpreted with public methods like return (array.at(4)<<8) + array.at(5) for the sequence number, with is not very elegant.
  2. The Telegram class has a public char[17] member variable in union with a struct. All data is read from the stream with readRawData and stored in there. When accessing the variables after, this is done only over the struct, like telegramstruct.squenceNumber I see potential problems with this method like endianness and padding.
  3. The Telegram class has private member variables for each of the telegram fields like QString strHeader. When reading the telegrams from the stream, data is stored directly in these variables. Reading is done with readRawData or with the operator>> for basic types.

The code should be as fast as possible since there is a lot of data to process. I am using Qt 5.0.1 on Windows with MinGW.

My questions:

  • Which one of the above solutions is best practice and fast, or is there a better way?
  • Is reading all at once like in 1 faster than reading 4 bytes, 2 bytes, 1 byte, ... like in 3?
  • With 1 and 3, I cannot use a provided header file with a defined struct, would that be bad practice?
  • Is it somehow possible to have kind of a "union" with QByteArray and a structure?
  • How would I easily calculate the checksum with solution 3?

Thanks a lot for your opinions and hints.

Chris


回答1:


Well, sorry dont have that much time to write code examples, but will try to give a brief hints. 1) Performance issue. As soon as you have performance constrains first thing to optimise is an amounts of actual reads from the stream from where data are coming. Whatever it's File/Socket/etc it's anyway QIODevice. So first thing to do is to maintain some kind of QByteArray where you adding all data available on a QIODevice on every ready attempt/handling data received notification. So below I assume there is certain QByteArray m_rawData which holds currently unprocessed bytes, which can be some amount of Telegrams + last telegram which can be received partially.

2) I would make a class like Telegram which holds telegram data, roughly saying

class Telegram {
       QString       header;
       int           sequenceNumber;
       unsigned char checkSum;
       QByteArray    data;

       ...

       bool          checkSumOK();  // check is checksum is OK for provided data

}

with constructors and operators on your taste (you can implement copy constructor/etc).. Then I would extend this class with ( <<, >>) operators to support QDataStream which operates on temporary buffer mentioned in part 1).

So general concept is that you read data from the stream to temporary buffer as soon as possible and after read is done you fetch from resulted buffer as many Telegram's instances as possible. Then you work with QDataSteam applied on a QByteArray you can safely use read 4 bytes, read 1 byte etc calls without having that much performance impact because in general it's mostly about shifting pointers.

3) Of course if you talking about extreme condition.. you can think of unions (as being mentioned in previous answer) to make direct copy of raw data on top of a aligned structure but that way requires much more careful programming (especially taking in consideration x32/x64 archs as well as big/little endians platforms)



来源:https://stackoverflow.com/questions/16014230/best-practice-how-to-interpret-process-qdatastream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!