1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qpacket_p.h" |
41 | |
42 | QT_BEGIN_NAMESPACE |
43 | |
44 | /*! |
45 | \class QPacket |
46 | \internal |
47 | |
48 | \brief The QPacket class encapsulates an unfragmentable packet of data to be |
49 | transmitted by QPacketProtocol. |
50 | |
51 | The QPacket class works together with QPacketProtocol to make it simple to |
52 | send arbitrary sized data "packets" across fragmented transports such as TCP |
53 | and UDP. |
54 | |
55 | QPacket provides a QDataStream interface to an unfragmentable packet. |
56 | Applications should construct a QPacket, propagate it with data and then |
57 | transmit it over a QPacketProtocol instance. For example: |
58 | \code |
59 | int version = QDataStream::Qt_DefaultCompiledVersion; |
60 | QPacketProtocol protocol(...); |
61 | |
62 | QPacket myPacket(version); |
63 | myPacket << "Hello world!" << 123; |
64 | protocol.send(myPacket.data()); |
65 | \endcode |
66 | |
67 | As long as both ends of the connection are using the QPacketProtocol class |
68 | and the same data stream version, the data within this packet will be |
69 | delivered unfragmented at the other end, ready for extraction. |
70 | |
71 | \code |
72 | QByteArray greeting; |
73 | int count; |
74 | |
75 | QPacket myPacket(version, protocol.read()); |
76 | |
77 | myPacket >> greeting >> count; |
78 | \endcode |
79 | |
80 | Only packets constructed from raw byte arrays may be read from. Empty QPacket |
81 | instances are for transmission only and are considered "write only". Attempting |
82 | to read data from them will result in undefined behavior. |
83 | |
84 | \ingroup io |
85 | \sa QPacketProtocol |
86 | */ |
87 | |
88 | |
89 | /*! |
90 | Constructs an empty write-only packet. |
91 | */ |
92 | QPacket::QPacket(int version) |
93 | { |
94 | buf.open(openMode: QIODevice::WriteOnly); |
95 | setDevice(&buf); |
96 | setVersion(version); |
97 | } |
98 | |
99 | /*! |
100 | Constructs a read-only packet. |
101 | */ |
102 | QPacket::QPacket(int version, const QByteArray &data) |
103 | { |
104 | buf.setData(data); |
105 | buf.open(openMode: QIODevice::ReadOnly); |
106 | setDevice(&buf); |
107 | setVersion(version); |
108 | } |
109 | |
110 | /*! |
111 | Returns a reference to the raw packet data. |
112 | */ |
113 | const QByteArray &QPacket::data() const |
114 | { |
115 | return buf.data(); |
116 | } |
117 | |
118 | /*! |
119 | Returns a copy of the raw packet data, with extra reserved space removed. |
120 | Mind that this triggers a deep copy. Use it if you anticipate the data to be detached soon anyway. |
121 | */ |
122 | QByteArray QPacket::squeezedData() const |
123 | { |
124 | QByteArray ret = buf.data(); |
125 | ret.squeeze(); |
126 | return ret; |
127 | } |
128 | |
129 | /*! |
130 | Clears the packet, discarding any data. |
131 | */ |
132 | void QPacket::clear() |
133 | { |
134 | buf.reset(); |
135 | QByteArray &buffer = buf.buffer(); |
136 | // Keep the old size to prevent unnecessary allocations |
137 | buffer.reserve(asize: buffer.capacity()); |
138 | buffer.truncate(pos: 0); |
139 | } |
140 | |
141 | QT_END_NAMESPACE |
142 | |