~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/network/qudpsocket.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the network module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
//#define QUDPSOCKET_DEBUG
 
30
 
 
31
/*! \class QUdpSocket
 
32
 
 
33
    \reentrant
 
34
    \brief The QUdpSocket class provides a UDP socket.
 
35
 
 
36
    \ingroup io
 
37
    \module network
 
38
 
 
39
    UDP (User Datagram Protocol) is a lightweight, unreliable,
 
40
    datagram-oriented, connectionless protocol. It can be used when
 
41
    reliability isn't important. QUdpSocket is a subclass of
 
42
    QAbstractSocket that allows you to send and receive UDP
 
43
    datagrams.
 
44
 
 
45
    The most common way to use this class is to bind to an address
 
46
    and port using bind(), then call writeDatagram() and
 
47
    readDatagram() to transfer data.
 
48
 
 
49
    The socket emits the bytesWritten() signal every time a datagram
 
50
    is written to the network. If you just want to send datagrams,
 
51
    you don't need to call bind().
 
52
 
 
53
    The readyRead() signal is emitted whenever datagrams arrive. In
 
54
    that case, hasPendingDatagrams() returns true. Call
 
55
    pendingDatagramSize() to obtain the size of the first pending
 
56
    datagram, and readDatagram() to read it.
 
57
 
 
58
    Example:
 
59
 
 
60
    \code
 
61
        void Server::initSocket()
 
62
        {
 
63
            udpSocket = new QUdpSocket(this);
 
64
            udpSocket->bind(QHostAddress::LocalHost, 7755);
 
65
 
 
66
            connect(udpSocket, SIGNAL(readyRead()),
 
67
                    this, SLOT(readPendingDatagrams()));
 
68
        }
 
69
 
 
70
        void Server::readPendingDatagrams()
 
71
        {
 
72
            while (udpSocket->hasPendingDatagrams()) {
 
73
                QByteArray datagram;
 
74
                datagram.resize(udpSocket->pendingDatagramSize());
 
75
                QHostAddress sender;
 
76
                quint16 senderPort;
 
77
 
 
78
                udpSocket->readDatagram(datagram.data(), datagram.size(),
 
79
                                        &sender, &senderPort);
 
80
 
 
81
                processTheDatagram(datagram);
 
82
            }
 
83
        }
 
84
    \endcode
 
85
 
 
86
    With QUdpSocket, you can also establish a virtual connection to a
 
87
    UDP server using connectToHost() and then use read() and write()
 
88
    to exchange datagrams without specifying the receiver for each
 
89
    datagram.
 
90
 
 
91
    The \l{network/broadcastsender}{Broadcast Sender} and
 
92
    \l{network/broadcastreceiver}{Broadcast Receiver} examples
 
93
    illustrate how to use QUdpSocket in applications.
 
94
 
 
95
    \sa QTcpSocket
 
96
*/
 
97
#include "qhostaddress.h"
 
98
#include "qabstractsocket_p.h"
 
99
#include "qudpsocket.h"
 
100
 
 
101
#if defined(QT_NO_IPV6)
 
102
#define QT_ENSURE_INITIALIZED(a) do { \
 
103
    QAbstractSocket::NetworkLayerProtocol proto = address.protocol(); \
 
104
    if (proto == QUdpSocket::IPv6Protocol) { \
 
105
        d_func()->socketError = QUdpSocket::UnsupportedSocketOperationError; \
 
106
        setErrorString(QT_TRANSLATE_NOOP("QUdpSocket", "This platform does not support IPv6")); \
 
107
        return (a); \
 
108
    } \
 
109
    if (!d_func()->socketLayer.isValid() || d_func()->socketLayer.protocol() != proto) \
 
110
        if (!d_func()->initSocketLayer(QUdpSocket::UdpSocket, proto)) \
 
111
            return (a); \
 
112
    } while (0)
 
113
#else
 
114
#define QT_ENSURE_INITIALIZED(a) do { \
 
