Qt 4.8
Public Functions | Protected Functions | Protected Variables | Friends | List of all members
QPacket Class Reference

The QPacket class encapsulates an unfragmentable packet of data to be transmitted by QPacketProtocol. More...

#include <qpacketprotocol_p.h>

Inheritance diagram for QPacket:
QDataStream QPacketAutoSend

Public Functions

void clear ()
 Clears data in the packet. More...
 
QByteArray data () const
 Returns raw packet data. More...
 
bool isEmpty () const
 Returns true if this packet is empty - that is, contains no data. More...
 
 QPacket ()
 Constructs an empty write-only packet. More...
 
 QPacket (const QPacket &)
 Creates a copy of other. More...
 
virtual ~QPacket ()
 Destroys the QPacket instance. More...
 
- Public Functions inherited from QDataStream
bool atEnd () const
 Returns true if the I/O device has reached the end position (end of the stream or file) or if there is no I/O device set; otherwise returns false. More...
 
ByteOrder byteOrder () const
 Returns the current byte order setting – either BigEndian or LittleEndian. More...
 
QIODevicedevice () const
 Returns the I/O device currently set, or 0 if no device is currently set. More...
 
FloatingPointPrecision floatingPointPrecision () const
 Returns the floating point precision of the data stream. More...
 
QDataStreamoperator<< (qint8 i)
 Writes a signed byte, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (quint8 i)
 Writes an unsigned byte, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (qint16 i)
 Writes a signed 16-bit integer, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (quint16 i)
 Writes an unsigned 16-bit integer, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (qint32 i)
 Writes a signed 32-bit integer, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (quint32 i)
 Writes an unsigned integer, i, to the stream as a 32-bit unsigned integer (quint32). More...
 
QDataStreamoperator<< (qint64 i)
 Writes a signed 64-bit integer, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (quint64 i)
 Writes an unsigned 64-bit integer, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (bool i)
 Writes a boolean value, i, to the stream. More...
 
QDataStreamoperator<< (float f)
 Writes a floating point number, f, to the stream using the standard IEEE 754 format. More...
 
QDataStreamoperator<< (double f)
 Writes a floating point number, f, to the stream using the standard IEEE 754 format. More...
 
QDataStreamoperator<< (const char *str)
 Writes the '\0'-terminated string s to the stream and returns a reference to the stream. More...
 
QDataStreamoperator>> (qint8 &i)
 Reads a signed byte from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (quint8 &i)
 Reads an unsigned byte from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (qint16 &i)
 Reads a signed 16-bit integer from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (quint16 &i)
 Reads an unsigned 16-bit integer from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (qint32 &i)
 Reads a signed 32-bit integer from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (quint32 &i)
 Reads an unsigned 32-bit integer from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (qint64 &i)
 Reads a signed 64-bit integer from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (quint64 &i)
 Reads an unsigned 64-bit integer from the stream, into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (bool &i)
 Reads a boolean value from the stream into i. More...
 
QDataStreamoperator>> (float &f)
 Reads a floating point number from the stream into f, using the standard IEEE 754 format. More...
 
QDataStreamoperator>> (double &f)
 Reads a floating point number from the stream into f, using the standard IEEE 754 format. More...
 
QDataStreamoperator>> (char *&str)
 Reads the '\0'-terminated string s from the stream and returns a reference to the stream. More...
 
 QDataStream ()
 Constructs a data stream that has no I/O device. More...
 
 QDataStream (QIODevice *)
 Constructs a data stream that uses the I/O device d. More...
 
 QDataStream (QByteArray *, QIODevice::OpenMode flags)
 Constructs a data stream that operates on a byte array, a. More...
 
 QDataStream (const QByteArray &)
 Constructs a read-only data stream that operates on byte array a. More...
 
QDataStreamreadBytes (char *&, uint &len)
 Reads the buffer s from the stream and returns a reference to the stream. More...
 
