source: trunk/src/corelib/kernel/qsocketnotifier.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 10.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qsocketnotifier.h"
43
44#include "qplatformdefs.h"
45
46#include "qabstracteventdispatcher.h"
47#include "qcoreapplication.h"
48
49#include "qobject_p.h"
50#include <private/qthread_p.h>
51
52QT_BEGIN_NAMESPACE
53
54/*!
55 \class QSocketNotifier
56 \brief The QSocketNotifier class provides support for monitoring
57 activity on a file descriptor.
58
59 \ingroup io
60
61 The QSocketNotifier makes it possible to integrate Qt's event
62 loop with other event loops based on file descriptors. For
63 example, the \l{CORBA Framework} uses it to process CORBA
64 events. File descriptor action is detected in Qt's main event
65 loop (QCoreApplication::exec()).
66
67 \target write notifiers
68
69 Once you have opened a device using a low-level (usually
70 platform-specific) API, you can create a socket notifier to
71 monitor the file descriptor. The socket notifier is enabled by
72 default, i.e. it emits the activated() signal whenever a socket
73 event corresponding to its type occurs. Connect the activated()
74 signal to the slot you want to be called when an event
75 corresponding to your socket notifier's type occurs.
76
77 There are three types of socket notifiers: read, write, and
78 exception. The type is described by the \l Type enum, and must be
79 specified when constructing the socket notifier. After
80 construction it can be determined using the type() function. Note
81 that if you need to monitor both reads and writes for the same
82 file descriptor, you must create two socket notifiers. Note also
83 that it is not possible to install two socket notifiers of the
84 same type (\l Read, \l Write, \l Exception) on the same socket.
85
86 The setEnabled() function allows you to disable as well as enable
87 the socket notifier. It is generally advisable to explicitly
88 enable or disable the socket notifier, especially for write
89 notifiers. A disabled notifier ignores socket events (the same
90 effect as not creating the socket notifier). Use the isEnabled()
91 function to determine the notifier's current status.
92
93 Finally, you can use the socket() function to retrieve the
94 socket identifier. Although the class is called QSocketNotifier,
95 it is normally used for other types of devices than sockets.
96 QTcpSocket and QUdpSocket provide notification through signals, so
97 there is normally no need to use a QSocketNotifier on them.
98
99 \section1 Notes for Windows Users
100
101 The socket passed to QSocketNotifier will become non-blocking, even if
102 it was created as a blocking socket.
103 The activated() signal is sometimes triggered by high general activity
104 on the host, even if there is nothing to read. A subsequent read from
105 the socket can then fail, the error indicating that there is no data
106 available (e.g., \c{WSAEWOULDBLOCK}). This is an operating system
107 limitation, and not a bug in QSocketNotifier.
108
109 To ensure that the socket notifier handles read notifications correctly,
110 follow these steps when you receive a notification:
111
112 \list 1
113 \o Disable the notifier.
114 \o Read data from the socket.
115 \o Re-enable the notifier if you are interested in more data (such as after
116 having written a new command to a remote server).
117 \endlist
118
119 To ensure that the socket notifier handles write notifications correctly,
120 follow these steps when you receive a notification:
121
122 \list 1
123 \o Disable the notifier.
124 \o Write as much data as you can (before \c EWOULDBLOCK is returned).
125 \o Re-enable notifier if you have more data to write.
126 \endlist
127
128 \bold{Further information:}
129 On Windows, Qt always disables the notifier after getting a notification,
130 and only re-enables it if more data is expected. For example, if data is
131 read from the socket and it can be used to read more, or if reading or
132 writing is not possible because the socket would block, in which case
133 it is necessary to wait before attempting to read or write again.
134
135 \sa QFile, QProcess, QTcpSocket, QUdpSocket
136*/
137
138/*!
139 \enum QSocketNotifier::Type
140
141 This enum describes the various types of events that a socket
142 notifier can recognize. The type must be specified when
143 constructing the socket notifier.
144
145 Note that if you need to monitor both reads and writes for the
146 same file descriptor, you must create two socket notifiers. Note
147 also that it is not possible to install two socket notifiers of
148 the same type (Read, Write, Exception) on the same socket.
149
150 \value Read There is data to be read.
151 \value Write Data can be written.
152 \value Exception An exception has occurred. We recommend against using this.
153
154 \sa QSocketNotifier(), type()
155*/
156
157/*!
158 Constructs a socket notifier with the given \a parent. It enables
159 the \a socket, and watches for events of the given \a type.
160
161 It is generally advisable to explicitly enable or disable the
162 socket notifier, especially for write notifiers.
163
164 \bold{Note for Windows users:} The socket passed to QSocketNotifier
165 will become non-blocking, even if it was created as a blocking socket.
166
167 \sa setEnabled(), isEnabled()
168*/
169
170QSocketNotifier::QSocketNotifier(int socket, Type type, QObject *parent)
171 : QObject(parent)
172{
173 if (socket < 0)
174 qWarning("QSocketNotifier: Invalid socket specified");
175 sockfd = socket;
176 sntype = type;
177 snenabled = true;
178
179 Q_D(QObject);
180 if (!d->threadData->eventDispatcher) {
181 qWarning("QSocketNotifier: Can only be used with threads started with QThread");
182 } else {
183 d->threadData->eventDispatcher->registerSocketNotifier(this);
184 }
185}
186
187#ifdef QT3_SUPPORT
188/*!
189 \obsolete
190
191 Use the QSocketNotifier() constructor combined with the
192 QObject::setObjectName() function instead.
193
194 \oldcode
195 QSocketNotifier *notifier = new QSocketNotifier(socket, type, parent, name);
196 \newcode
197 QSocketNotifier *notifier = new QSocketNotifier(socket, type, parent);
198 notifier->setObjectName(name);
199 \endcode
200*/
201
202QSocketNotifier::QSocketNotifier(int socket, Type type, QObject *parent,
203 const char *name)
204 : QObject(parent)
205{
206 setObjectName(QString::fromAscii(name));
207 if (socket < 0)
208 qWarning("QSocketNotifier: Invalid socket specified");
209 sockfd = socket;
210 sntype = type;
211 snenabled = true;
212
213 Q_D(QObject);
214 if (!d->threadData->eventDispatcher) {
215 qWarning("QSocketNotifier: Can only be used with threads started with QThread");
216 } else {
217 d->threadData->eventDispatcher->registerSocketNotifier(this);
218 }
219}
220#endif
221/*!
222 Destroys this socket notifier.
223*/
224
225QSocketNotifier::~QSocketNotifier()
226{
227 setEnabled(false);
228}
229
230
231/*!
232 \fn void QSocketNotifier::activated(int socket)
233
234 This signal is emitted whenever the socket notifier is enabled and
235 a socket event corresponding to its \l {Type}{type} occurs.
236
237 The socket identifier is passed in the \a socket parameter.
238
239 \sa type(), socket()
240*/
241
242
243/*!
244 \fn int QSocketNotifier::socket() const
245
246 Returns the socket identifier specified to the constructor.
247
248 \sa type()
249*/
250
251/*!
252 \fn Type QSocketNotifier::type() const
253
254 Returns the socket event type specified to the constructor.
255
256 \sa socket()
257*/
258
259
260/*!
261 \fn bool QSocketNotifier::isEnabled() const
262
263 Returns true if the notifier is enabled; otherwise returns false.
264
265 \sa setEnabled()
266*/
267
268/*!
269 If \a enable is true, the notifier is enabled; otherwise the notifier
270 is disabled.
271
272 The notifier is enabled by default, i.e. it emits the activated()
273 signal whenever a socket event corresponding to its
274 \l{type()}{type} occurs. If it is disabled, it ignores socket
275 events (the same effect as not creating the socket notifier).
276
277 Write notifiers should normally be disabled immediately after the
278 activated() signal has been emitted
279
280 \sa isEnabled(), activated()
281*/
282
283void QSocketNotifier::setEnabled(bool enable)
284{
285 if (sockfd < 0)
286 return;
287 if (snenabled == enable) // no change
288 return;
289 snenabled = enable;
290
291 Q_D(QObject);
292 if (!d->threadData->eventDispatcher) // perhaps application/thread is shutting down
293 return;
294 if (snenabled)
295 d->threadData->eventDispatcher->registerSocketNotifier(this);
296 else
297 d->threadData->eventDispatcher->unregisterSocketNotifier(this);
298}
299
300
301/*!\reimp
302*/
303bool QSocketNotifier::event(QEvent *e)
304{
305 // Emits the activated() signal when a QEvent::SockAct is
306 // received.
307 if (e->type() == QEvent::ThreadChange) {
308 if (snenabled) {
309 QMetaObject::invokeMethod(this, "setEnabled", Qt::QueuedConnection,
310 Q_ARG(bool, snenabled));
311 setEnabled(false);
312 }
313 }
314 QObject::event(e); // will activate filters
315 if (e->type() == QEvent::SockAct) {
316 emit activated(sockfd);
317 return true;
318 }
319 return false;
320}
321
322QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.