115
    QAbstractSocket::NetworkLayerProtocol proto = address.protocol(); \
 
116
    if (!d_func()->socketLayer.isValid() || d_func()->socketLayer.protocol() != proto) \
 
117
        if (!d_func()->initSocketLayer(QUdpSocket::UdpSocket, proto)) \
 
118
            return (a); \
 
119
    } while (0)
 
120
#endif
 
121
#define QT_CHECK_BOUND(function, a) do { \
 
122
    if (!isValid()) { \
 
123
        qWarning(function" called on a QUdpSocket when not in QUdpSocket::BoundState"); \
 
124
        return (a); \
 
125
    } } while (0)
 
126
 
 
127
class QUdpSocketPrivate : public QAbstractSocketPrivate
 
128
{
 
129
    Q_DECLARE_PUBLIC(QUdpSocket)
 
130
};
 
131
 
 
132
/*!
 
133
    Creates a QUdpSocket object.
 
134
 
 
135
    \a parent is passed to the QObject constructor.
 
136
 
 
137
    \sa socketType()
 
138
*/
 
139
QUdpSocket::QUdpSocket(QObject *parent)
 
140
    : QAbstractSocket(UdpSocket, *new QUdpSocketPrivate, parent)
 
141
{
 
142
    d_func()->isBuffered = false;
 
143
}
 
144
 
 
145
/*!
 
146
    Destroys the socket, closing the connection if necessary.
 
147
 
 
148
    \sa close()
 
149
*/
 
150
QUdpSocket::~QUdpSocket()
 
151
{
 
152
}
 
153
 
 
154
/*!
 
155
    Binds this socket to the address \a address and the port \a port.
 
156
    When bound, the signal readyRead() is emitted whenever a UDP
 
157
    datagram arrives on the specified address and port. This function
 
158
    is useful to write UDP servers.
 
159
 
 
160
    On success, the functions returns true and the socket enters
 
161
    BoundState; otherwise it returns false.
 
162
 
 
163
    \sa readDatagram()
 
164
*/
 
165
bool QUdpSocket::bind(const QHostAddress &address, quint16 port)
 
166
{
 
167
    QT_ENSURE_INITIALIZED(false);
 
168
 
 
169
    bool result = d_func()->socketLayer.bind(address, port);
 
170
    if (!result) {
 
171
        d_func()->socketError = d_func()->socketLayer.error();
 
172
        setErrorString(d_func()->socketLayer.errorString());
 
173
        emit error(d_func()->socketError);
 
174
        return false;
 
175
    }
 
176
 
 
177
    d_func()->state = BoundState;
 
178
    emit stateChanged(d_func()->state);
 
179
    d_func()->readSocketNotifier->setEnabled(true);
 
180
    return true;
 
181
}
 
182
 
 
183
/*! \overload
 
184
 
 
185
    Binds to QHostAddress:Any on port \a port.
 
186
*/
 
187
bool QUdpSocket::bind(quint16 port)
 
188
{
 
189
    return bind(QHostAddress::Any, port);
 
190
}
 
191
 
 
192
/*!
 
193
    Returns true if at least one datagram is waiting to be read;
 
194
    otherwise returns false.
 
195
 
 
196
    \sa pendingDatagramSize(), readDatagram()
 
197
*/
 
198
bool QUdpSocket::hasPendingDatagrams() const
 
199
{
 
200
    QT_CHECK_BOUND("QUdpSocket::hasPendingDatagrams()", false);
 
201
    return d_func()->socketLayer.hasPendingDatagrams();
 
202
}
 
203
 
 
204
/*!
 
205
    Returns the size of the first pending UDP datagram. If there is
 
206
    no datagram available, this function returns -1.
 
207
 
 
208
    \sa hasPendingDatagrams(), readDatagram()
 
209
*/
 
210
qint64 QUdpSocket::pendingDatagramSize() const
 
