| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/kernel/qsocketnotifier.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
| 16 | ** | - | ||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
| 19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
| 25 | ** | - | ||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
| 29 | ** | - | ||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 31 | ** | - | ||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||
| 33 | - | |||||||||||||
| 34 | #include "qsocketnotifier.h" | - | ||||||||||||
| 35 | - | |||||||||||||
| 36 | #include "qplatformdefs.h" | - | ||||||||||||
| 37 | - | |||||||||||||
| 38 | #include "qabstracteventdispatcher.h" | - | ||||||||||||
| 39 | #include "qcoreapplication.h" | - | ||||||||||||
| 40 | - | |||||||||||||
| 41 | #include "qobject_p.h" | - | ||||||||||||
| 42 | #include <private/qthread_p.h> | - | ||||||||||||
| 43 | - | |||||||||||||
| 44 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 45 | - | |||||||||||||
| 46 | class QSocketNotifierPrivate : public QObjectPrivate | - | ||||||||||||
| 47 | { | - | ||||||||||||
| 48 | Q_DECLARE_PUBLIC(QSocketNotifier) | - | ||||||||||||
| 49 | public: | - | ||||||||||||
| 50 | qintptr sockfd; | - | ||||||||||||
| 51 | QSocketNotifier::Type sntype; | - | ||||||||||||
| 52 | bool snenabled; | - | ||||||||||||
| 53 | }; | - | ||||||||||||
| 54 | - | |||||||||||||
| 55 | /*! | - | ||||||||||||
| 56 | \class QSocketNotifier | - | ||||||||||||
| 57 | \inmodule QtCore | - | ||||||||||||
| 58 | \brief The QSocketNotifier class provides support for monitoring | - | ||||||||||||
| 59 | activity on a file descriptor. | - | ||||||||||||
| 60 | - | |||||||||||||
| 61 | \ingroup network | - | ||||||||||||
| 62 | \ingroup io | - | ||||||||||||
| 63 | - | |||||||||||||
| 64 | The QSocketNotifier makes it possible to integrate Qt's event | - | ||||||||||||
| 65 | loop with other event loops based on file descriptors. File | - | ||||||||||||
| 66 | descriptor action is detected in Qt's main event | - | ||||||||||||
| 67 | loop (QCoreApplication::exec()). | - | ||||||||||||
| 68 | - | |||||||||||||
| 69 | \target write notifiers | - | ||||||||||||
| 70 | - | |||||||||||||
| 71 | Once you have opened a device using a low-level (usually | - | ||||||||||||
| 72 | platform-specific) API, you can create a socket notifier to | - | ||||||||||||
| 73 | monitor the file descriptor. The socket notifier is enabled by | - | ||||||||||||
| 74 | default, i.e. it emits the activated() signal whenever a socket | - | ||||||||||||
| 75 | event corresponding to its type occurs. Connect the activated() | - | ||||||||||||
| 76 | signal to the slot you want to be called when an event | - | ||||||||||||
| 77 | corresponding to your socket notifier's type occurs. | - | ||||||||||||
| 78 | - | |||||||||||||
| 79 | There are three types of socket notifiers: read, write, and | - | ||||||||||||
| 80 | exception. The type is described by the \l Type enum, and must be | - | ||||||||||||
| 81 | specified when constructing the socket notifier. After | - | ||||||||||||
| 82 | construction it can be determined using the type() function. Note | - | ||||||||||||
| 83 | that if you need to monitor both reads and writes for the same | - | ||||||||||||
| 84 | file descriptor, you must create two socket notifiers. Note also | - | ||||||||||||
| 85 | that it is not possible to install two socket notifiers of the | - | ||||||||||||
| 86 | same type (\l Read, \l Write, \l Exception) on the same socket. | - | ||||||||||||
| 87 | - | |||||||||||||
| 88 | The setEnabled() function allows you to disable as well as enable | - | ||||||||||||
| 89 | the socket notifier. It is generally advisable to explicitly | - | ||||||||||||
| 90 | enable or disable the socket notifier, especially for write | - | ||||||||||||
| 91 | notifiers. A disabled notifier ignores socket events (the same | - | ||||||||||||
| 92 | effect as not creating the socket notifier). Use the isEnabled() | - | ||||||||||||
| 93 | function to determine the notifier's current status. | - | ||||||||||||
| 94 | - | |||||||||||||
| 95 | Finally, you can use the socket() function to retrieve the | - | ||||||||||||
| 96 | socket identifier. Although the class is called QSocketNotifier, | - | ||||||||||||
| 97 | it is normally used for other types of devices than sockets. | - | ||||||||||||
| 98 | QTcpSocket and QUdpSocket provide notification through signals, so | - | ||||||||||||
| 99 | there is normally no need to use a QSocketNotifier on them. | - | ||||||||||||
| 100 | - | |||||||||||||
| 101 | \sa QFile, QProcess, QTcpSocket, QUdpSocket | - | ||||||||||||
| 102 | */ | - | ||||||||||||
| 103 | - | |||||||||||||
| 104 | /*! | - | ||||||||||||
| 105 | \enum QSocketNotifier::Type | - | ||||||||||||
| 106 | - | |||||||||||||
| 107 | This enum describes the various types of events that a socket | - | ||||||||||||
| 108 | notifier can recognize. The type must be specified when | - | ||||||||||||
| 109 | constructing the socket notifier. | - | ||||||||||||
| 110 | - | |||||||||||||
| 111 | Note that if you need to monitor both reads and writes for the | - | ||||||||||||
| 112 | same file descriptor, you must create two socket notifiers. Note | - | ||||||||||||
| 113 | also that it is not possible to install two socket notifiers of | - | ||||||||||||
| 114 | the same type (Read, Write, Exception) on the same socket. | - | ||||||||||||
| 115 | - | |||||||||||||
| 116 | \value Read There is data to be read. | - | ||||||||||||
| 117 | \value Write Data can be written. | - | ||||||||||||
| 118 | \value Exception An exception has occurred. We recommend against using this. | - | ||||||||||||
| 119 | - | |||||||||||||
| 120 | \sa QSocketNotifier(), type() | - | ||||||||||||
| 121 | */ | - | ||||||||||||
| 122 | - | |||||||||||||
| 123 | /*! | - | ||||||||||||
| 124 | Constructs a socket notifier with the given \a parent. It enables | - | ||||||||||||
| 125 | the \a socket, and watches for events of the given \a type. | - | ||||||||||||
| 126 | - | |||||||||||||
| 127 | It is generally advisable to explicitly enable or disable the | - | ||||||||||||
| 128 | socket notifier, especially for write notifiers. | - | ||||||||||||
| 129 | - | |||||||||||||
| 130 | \b{Note for Windows users:} The socket passed to QSocketNotifier | - | ||||||||||||
| 131 | will become non-blocking, even if it was created as a blocking socket. | - | ||||||||||||
| 132 | - | |||||||||||||
| 133 | \sa setEnabled(), isEnabled() | - | ||||||||||||
| 134 | */ | - | ||||||||||||
| 135 | - | |||||||||||||
| 136 | QSocketNotifier::QSocketNotifier(qintptr socket, Type type, QObject *parent) | - | ||||||||||||
| 137 | : QObject(*new QSocketNotifierPrivate, parent) | - | ||||||||||||
| 138 | { | - | ||||||||||||
| 139 | Q_D(QSocketNotifier); | - | ||||||||||||
| 140 | d->sockfd = socket; | - | ||||||||||||
| 141 | d->sntype = type; | - | ||||||||||||
| 142 | d->snenabled = true; | - | ||||||||||||
| 143 | - | |||||||||||||
| 144 | if (socket < 0)
| 0-23207 | ||||||||||||
| 145 | qWarning("QSocketNotifier: Invalid socket specified"); never executed: QMessageLogger(__FILE__, 145, __PRETTY_FUNCTION__).warning("QSocketNotifier: Invalid socket specified"); | 0 | ||||||||||||
| 146 | else if (!d->threadData->eventDispatcher.load())
| 0-23207 | ||||||||||||
| 147 | qWarning("QSocketNotifier: Can only be used with threads started with QThread"); never executed: QMessageLogger(__FILE__, 147, __PRETTY_FUNCTION__).warning("QSocketNotifier: Can only be used with threads started with QThread"); | 0 | ||||||||||||
| 148 | else | - | ||||||||||||
| 149 | d->threadData->eventDispatcher.load()->registerSocketNotifier(this); executed 23207 times by 202 tests: d->threadData->eventDispatcher.load()->registerSocketNotifier(this);Executed by:
| 23207 | ||||||||||||
| 150 | } | - | ||||||||||||
| 151 | - | |||||||||||||
| 152 | /*! | - | ||||||||||||
| 153 | Destroys this socket notifier. | - | ||||||||||||
| 154 | */ | - | ||||||||||||
| 155 | - | |||||||||||||
| 156 | QSocketNotifier::~QSocketNotifier() | - | ||||||||||||
| 157 | { | - | ||||||||||||
| 158 | setEnabled(false); | - | ||||||||||||
| 159 | } executed 23213 times by 236 tests: end of blockExecuted by:
| 23213 | ||||||||||||
| 160 | - | |||||||||||||
| 161 | - | |||||||||||||
| 162 | /*! | - | ||||||||||||
| 163 | \fn void QSocketNotifier::activated(int socket) | - | ||||||||||||
| 164 | - | |||||||||||||
| 165 | This signal is emitted whenever the socket notifier is enabled and | - | ||||||||||||
| 166 | a socket event corresponding to its \l {Type}{type} occurs. | - | ||||||||||||
| 167 | - | |||||||||||||
| 168 | The socket identifier is passed in the \a socket parameter. | - | ||||||||||||
| 169 | - | |||||||||||||
| 170 | \sa type(), socket() | - | ||||||||||||
| 171 | */ | - | ||||||||||||
| 172 | - | |||||||||||||
| 173 | - | |||||||||||||
| 174 | /*! | - | ||||||||||||
| 175 | Returns the socket identifier specified to the constructor. | - | ||||||||||||
| 176 | - | |||||||||||||
| 177 | \sa type() | - | ||||||||||||
| 178 | */ | - | ||||||||||||
| 179 | qintptr QSocketNotifier::socket() const | - | ||||||||||||
| 180 | { | - | ||||||||||||
| 181 | Q_D(const QSocketNotifier); | - | ||||||||||||
| 182 | return d->sockfd; executed 86290 times by 359 tests: return d->sockfd;Executed by:
| 86290 | ||||||||||||
| 183 | } | - | ||||||||||||
| 184 | - | |||||||||||||
| 185 | /*! | - | ||||||||||||
| 186 | Returns the socket event type specified to the constructor. | - | ||||||||||||
| 187 | - | |||||||||||||
| 188 | \sa socket() | - | ||||||||||||
| 189 | */ | - | ||||||||||||
| 190 | QSocketNotifier::Type QSocketNotifier::type() const | - | ||||||||||||
| 191 | { | - | ||||||||||||
| 192 | Q_D(const QSocketNotifier); | - | ||||||||||||
| 193 | return d->sntype; executed 43144 times by 202 tests: return d->sntype;Executed by:
| 43144 | ||||||||||||
| 194 | } | - | ||||||||||||
| 195 | - | |||||||||||||
| 196 | /*! | - | ||||||||||||
| 197 | Returns \c true if the notifier is enabled; otherwise returns \c false. | - | ||||||||||||
| 198 | - | |||||||||||||
| 199 | \sa setEnabled() | - | ||||||||||||
| 200 | */ | - | ||||||||||||
| 201 | bool QSocketNotifier::isEnabled() const | - | ||||||||||||
| 202 | { | - | ||||||||||||
| 203 | Q_D(const QSocketNotifier); | - | ||||||||||||
| 204 | return d->snenabled; executed 321802 times by 303 tests: return d->snenabled;Executed by:
| 321802 | ||||||||||||
| 205 | } | - | ||||||||||||
| 206 | - | |||||||||||||
| 207 | /*! | - | ||||||||||||
| 208 | If \a enable is true, the notifier is enabled; otherwise the notifier | - | ||||||||||||
| 209 | is disabled. | - | ||||||||||||
| 210 | - | |||||||||||||
| 211 | The notifier is enabled by default, i.e. it emits the activated() | - | ||||||||||||
| 212 | signal whenever a socket event corresponding to its | - | ||||||||||||
| 213 | \l{type()}{type} occurs. If it is disabled, it ignores socket | - | ||||||||||||
| 214 | events (the same effect as not creating the socket notifier). | - | ||||||||||||
| 215 | - | |||||||||||||
| 216 | Write notifiers should normally be disabled immediately after the | - | ||||||||||||
| 217 | activated() signal has been emitted | - | ||||||||||||
| 218 | - | |||||||||||||
| 219 | \sa isEnabled(), activated() | - | ||||||||||||
| 220 | */ | - | ||||||||||||
| 221 | - | |||||||||||||
| 222 | void QSocketNotifier::setEnabled(bool enable) | - | ||||||||||||
| 223 | { | - | ||||||||||||
| 224 | Q_D(QSocketNotifier); | - | ||||||||||||
| 225 | if (d->sockfd < 0)
| 0-812205 | ||||||||||||
| 226 | return; never executed: return; | 0 | ||||||||||||
| 227 | if (d->snenabled == enable) // no change
| 63113-749092 | ||||||||||||
| 228 | return; executed 749092 times by 360 tests: return;Executed by:
| 749092 | ||||||||||||
| 229 | d->snenabled = enable; | - | ||||||||||||
| 230 | - | |||||||||||||
| 231 | if (!d->threadData->eventDispatcher.load()) // perhaps application/thread is shutting down
| 30-63083 | ||||||||||||
| 232 | return; executed 30 times by 3 tests: return;Executed by:
| 30 | ||||||||||||
| 233 | if (Q_UNLIKELY(thread() != QThread::currentThread())) {
| 0-63083 | ||||||||||||
| 234 | qWarning("QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread"); | - | ||||||||||||
| 235 | return; never executed: return; | 0 | ||||||||||||
| 236 | } | - | ||||||||||||
| 237 | if (d->snenabled)
| 19937-43146 | ||||||||||||
| 238 | d->threadData->eventDispatcher.load()->registerSocketNotifier(this); executed 19937 times by 37 tests: d->threadData->eventDispatcher.load()->registerSocketNotifier(this);Executed by:
| 19937 | ||||||||||||
| 239 | else | - | ||||||||||||
| 240 | d->threadData->eventDispatcher.load()->unregisterSocketNotifier(this); executed 43146 times by 359 tests: d->threadData->eventDispatcher.load()->unregisterSocketNotifier(this);Executed by:
| 43146 | ||||||||||||
| 241 | } | - | ||||||||||||
| 242 | - | |||||||||||||
| 243 | - | |||||||||||||
| 244 | /*!\reimp | - | ||||||||||||
| 245 | */ | - | ||||||||||||
| 246 | bool QSocketNotifier::event(QEvent *e) | - | ||||||||||||
| 247 | { | - | ||||||||||||
| 248 | Q_D(QSocketNotifier); | - | ||||||||||||
| 249 | // Emits the activated() signal when a QEvent::SockAct or QEvent::SockClose is | - | ||||||||||||
| 250 | // received. | - | ||||||||||||
| 251 | if (e->type() == QEvent::ThreadChange) {
| 35-5910 | ||||||||||||
| 252 | if (d->snenabled) {
| 4-31 | ||||||||||||
| 253 | QMetaObject::invokeMethod(this, "setEnabled", Qt::QueuedConnection, | - | ||||||||||||
| 254 | Q_ARG(bool, d->snenabled)); | - | ||||||||||||
| 255 | setEnabled(false); | - | ||||||||||||
| 256 | } executed 31 times by 3 tests: end of blockExecuted by:
| 31 | ||||||||||||
| 257 | } executed 35 times by 3 tests: end of blockExecuted by:
| 35 | ||||||||||||
| 258 | QObject::event(e); // will activate filters | - | ||||||||||||
| 259 | if ((e->type() == QEvent::SockAct) || (e->type() == QEvent::SockClose)) {
| 0-5786 | ||||||||||||
| 260 | emit activated(d->sockfd, QPrivateSignal()); | - | ||||||||||||
| 261 | return true; executed 5786 times by 290 tests: return true;Executed by:
| 5786 | ||||||||||||
| 262 | } | - | ||||||||||||
| 263 | return false; executed 159 times by 8 tests: return false;Executed by:
| 159 | ||||||||||||
| 264 | } | - | ||||||||||||
| 265 | - | |||||||||||||
| 266 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |