Browse Source

modified serialized() method to use QDataStream

develop
Robin Thoni 6 years ago
parent
commit
ee7abfaeb5
3 changed files with 34 additions and 29 deletions
  1. 2
    0
      src/modules/dns/includes/dns/QDnsHeader.h
  2. 31
    25
      src/modules/dns/src/QDnsHeader.cpp
  3. 1
    4
      tests/main.cpp

+ 2
- 0
src/modules/dns/includes/dns/QDnsHeader.h View File

@@ -17,7 +17,9 @@ namespace QDNS
17 17
         QDnsHeader();
18 18
 
19 19
         static QDnsHeader parse(const QByteArray& data);
20
+        static QDnsHeader parse(QDataStream& dataStream);
20 21
         QByteArray serialize() const;
22
+        void serialize(QDataStream& dataStream) const;
21 23
 
22 24
         bool isValid() const;
23 25
 

+ 31
- 25
src/modules/dns/src/QDnsHeader.cpp View File

@@ -21,27 +21,50 @@ namespace QDNS
21 21
     }
22 22
 
23 23
     QDnsHeader QDnsHeader::parse(const QByteArray& data)
24
+    {
25
+        QDataStream dataStream(data);
26
+        return QDnsHeader::parse(dataStream);
27
+    }
28
+
29
+    QDnsHeader QDnsHeader::parse(QDataStream& dataStream)
24 30
     {
25 31
         QDnsHeader header;
26 32
 
27
-        if (data.size() < 12)
33
+        if (dataStream.device()->bytesAvailable() < 12)
28 34
         {
29 35
             header.m_isValid = false;
30 36
         }
31 37
         else
32 38
         {
33
-            QDataStream ds(data);
34
-            ds >> header.m_id;
35
-            ds >> header.m_flags;
36
-            ds >> header.m_questionCount;
37
-            ds >> header.m_answerCount;
38
-            ds >> header.m_authorityCount;
39
-            ds >> header.m_additionalCount;
39
+            dataStream >> header.m_id;
40
+            dataStream >> header.m_flags;
41
+            dataStream >> header.m_questionCount;
42
+            dataStream >> header.m_answerCount;
43
+            dataStream >> header.m_authorityCount;
44
+            dataStream >> header.m_additionalCount;
40 45
         }
41 46
 
42 47
         return header;
43 48
     }
44 49
 
50
+    QByteArray QDnsHeader::serialize() const
51
+    {
52
+        QByteArray byteArray;
53
+        QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
54
+        serialize(dataStream);
55
+        return byteArray;
56
+    }
57
+
58
+    void QDnsHeader::serialize(QDataStream& dataStream) const
59
+    {
60
+        dataStream << m_id;
61
+        dataStream << m_flags;
62
+        dataStream << m_questionCount;
63
+        dataStream << m_answerCount;
64
+        dataStream << m_authorityCount;
65
+        dataStream << m_additionalCount;
66
+    }
67
+
45 68
     bool QDnsHeader::isValid() const
46 69
     {
47 70
         return m_isValid;
@@ -230,21 +253,4 @@ namespace QDNS
230 253
         m_additionalCount = additionalCount;
231 254
     }
232 255
 
233
-    QByteArray QDnsHeader::serialize() const
234
-    {
235
-        if (isValid())
236
-        {
237
-            QByteArray data;
238
-            QDataStream ds(&data, QIODevice::WriteOnly);
239
-            ds << m_id;
240
-            ds << m_flags;
241
-            ds << m_questionCount;
242
-            ds << m_answerCount;
243
-            ds << m_authorityCount;
244
-            ds << m_additionalCount;
245
-            return data;
246
-        }
247
-        return QByteArray();
248
-    }
249
-
250 256
 }

+ 1
- 4
tests/main.cpp View File

@@ -1,6 +1,7 @@
1 1
 #include <gtest/gtest.h>
2 2
 #include <QByteArray>
3 3
 #include <QString>
4
+#include <QtCore/QDataStream>
4 5
 
5 6
 #include "dns/QDnsHeader.h"
6 7
 
@@ -61,10 +62,6 @@ TEST_P(DNSHeaderTest, parseAndGetters)
61 62
 
62 63
         ASSERT_EQ(header.serialize().toHex(), QString(params.hexData).remove(" "));
63 64
     }
64
-    else
65
-    {
66
-        ASSERT_EQ(header.serialize().size(), 0);
67
-    }
68 65
 }
69 66
 
70 67
 TEST_P(DNSHeaderTest, settersAndGetters1)

Loading…
Cancel
Save