Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpacket.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant
4
5
#
include
"qpacket_p.h"
6
7
QT_BEGIN_NAMESPACE
8
9
/*!
10
\class QPacket
11
\internal
12
13
\brief The QPacket class encapsulates an unfragmentable packet of data to be
14
transmitted by QPacketProtocol.
15
16
The QPacket class works together with QPacketProtocol to make it simple to
17
send arbitrary sized data "packets" across fragmented transports such as TCP
18
and UDP.
19
20
QPacket provides a QDataStream interface to an unfragmentable packet.
21
Applications should construct a QPacket, propagate it with data and then
22
transmit it over a QPacketProtocol instance. For example:
23
\code
24
int version = QDataStream::Qt_DefaultCompiledVersion;
25
QPacketProtocol protocol(...);
26
27
QPacket myPacket(version);
28
myPacket << "Hello world!" << 123;
29
protocol.send(myPacket.data());
30
\endcode
31
32
As long as both ends of the connection are using the QPacketProtocol class
33
and the same data stream version, the data within this packet will be
34
delivered unfragmented at the other end, ready for extraction.
35
36
\code
37
QByteArray greeting;
38
int count;
39
40
QPacket myPacket(version, protocol.read());
41
42
myPacket >> greeting >> count;
43
\endcode
44
45
Only packets constructed from raw byte arrays may be read from. Empty QPacket
46
instances are for transmission only and are considered "write only". Attempting
47
to read data from them will result in undefined behavior.
48
49
\ingroup io
50
\sa QPacketProtocol
51
*/
52
53
54
/*!
55
Constructs an empty write-only packet.
56
*/
57
QPacket::QPacket(
int
version)
58
{
59
buf.open(QIODevice::WriteOnly);
60
setDevice(&buf);
61
setVersion(version);
62
}
63
64
/*!
65
Constructs a read-only packet.
66
*/
67
QPacket::QPacket(
int
version,
const
QByteArray &data)
68
{
69
buf.setData(data);
70
buf.open(QIODevice::ReadOnly);
71
setDevice(&buf);
72
setVersion(version);
73
}
74
75
/*!
76
Returns a reference to the raw packet data.
77
*/
78
const
QByteArray &QPacket::data()
const
79
{
80
return
buf.data();
81
}
82
83
/*!
84
Returns a copy of the raw packet data, with extra reserved space removed.
85
Mind that this triggers a deep copy. Use it if you anticipate the data to be detached soon anyway.
86
*/
87
QByteArray QPacket::squeezedData()
const
88
{
89
QByteArray ret = buf.data();
90
ret.squeeze();
91
return
ret;
92
}
93
94
/*!
95
Clears the packet, discarding any data.
96
*/
97
void
QPacket::clear()
98
{
99
buf.reset();
100
QByteArray &buffer = buf.buffer();
101
// Keep the old size to prevent unnecessary allocations
102
buffer.reserve(buffer.capacity());
103
buffer.truncate(0);
104
}
105
106
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qrandomaccessasyncfile_darwin.mm:17
qtdeclarative
src
plugins
qmltooling
packetprotocol
qpacket.cpp
Generated on
for Qt by
1.14.0