From 89efa7333db4b5877cb58b16e511b690fe43507c Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 10 Mar 2015 15:01:21 -0700 Subject: QAbstractSocketEngine: introduce QIpPacketHeader for datagrams This commit changes the readDatagram() and writeDatagram() virtual functions to take a QIpPacketHeader as meta data, instead of a QHostAddress/quint16 pair. As previously, the header is an "out" parameter for readDatagram() and an "in" parameter for writeDatagram(). The header pointer in readDatagram() is allowed to be null if the PacketHeaderOptions indicates WantNone. Otherwise, it must not be null. The extra options parameter is introduced because we may not always want all the metadata upon reception. For sending, we know what to include or not based on what's set in the incoming header parameter. QIpPacketHeader splits sender and destination because we'll be able to return both on datagram reception. Change-Id: Iee8cbc07c4434ce9b560ffff13ca4213255008c7 Reviewed-by: Richard J. Moore --- src/network/socket/qudpsocket.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/network/socket/qudpsocket.cpp') diff --git a/src/network/socket/qudpsocket.cpp b/src/network/socket/qudpsocket.cpp index 87686f94e1..f4e7d20b03 100644 --- a/src/network/socket/qudpsocket.cpp +++ b/src/network/socket/qudpsocket.cpp @@ -333,7 +333,7 @@ qint64 QUdpSocket::writeDatagram(const char *data, qint64 size, const QHostAddre if (state() == UnconnectedState) bind(); - qint64 sent = d->socketEngine->writeDatagram(data, size, address, port); + qint64 sent = d->socketEngine->writeDatagram(data, size, QIpPacketHeader(address, port)); d->cachedSocketDescriptor = d->socketEngine->socketDescriptor(); if (sent >= 0) { @@ -379,7 +379,20 @@ qint64 QUdpSocket::readDatagram(char *data, qint64 maxSize, QHostAddress *addres qDebug("QUdpSocket::readDatagram(%p, %llu, %p, %p)", data, maxSize, address, port); #endif QT_CHECK_BOUND("QUdpSocket::readDatagram()", -1); - qint64 readBytes = d->socketEngine->readDatagram(data, maxSize, address, port); + + qint64 readBytes; + if (address || port) { + QIpPacketHeader header; + readBytes = d->socketEngine->readDatagram(data, maxSize, &header, + QAbstractSocketEngine::WantDatagramSender); + if (address) + *address = header.senderAddress; + if (port) + *port = header.senderPort; + } else { + readBytes = d->socketEngine->readDatagram(data, maxSize); + } + d_func()->socketEngine->setReadNotificationEnabled(true); if (readBytes < 0) { d->socketError = d->socketEngine->error(); -- cgit v1.2.3