int readRawData (char *, int len)
 Reads at most len bytes from the stream into s and returns the number of bytes read. More...
 
void resetStatus ()
 Resets the status of the data stream. More...
 
void setByteOrder (ByteOrder)
 Sets the serialization byte order to bo. More...
 
void setDevice (QIODevice *)
 void QDataStream::setDevice(QIODevice *d) More...
 
void setFloatingPointPrecision (FloatingPointPrecision precision)
 Sets the floating point precision of the data stream to precision. More...
 
void setStatus (Status status)
 Sets the status of the data stream to the status given. More...
 
void setVersion (int)
 Sets the version number of the data serialization format to v. More...
 
int skipRawData (int len)
 Skips len bytes from the device. More...
 
Status status () const
 Returns the status of the data stream. More...
 
void unsetDevice ()
 Unsets the I/O device. More...
 
int version () const
 Returns the version number of the data serialization format. More...
 
QDataStreamwriteBytes (const char *, uint len)
 Writes the length specifier len and the buffer s to the stream and returns a reference to the stream. More...
 
int writeRawData (const char *, int len)
 Writes len bytes from s to the stream. More...
 
virtual ~QDataStream ()
 Destroys the data stream. More...
 

Protected Functions

 QPacket (const QByteArray &ba)
 

Protected Variables

QByteArray b
 
QBufferbuf
 

Friends

class QPacketProtocol
 

Additional Inherited Members

- Public Types inherited from QDataStream
enum  ByteOrder { BigEndian = QSysInfo::BigEndian, LittleEndian = QSysInfo::LittleEndian }
 The byte order used for reading/writing the data. More...
 
enum  FloatingPointPrecision { SinglePrecision, DoublePrecision }
 The precision of floating point numbers used for reading/writing the data. More...
 
enum  Status { Ok, ReadPastEnd, ReadCorruptData, WriteFailed }
 This enum describes the current status of the data stream. More...
 
enum  Version {
  Qt_1_0 = 1, Qt_2_0 = 2, Qt_2_1 = 3, Qt_3_0 = 4,
  Qt_3_1 = 5, Qt_3_3 = 6, Qt_4_0 = 7, Qt_4_1 = Qt_4_0,
  Qt_4_2 = 8, Qt_4_3 = 9, Qt_4_4 = 10, Qt_4_5 = 11,
  Qt_4_6 = 12, Qt_4_7 = Qt_4_6, Qt_4_8 = Qt_4_7
}
 This enum provides symbolic synonyms for the data serialization format version numbers. More...
 

Detailed Description

The QPacket class encapsulates an unfragmentable packet of data to be transmitted by QPacketProtocol.

Warning
This function is not part of the public interface.

The QPacket class works together with QPacketProtocol to make it simple to send arbitrary sized data "packets" across fragmented transports such as TCP and UDP.

QPacket provides a QDataStream interface to an unfragmentable packet. Applications should construct a QPacket, propagate it with data and then transmit it over a QPacketProtocol instance. For example:

QPacketProtocol protocol(...);
QPacket myPacket;
myPacket << "Hello world!" << 123;
protocol.send(myPacket);

As long as both ends of the connection are using the QPacketProtocol class, the data within this packet will be delivered unfragmented at the other end, ready for extraction.

QByteArray greeting;
int count;
QPacket myPacket = protocol.read();
myPacket >> greeting >> count;

Only packets returned from QPacketProtocol::read() may be read from. QPacket instances constructed by directly by applications are for transmission only and are considered "write only". Attempting to read data from them will result in undefined behavior.

See also
QPacketProtocol

Definition at line 94 of file qpacketprotocol_p.h.

Constructors and Destructors

◆ QPacket() [1/3]

QPacket::QPacket ( )

Constructs an empty write-only packet.

Definition at line 472 of file qpacketprotocol.cpp.

