summaryrefslogtreecommitdiffstats
path: root/src/network/socket/qudpsocket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/network/socket/qudpsocket.cpp')
-rw-r--r--src/network/socket/qudpsocket.cpp108
1 files changed, 102 insertions, 6 deletions
diff --git a/src/network/socket/qudpsocket.cpp b/src/network/socket/qudpsocket.cpp
index 083648bc23..d78379fb39 100644
--- a/src/network/socket/qudpsocket.cpp
+++ b/src/network/socket/qudpsocket.cpp
@@ -55,10 +55,10 @@
datagrams.
The most common way to use this class is to bind to an address and port
- using bind(), then call writeDatagram() and readDatagram() to transfer
- data. If you want to use the standard QIODevice functions read(),
- readLine(), write(), etc., you must first connect the socket directly to a
- peer by calling connectToHost().
+ using bind(), then call writeDatagram() and readDatagram() /
+ receiveDatagram() to transfer data. If you want to use the standard
+ QIODevice functions read(), readLine(), write(), etc., you must first
+ connect the socket directly to a peer by calling connectToHost().
The socket emits the bytesWritten() signal every time a datagram
is written to the network. If you just want to send datagrams,
@@ -67,7 +67,7 @@
The readyRead() signal is emitted whenever datagrams arrive. In
that case, hasPendingDatagrams() returns \c true. Call
pendingDatagramSize() to obtain the size of the first pending
- datagram, and readDatagram() to read it.
+ datagram, and readDatagram() or receiveDatagram() to read it.
\note An incoming datagram should be read when you receive the readyRead()
signal, otherwise this signal will not be emitted for the next datagram.
@@ -94,11 +94,12 @@
\l{multicastreceiver}{Multicast Receiver} examples illustrate how
to use QUdpSocket in applications.
- \sa QTcpSocket
+ \sa QTcpSocket, QNetworkDatagram
*/
#include "qudpsocket.h"
#include "qhostaddress.h"
+#include "qnetworkdatagram.h"
#include "qnetworkinterface.h"
#include "qabstractsocket_p.h"
@@ -358,7 +359,101 @@ qint64 QUdpSocket::writeDatagram(const char *data, qint64 size, const QHostAddre
Sends the datagram \a datagram to the host address \a host and at
port \a port.
+
+ The function returns the number of bytes sent if it succeeded or -1 if it
+ encountered an error.
+*/
+
+/*!
+ \overload
+
+ Sends the datagram \a datagram to the host address and port numbers
+ contained in \a datagram, using the network interface and hop count limits
+ also set there. If the destination address and port numbers are unset, this
+ function will send to the address that was passed to connectToHost().
+
+ If the destination address is IPv6 with a non-empty
+ \l{QHostAddress::scopeId()}{scope id} but differs from the interface index
+ in \a datagram, it is undefined which interface the operating system will
+ choose to send on.
+
+ The function returns the number of bytes sent if it succeeded or -1 if it
+ encountered an error.
+
+ \warning Calling this function on a connected UDP socket may
+ result in an error and no packet being sent. If you are using a
+ connected socket, use write() to send datagrams.
+
+ \sa QNetworkDatagram::setDestination(), QNetworkDatagram::setHopLimit(), QNetworkDatagram::setInterfaceIndex()
+*/
+qint64 QUdpSocket::writeDatagram(const QNetworkDatagram &datagram)
+{
+ Q_D(QUdpSocket);
+#if defined QUDPSOCKET_DEBUG
+ qDebug("QUdpSocket::writeDatagram(%p, %i, \"%s\", %i)",
+ datagram.d->data.constData(),
+ datagram.d->data.size(),
+ datagram.destinationAddress().toString().toLatin1().constData(),
+ datagram.destinationPort());
+#endif
+ if (!d->doEnsureInitialized(QHostAddress::Any, 0, datagram.destinationAddress()))
+ return -1;
+ if (state() == UnconnectedState)
+ bind();
+
+ qint64 sent = d->socketEngine->writeDatagram(datagram.d->data,
+ datagram.d->data.size(),
+ datagram.d->header);
+ d->cachedSocketDescriptor = d->socketEngine->socketDescriptor();
+
+ if (sent >= 0) {
+ emit bytesWritten(sent);
+ } else {
+ d->setErrorAndEmit(d->socketEngine->error(), d->socketEngine->errorString());
+ }
+ return sent;
+}
+
+/*!
+ Receives a datagram no larger than \a maxSize bytes and returns it in the
+ QNetworkDatagram object, along with the sender's host address and port. If
+ possible, this function will also try to determine the datagram's
+ destination address, port, and the number of hop counts at reception time.
+
+ On failure, returns a QNetworkDatagram that reports \l
+ {QNetworkDatagram::isValid()}{not valid}.
+
+ If \a maxSize is too small, the rest of the datagram will be lost. If \a
+ maxSize is 0, the datagram will be discarded. If \a maxSize is -1 (the
+ default), this function will attempt to read the entire datagram.
+
+ \sa writeDatagram(), hasPendingDatagrams(), pendingDatagramSize()
*/
+QNetworkDatagram QUdpSocket::receiveDatagram(qint64 maxSize)
+{
+ Q_D(QUdpSocket);
+
+#if defined QUDPSOCKET_DEBUG
+ qDebug("QUdpSocket::receiveDatagram(%lld)", maxSize);
+#endif
+ QT_CHECK_BOUND("QUdpSocket::receiveDatagram()", QNetworkDatagram());
+
+ if (maxSize < 0)
+ maxSize = d->socketEngine->pendingDatagramSize();
+ if (maxSize < 0)
+ return QNetworkDatagram();
+
+ QNetworkDatagram result(QByteArray(maxSize, Qt::Uninitialized));
+ qint64 readBytes = d->socketEngine->readDatagram(result.d->data.data(), maxSize, &result.d->header,
+ QAbstractSocketEngine::WantAll);
+ d->hasPendingData = false;
+ d->socketEngine->setReadNotificationEnabled(true);
+ if (readBytes < 0)
+ d->setErrorAndEmit(d->socketEngine->error(), d->socketEngine->errorString());
+ else if (readBytes != result.d->data.size())
+ result.d->data.truncate(readBytes);
+ return result;
+}
/*!
Receives a datagram no larger than \a maxSize bytes and stores
@@ -404,6 +499,7 @@ qint64 QUdpSocket::readDatagram(char *data, qint64 maxSize, QHostAddress *addres
d->setErrorAndEmit(d->socketEngine->error(), d->socketEngine->errorString());
return readBytes;
}
+
#endif // QT_NO_UDPSOCKET
QT_END_NAMESPACE