211
{
 
212
    QT_CHECK_BOUND("QUdpSocket::pendingDatagramSize()", -1);
 
213
    return d_func()->socketLayer.pendingDatagramSize();
 
214
}
 
215
 
 
216
/*!
 
217
    Sends the datagram at \a data of size \a size to the host
 
218
    address \a address at port \a port. Returns the number of
 
219
    bytes sent on success; otherwise returns -1.
 
220
 
 
221
    Datagrams are always written as one block. The maximum size of a
 
222
    datagram is highly platform-dependent, but can be as low as 8192
 
223
    bytes. If the datagram is too large, this function will return -1
 
224
    and error() will return DatagramTooLargeError.
 
225
 
 
226
    Sending datagrams larger than 512 bytes is in general disadvised,
 
227
    as even if they are sent successfully, they are likely to be
 
228
    fragmented by the IP layer before arriving at their final
 
229
    destination.
 
230
 
 
231
    \sa readDatagram()
 
232
*/
 
233
qint64 QUdpSocket::writeDatagram(const char *data, qint64 size, const QHostAddress &address,
 
234
                                  quint16 port)
 
235
{
 
236
    Q_D(QUdpSocket);
 
237
#if defined QUDPSOCKET_DEBUG
 
238
    qDebug("QUdpSocket::writeDatagram(%p, %llu, \"%s\", %i)", data, size,
 
239
           address.toString().toLatin1().constData(), port);
 
240
#endif
 
241
    QT_ENSURE_INITIALIZED(-1);
 
242
    qint64 sent = d->socketLayer.writeDatagram(data, size, address, port);
 
243
    if (sent >= 0) {
 
244
        emit bytesWritten(sent);
 
245
    } else {
 
246
        d->socketError = d->socketLayer.error();
 
247
        setErrorString(d->socketLayer.errorString());
 
248
        emit error(d->socketError);
 
249
    }
 
250
    return sent;
 
251
}
 
252
 
 
253
/*! \fn qint64 QUdpSocket::writeDatagram(const QByteArray &datagram,
 
254
                                             const QHostAddress &host, quint16 port)
 
255
    \overload
 
256
 
 
257
    Sends the datagram \a datagram to the host address \a host and at
 
258
    port \a port.
 
259
*/
 
260
 
 
261
/*!
 
262
    Receives a datagram no larger than \a maxSize bytes and stores
 
263
    it in \a data. The sender's host address and port is stored in
 
264
    *\a address and *\a port (unless the pointers are 0).
 
265
 
 
266
    Returns the size of the datagram on success; otherwise returns
 
267
    -1.
 
268
 
 
269
    If \a maxSize is too small, the rest of the datagram will be
 
270
    lost. To avoid loss of data, call pendingDatagramSize() to
 
271
    determine the size of the pending datagram before attempting to
 
272
    read it. If \a maxSize is 0, the datagram will be discarded.
 
273
 
 
274
    \sa writeDatagram(), hasPendingDatagrams(), pendingDatagramSize()
 
275
*/
 
276
qint64 QUdpSocket::readDatagram(char *data, qint64 maxSize, QHostAddress *address,
 
277
                                    quint16 *port)
 
278
{
 
279
    Q_D(QUdpSocket);
 
280
 
 
281
#if defined QUDPSOCKET_DEBUG
 
282
    qDebug("QUdpSocket::readDatagram(%p, %llu, %p, %p)", data, maxSize, address, port);
 
283
#endif
 
284
    QT_CHECK_BOUND("QUdpSocket::readDatagram()", -1);
 
285
    qint64 readBytes = d->socketLayer.readDatagram(data, maxSize, address, port);
 
286
    if (readBytes < 0) {
 
287
        d->socketError = d->socketLayer.error();
 
288
        setErrorString(d->socketLayer.errorString());
 
289
        emit error(d->socketError);
 
290
    }
 
291
    d->readSocketNotifier->setEnabled(true);
 
292
    return readBytes;
 
293
}