473 : QDataStream(), buf(0)
474 {
475  buf = new QBuffer(&b);
477  setDevice(buf);
479 }
QDataStream()
Constructs a data stream that has no I/O device.
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
void setVersion(int)
Sets the version number of the data serialization format to v.
Definition: qdatastream.h:215
QByteArray b
void setDevice(QIODevice *)
void QDataStream::setDevice(QIODevice *d)
QBuffer * buf

◆ QPacket() [2/3]

QPacket::QPacket ( const QPacket other)

Creates a copy of other.

The initial stream positions are shared, but the two packets are otherwise independent.

Definition at line 496 of file qpacketprotocol.cpp.

497 : QDataStream(), b(other.b), buf(0)
498 {
499  buf = new QBuffer(&b);
500  buf->open(other.buf->openMode());
501  setDevice(buf);
502 }
QDataStream()
Constructs a data stream that has no I/O device.
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
OpenMode openMode() const
Returns the mode in which the device has been opened; i.e.
Definition: qiodevice.cpp:465
QByteArray b
void setDevice(QIODevice *)
void QDataStream::setDevice(QIODevice *d)
QBuffer * buf

◆ ~QPacket()

QPacket::~QPacket ( )
virtual

Destroys the QPacket instance.

Definition at line 484 of file qpacketprotocol.cpp.

485 {
486  if(buf) {
487  delete buf;
488  buf = 0;
489  }
490 }
QBuffer * buf

◆ QPacket() [3/3]

QPacket::QPacket ( const QByteArray ba)
protected
Warning
This function is not part of the public interface.

Definition at line 507 of file qpacketprotocol.cpp.

508 : QDataStream(), b(ba), buf(0)
509 {
510  buf = new QBuffer(&b);
512  setDevice(buf);
513 }
QDataStream()
Constructs a data stream that has no I/O device.
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
QByteArray b
void setDevice(QIODevice *)
void QDataStream::setDevice(QIODevice *d)
QBuffer * buf

Functions

◆ clear()

void QPacket::clear ( )

Clears data in the packet.

This is useful for reusing one writable packet. For example

QPacketProtocol protocol(...);
QPacket packet;
packet << "Hello world!" << 123;
protocol.send(packet);
packet.clear();
packet << "Goodbyte world!" << 789;
protocol.send(packet);

Definition at line 547 of file qpacketprotocol.cpp.

548 {
549  QBuffer::OpenMode oldMode = buf->openMode();
550  buf->close();
551  b.clear();
552  buf->setBuffer(&b); // reset QBuffer internals with new size of b.
553  buf->open(oldMode);
554 }
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
void close()
Reimplemented Function
Definition: qbuffer.cpp:359
void setBuffer(QByteArray *a)
Makes QBuffer uses the QByteArray pointed to by byteArray as its internal buffer. ...
Definition: qbuffer.cpp:248
OpenMode openMode() const
Returns the mode in which the device has been opened; i.e.
Definition: qiodevice.cpp:465
QByteArray b
QBuffer * buf
void clear()
Clears the contents of the byte array and makes it empty.

◆ data()

QByteArray QPacket::data ( ) const

Returns raw packet data.

Definition at line 526 of file qpacketprotocol.cpp.

Referenced by QmlOstPlugin::readyRead(), QTcpServerConnection::readyRead(), and QTcpServerConnection::waitForMessage().

527 {
528  return b;
529 }
QByteArray b

◆ isEmpty()

bool QPacket::isEmpty ( ) const

Returns true if this packet is empty - that is, contains no data.

Definition at line 518 of file qpacketprotocol.cpp.

519 {
520  return b.isEmpty();
521 }
QByteArray b
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421

Friends and Related Functions

◆ QPacketProtocol

friend class QPacketProtocol
friend

Definition at line 106 of file qpacketprotocol_p.h.

Properties

◆ b

QByteArray QPacket::b
protected

◆ buf

QBuffer* QPacket::buf
protected

Definition at line 109 of file qpacketprotocol_p.h.

Referenced by clear(), QPacket(), and ~QPacket().


The documentation for this class was generated from the following files: